New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Yarn support in new apps using --yarn option #26836

Merged
merged 4 commits into from Nov 29, 2016
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+106 −3
Diff settings

Always

Just for now

@@ -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"

@@ -414,6 +417,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"
}
}
@@ -494,6 +494,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"
@@ -614,6 +620,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)
@@ -839,4 +849,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
ProTip! Use n and p to navigate between commits in a pull request.