Permalink
Browse files

Merge pull request #26836 from Liceth/npm

Add Yarn support in new apps using --yarn option
  • Loading branch information...
guilleiguaran committed Nov 29, 2016
2 parents ffb81ad + 9a95e13 commit 43e42ad385f1048e6ec7a9df6c83daa5ef003870
@@ -33,6 +33,9 @@ def self.add_shared_options_for(name)
class_option :javascript, type: :string, aliases: "-j",
desc: "Preconfigure for selected JavaScript library"
class_option :yarn, type: :boolean, default: false,
desc: "Preconfigure for assets management with Yarn"
class_option :skip_gemfile, type: :boolean, default: false,
desc: "Don't create a Gemfile"
@@ -411,6 +414,55 @@ def run_bundle
bundle_command("install") if bundle_install?
end
def run_yarn
if package_json_exist?
if yarn_path
say_status :run, "yarn install"
yarn_command("install")
else
say_status :warning, "yarn option passed but Yarn executable was not detected in the system.", :yellow
say_status :warning, "Download Yarn at https://yarnpkg.com/en/docs/install", :yellow
end
end
end
def package_json_exist?
File.exist?("package.json")
end
def yarn_path
commands = ["yarn"]
if RbConfig::CONFIG["host_os"] =~ /mswin|cygwin/
ENV["PATHEXT"].split(File::PATH_SEPARATOR).each do |ext|
commands << commands[0] + ext
end
end
yarn_path = commands.find do |cmd|
paths = ENV["PATH"].split(File::PATH_SEPARATOR)
path = paths.find do |p|
full_path = File.expand_path(cmd, p)
File.executable?(full_path) && File.file?(full_path)
end
path && File.expand_path(cmd, path)
end
yarn_path
end
def yarn_command(command)
full_command = "#{yarn_path} #{command}"
if options[:quiet]
system(full_command, out: File::NULL)
else
system(full_command)
end
end
def generate_spring_binstubs
if bundle_install? && spring_install?
bundle_command("exec spring binstub --all")
@@ -53,6 +53,10 @@ def gitignore
template "gitignore", ".gitignore"
end
def packagejson
template "package.json"
end
def app
directory "app"
@@ -205,6 +209,7 @@ def create_root_files
build(:readme)
build(:rakefile)
build(:configru)
build(:packagejson) if options[:yarn]
build(:gitignore) unless options[:skip_git]
build(:gemfile) unless options[:skip_gemfile]
end
@@ -355,7 +360,7 @@ def finish_template
end
public_task :apply_rails_template, :run_bundle
public_task :generate_spring_binstubs
public_task :run_yarn, :generate_spring_binstubs
def run_after_bundle_callbacks
@after_bundle_callbacks.each(&:call)
@@ -16,8 +16,11 @@ chdir APP_ROOT do
puts '== Installing dependencies =='
system! 'gem install bundler --conservative'
system('bundle check') || system!('bundle install')
<% unless options.skip_active_record -%>
<% if options[:yarn] %>
system! 'yarn'
<% end %>
<% unless options.skip_active_record -%>
# puts "\n== Copying sample files =="
# unless File.exist?('config/database.yml')
# cp 'config/database.yml.sample', 'config/database.yml'
@@ -16,8 +16,11 @@ chdir APP_ROOT do
puts '== Installing dependencies =='
system! 'gem install bundler --conservative'
system('bundle check') || system!('bundle install')
<% unless options.skip_active_record -%>
<% if options[:yarn] %>
system! 'yarn'
<% end %>
<% unless options.skip_active_record -%>
puts "\n== Updating database =="
system! 'bin/rails db:migrate'
<% end -%>
@@ -5,6 +5,10 @@ Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path
<%- if options[:yarn] -%>
# Add Yarn node_modules folder to the asset load path.
Rails.application.config.assets.paths << Rails.root.join('node_modules')
<%- end -%>
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in the app/assets
@@ -21,5 +21,10 @@
!/tmp/.keep
<% end -%>
<% if options[:yarn] -%>
# Ignore node modules.
/node_modules/*
<% end -%>
# Ignore Byebug command history file.
.byebug_history
@@ -0,0 +1,7 @@
{
"name": "<%= app_name %>",
"private": true,
"dependencies": {
"rails-ujs": "rails/rails-ujs"
}
}
@@ -492,6 +492,12 @@ def test_coffeescript_is_skipped_if_required
end
end
def test_generator_if_yarn_option_is_given
run_generator([destination_root, "--yarn"])
assert_file "package.json", /dependencies/
assert_file "config/initializers/assets.rb", /node_modules/
end
def test_inclusion_of_jbuilder
run_generator
assert_gem "jbuilder"
@@ -612,6 +618,10 @@ def test_generation_runs_bundle_install
assert_generates_with_bundler
end
def test_generation_runs_yarn_install_with_yarn_option
assert_generates_with_yarn yarn: true
end
def test_dev_option
assert_generates_with_bundler dev: true
rails_path = File.expand_path("../../..", Rails.root)
@@ -837,4 +847,18 @@ def assert_generates_with_bundler(options = {})
quietly { generator.invoke_all }
end
end
def assert_generates_with_yarn(options = {})
generator([destination_root], options)
command_check = -> command do
@install_called ||= 0
@install_called += 1
assert_equal 1, @install_called, "install expected to be called once, but was called #{@install_called} times"
end
generator.stub :yarn_command, command_check do
quietly { generator.invoke_all }
end
end
end

0 comments on commit 43e42ad

Please sign in to comment.