aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/rails/generators/app_base.rb52
-rw-r--r--railties/lib/rails/generators/rails/app/app_generator.rb7
-rw-r--r--railties/lib/rails/generators/rails/app/templates/package.json7
3 files changed, 65 insertions, 1 deletions
diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb
index 2951d6c83d..f54604b54d 100644
--- a/railties/lib/rails/generators/app_base.rb
+++ b/railties/lib/rails/generators/app_base.rb
@@ -33,6 +33,9 @@ module Rails
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 @@ module Rails
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")
diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb
index d6ffa2d89d..03fd298fbc 100644
--- a/railties/lib/rails/generators/rails/app/app_generator.rb
+++ b/railties/lib/rails/generators/rails/app/app_generator.rb
@@ -53,6 +53,10 @@ module Rails
template "gitignore", ".gitignore"
end
+ def packagejson
+ template "package.json"
+ end
+
def app
directory "app"
@@ -205,6 +209,7 @@ module Rails
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 @@ module Rails
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)
diff --git a/railties/lib/rails/generators/rails/app/templates/package.json b/railties/lib/rails/generators/rails/app/templates/package.json
new file mode 100644
index 0000000000..78f4a2e6b0
--- /dev/null
+++ b/railties/lib/rails/generators/rails/app/templates/package.json
@@ -0,0 +1,7 @@
+{
+ "name": "<%= app_name %>",
+ "private": true,
+ "dependencies": {
+ "rails-ujs": "rails/rails-ujs"
+ }
+}