npm-rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 70d1fb460960933460cc23ca9ee750dab26586b1
4
+ data.tar.gz: 11d347004b9b980843bd29938d993305bc53a408
5
+ SHA512:
6
+ metadata.gz: 336cc59ff6029d3cdb0964bf85fd0502689b2246f0e12a6f7277624c542e4a3ddae66c7f0adbdd9f6aa47fb2c3ccc4b0a846f0ff9fcb42916328e4b8584b7236
7
+ data.tar.gz: 0c04248b38f8b1acd31baea9a12ac3b05b60b230ccd48a258767e01883bdd09afd257f17034f252c7fa8c014e019d53fe1058db341317b770517270c94f1946b
@@ -0,0 +1,71 @@
1
+ # npm-rails
2
+
3
+ NPM support for Rails projects. It let you use Bundler-like DSL and rake tasks
4
+ for including npm packages. This gem based on Browserify for bundling packages
5
+ and resolve dependencies.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'npm-rails'
13
+ ```
14
+
15
+ Then run:
16
+
17
+ rails g npm_rails:initialize
18
+
19
+ And require `npm-dependencies.js`:
20
+
21
+ //=require npm-dependencies
22
+
23
+ ## Usage
24
+
25
+ 1. Add a package to `npm_packages` file
26
+ 2. Run `rake npm:install`
27
+ 3. Use the package in your javascript code by calling the camelize name
28
+ or `build_name` if you set it
29
+
30
+ **Example `npm_packages` file**
31
+
32
+ ```ruby
33
+ # call 'React' in your js code to use it
34
+ npm 'react'
35
+
36
+ # Set version
37
+ npm 'redux', '3.3.1'
38
+
39
+ # Set build_name to a package.
40
+ # Call '_' to get Underscore
41
+ npm 'underscore', build_name: '_'
42
+
43
+ # You can add a package for development
44
+ npm 'jasmine', development: true
45
+
46
+ # Or in block
47
+ development do
48
+ npm 'jasmine'
49
+ end
50
+
51
+ # Install a package but do not require it
52
+ npm 'browserify', require: false
53
+ ```
54
+
55
+ ## Configuration Options
56
+
57
+ The following options are available for configuration in your application or environment-level
58
+ config files (`config/application.rb`, `config/environments/development.rb`, etc.):
59
+
60
+ | Configuration Option | Description |
61
+ |---------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------- |
62
+ | `config.npm.package_file` | Specificies a package file. Default value: `npm_packages` |
63
+ | `config.npm.output_file` | Specifies a file where to bundle npm packages. Default value: `vendor/assets/javascripts/npm-dependencies.js`. |
64
+ | `config.npm.browserify_options` | Sets options for browserify command. See all available options in [Browserify documentation](https://github.com/substack/node-browserify#usage) |
65
+
66
+ ## How it works
67
+
68
+ The generator creates `npm_packages` file. This file contains a list of packages. Rake uses NPM to install the packages and Browserify to bundle them and output the bundled results to `vendor/assets/javascripts/npm-dependencies.js`, which are then loaded by sprockets. These generated bundle file have been added to your `.gitignore` for your convenience. All packages attached to `window`.
69
+
70
+
71
+
@@ -0,0 +1,20 @@
1
+ module NpmRails
2
+ class InitializeGenerator < Rails::Generators::Base
3
+ desc 'Adds a boilerplate package_file to the root of Rails project'
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def create_package_file
7
+ copy_file 'npm_packages', 'npm_packages'
8
+ end
9
+
10
+ def add_to_gitignore
11
+ append_to_file ".gitignore" do
12
+ <<-EOF.strip_heredoc
13
+ # Added by npm-rails
14
+ /node_modules
15
+ /vendor/assets/javascripts/npm-dependencies.js
16
+ EOF
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # Browserify is required.
2
+ # You can install it globally and delete from here.
3
+ npm 'browserify', require: false, development: true
4
+
5
+ # Add some npm packages
@@ -0,0 +1 @@
1
+ require 'npm/rails'
@@ -0,0 +1,7 @@
1
+ require 'npm/rails/version'
2
+ require 'npm/rails/railtie'
3
+ require 'npm/rails/package_bundler'
4
+ require 'npm/rails/package_manager'
5
+ require 'npm/rails/errors'
6
+ require 'npm/rails/package_file_parser'
7
+ require 'npm/rails/package'
@@ -0,0 +1,6 @@
1
+ module Npm
2
+ module Rails
3
+ class BrowserifyNotFound < StandardError ; end
4
+ class PackageFileNotFound < StandardError ; end
5
+ end
6
+ end
@@ -0,0 +1,32 @@
1
+ module Npm
2
+ module Rails
3
+ class Package
4
+
5
+ attr_reader :name
6
+ attr_reader :version
7
+ attr_reader :build_name
8
+
9
+ def initialize(name, version, options = {})
10
+ @name = name
11
+ @version = version
12
+ @development = options.fetch(:development, false)
13
+ @build_name = options.fetch(:build_name, create_build_name_from_name)
14
+ @require = options.fetch(:require, true)
15
+ end
16
+
17
+ def development?
18
+ @development
19
+ end
20
+
21
+ def should_require?
22
+ @require
23
+ end
24
+
25
+ private
26
+
27
+ def create_build_name_from_name
28
+ @name.gsub("-", "_").camelize
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+
2
+ module Npm
3
+ module Rails
4
+ class PackageBundler
5
+
6
+ def self.bundle(root_path, package_file, env, &block)
7
+ new.bundle(root_path, package_file, env, &block)
8
+ end
9
+
10
+ def bundle(root_path, package_file, env, &block)
11
+ @root_path = root_path
12
+ @package_file = package_file
13
+ @env = env
14
+
15
+ if File.exist?("#{ root_path }/#{ package_file }")
16
+ bundle_file_path = package_manager.write_bundle_file
17
+ if block_given?
18
+ yield package_manager.to_npm_format, bundle_file_path
19
+ end
20
+ else
21
+ raise PackageFileNotFound, "#{ package_file } not found! Make sure you have it at the root of your project"
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def package_manager
28
+ @package_manager ||= PackageManager.build(@root_path, @package_file, @env)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ module Npm
2
+ module Rails
3
+ class PackageFileParser
4
+
5
+ attr_reader :packages
6
+
7
+ def self.parse(package_file_path)
8
+ parser = new
9
+ parser.parse(package_file_path)
10
+ parser.packages
11
+ end
12
+
13
+ def initialize
14
+ @packages = []
15
+ @development = false
16
+ end
17
+
18
+ def parse(package_file_path)
19
+ @package_file = File.open(package_file_path, "r", &:read)
20
+ eval(@package_file)
21
+ end
22
+
23
+ private
24
+
25
+ def npm(package_name, *args)
26
+ options = args.last.is_a?(Hash) ? args.pop : {}
27
+ options.merge!(development: @development)
28
+ version = args.empty? ? "latest" : args.pop
29
+
30
+ @packages << Npm::Rails::Package.new(package_name, version, options)
31
+ end
32
+
33
+ def development
34
+ @development = true
35
+ yield
36
+ @development = false
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,55 @@
1
+ module Npm
2
+ module Rails
3
+ class PackageManager
4
+
5
+ def self.build(root_path, package_file, env)
6
+ package_file_path = "#{ root_path }/#{ package_file }"
7
+ packages = PackageFileParser.parse(package_file_path)
8
+ new(packages, root_path, env)
9
+ end
10
+
11
+ def initialize(packages, root_path, env)
12
+ @packages = packages
13
+ @root_path = root_path
14
+ @env = env
15
+ end
16
+
17
+ def write_bundle_file
18
+ bundle_file_path = "#{ @root_path }/tmp/npm-rails/bundle.js"
19
+ Dir.mkdir("tmp/npm-rails") unless File.exist?("tmp/npm-rails")
20
+ File.open(bundle_file_path, "w") do |file|
21
+ packages_for_bundle_file.each do |package|
22
+ file.write "window.#{ package.build_name } = require('#{ package.name }')\n"
23
+ end
24
+ end
25
+ bundle_file_path
26
+ end
27
+
28
+ # Return string of packages for 'npm install' command
29
+ def to_npm_format
30
+ @packages.inject "" do |string, package|
31
+ # do not add development packages in production environment
32
+ if (@env.production? and package.development?)
33
+ string
34
+ else
35
+ string << "#{ package.name }@\"#{ package.version }\" "
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def packages_for_bundle_file
43
+ if @env.production?
44
+ @packages.select do |package|
45
+ package.should_require? && !package.development?
46
+ end
47
+ else
48
+ @packages.select do |package|
49
+ package.should_require?
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,16 @@
1
+ require "rails"
2
+ require "rails/railtie"
3
+
4
+ module Npm
5
+ module Rails
6
+ class Railtie < ::Rails::Railtie
7
+ config.npm = ActiveSupport::OrderedOptions.new
8
+ config.npm.package_file = "npm_packages"
9
+ config.npm.output_file = "vendor/assets/javascripts/npm-dependencies.js"
10
+
11
+ rake_tasks do
12
+ load "tasks/npm.rake"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Npm
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ require 'fileutils'
2
+ require "mkmf"
3
+
4
+ namespace :npm do
5
+ desc "Install npm packages"
6
+ task :install do
7
+ package_file = ::Rails.configuration.npm.package_file
8
+ output_file_path = ::Rails.root.join(::Rails.configuration.npm.output_file)
9
+ browserify_options = ::Rails.configuration.npm.browserify_options
10
+
11
+ Npm::Rails::PackageBundler.bundle(::Rails.root, package_file, ::Rails.env) do |packages, bundle_file_path|
12
+ FileUtils.touch(output_file_path) unless File.exist?(output_file_path)
13
+ sh "cd #{ ::Rails.root }"
14
+ sh "npm install --loglevel error #{ packages }"
15
+
16
+ browserify = find_executable0("browserify") ||
17
+ find_executable0("#{ ::Rails.root }/node_modules/.bin/browserify")
18
+
19
+ if browserify.nil?
20
+ raise Npm::Rails::BrowserifyNotFound, "Browserify not found! You can install Browserify using npm: npm install browserify -g"
21
+ end
22
+
23
+ if Rails.env.production?
24
+ browserify_comamnd = "NODE_ENV=production #{ browserify } #{ browserify_options } #{ bundle_file_path } > #{ output_file_path }"
25
+ else
26
+ browserify_command = "#{ browserify } #{ browserify_options } #{ bundle_file_path } > #{ output_file_path }"
27
+ end
28
+
29
+ sh browserify_command
30
+ end
31
+ end
32
+ end
33
+
34
+ task "assets:precompile" => ["npm:install"]
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: npm-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stepan Lusnikov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ description: Use NPM packages in you Ruby on Rails application
70
+ email:
71
+ - endenwer@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - lib/generators/npm_rails/initialize_generator.rb
78
+ - lib/generators/npm_rails/templates/npm_packages
79
+ - lib/npm-rails.rb
80
+ - lib/npm/rails.rb
81
+ - lib/npm/rails/errors.rb
82
+ - lib/npm/rails/package.rb
83
+ - lib/npm/rails/package_bundler.rb
84
+ - lib/npm/rails/package_file_parser.rb
85
+ - lib/npm/rails/package_manager.rb
86
+ - lib/npm/rails/railtie.rb
87
+ - lib/npm/rails/version.rb
88
+ - lib/tasks/npm.rake
89
+ homepage: https://github.com/endenwer/npm-rails
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: NPM & Ruby on Rails integration
113
+ test_files: []