aboutsummaryrefslogtreecommitdiffstats
path: root/actiontext/lib
diff options
context:
space:
mode:
authorJavan Makhmali <javan@javan.us>2019-01-17 10:41:44 -0500
committerJavan Makhmali <javan@javan.us>2019-01-17 10:42:09 -0500
commit866da19fd9fc12d001ab99ac87890ea1cebb2cd9 (patch)
treecf3e8f6000f5dff0e939b12509d483d753b38d91 /actiontext/lib
parent11b73c40c289b03226da5d3f5a8d71b009390dbd (diff)
downloadrails-866da19fd9fc12d001ab99ac87890ea1cebb2cd9.tar.gz
rails-866da19fd9fc12d001ab99ac87890ea1cebb2cd9.tar.bz2
rails-866da19fd9fc12d001ab99ac87890ea1cebb2cd9.zip
Tidy up action_text:install task
Automate installing the appropriate packages with yarn and appending them to the default application.js pack.
Diffstat (limited to 'actiontext/lib')
-rw-r--r--actiontext/lib/templates/installer.rb29
1 files changed, 20 insertions, 9 deletions
diff --git a/actiontext/lib/templates/installer.rb b/actiontext/lib/templates/installer.rb
index e7c6c2623e..990e41ca00 100644
--- a/actiontext/lib/templates/installer.rb
+++ b/actiontext/lib/templates/installer.rb
@@ -1,3 +1,13 @@
+require "pathname"
+require "json"
+
+APPLICATION_PACK_PATH = Pathname.new("app/javascript/packs/application.js")
+JS_PACKAGE_PATH = Pathname.new("#{__dir__}/../../package.json")
+
+JS_PACKAGE = JSON.load(JS_PACKAGE_PATH)
+JS_DEPENDENCIES = JS_PACKAGE["peerDependencies"].dup.merge \
+ JS_PACKAGE["name"] => "^#{JS_PACKAGE["version"]}"
+
say "Copying actiontext.scss to app/assets/stylesheets"
copy_file "#{__dir__}/actiontext.scss", "app/assets/stylesheets/actiontext.scss"
@@ -8,14 +18,15 @@ say "Copying blob rendering partial to app/views/active_storage/blobs/_blob.html
copy_file "#{__dir__}/../../app/views/active_storage/blobs/_blob.html.erb",
"app/views/active_storage/blobs/_blob.html.erb"
-say "Installing JavaScript dependency"
-run "yarn add @rails/actiontext"
-
-APPLICATION_PACK_PATH = "app/javascript/packs/application.js"
+say "Installing JavaScript dependencies"
+run "yarn add #{JS_DEPENDENCIES.map { |name, version| "#{name}@#{version}" }.join(" ")}"
-if File.exist?(APPLICATION_PACK_PATH) && File.read(APPLICATION_PACK_PATH) !~ /import "@rails\/actiontext"/
- say "Adding import to default JavaScript pack"
- append_to_file APPLICATION_PACK_PATH, <<-EOS
-import "@rails/actiontext"
-EOS
+if APPLICATION_PACK_PATH.exist?
+ JS_DEPENDENCIES.keys.each do |name|
+ line = %[require("#{name}")]
+ unless APPLICATION_PACK_PATH.read.include? line
+ say "Adding #{name} to #{APPLICATION_PACK_PATH}"
+ append_to_file APPLICATION_PACK_PATH, "#{line}\n"
+ end
+ end
end