From 484f4536e8a9fde457983332c682ca1670063f40 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Tue, 9 Jan 2024 16:51:57 -0600 Subject: [PATCH] Use Thor's `apply` instead of prerequisite tasks The `javascript:install:shared` and `javascript:install:node_shared` tasks serve only as prerequisites for the other installer tasks; they should not be run on their own (nor listed with `rake --tasks`). By replacing those tasks with corresponding calls to Thor's `apply` method, we avoid the overhead of running `bin/rails app:template` (and `bundle install`) multiple times. This commit also renames `install_node.rb` to `install_procfile.rb` since it is concerned with generating a `Procfile.dev` file. --- lib/install/bun/install.rb | 2 ++ lib/install/esbuild/install.rb | 3 +++ .../{install_node.rb => install_procfile.rb} | 0 lib/install/rollup/install.rb | 3 +++ lib/install/webpack/install.rb | 3 +++ lib/tasks/jsbundling/install.rake | 18 ++++-------------- 6 files changed, 15 insertions(+), 14 deletions(-) rename lib/install/{install_node.rb => install_procfile.rb} (100%) diff --git a/lib/install/bun/install.rb b/lib/install/bun/install.rb index 15d5cde..707fea1 100644 --- a/lib/install/bun/install.rb +++ b/lib/install/bun/install.rb @@ -1,5 +1,7 @@ require 'json' +apply "#{__dir__}/../install.rb" + if Rails.root.join("Procfile.dev").exist? append_to_file "Procfile.dev", "js: bun run build --watch\n" else diff --git a/lib/install/esbuild/install.rb b/lib/install/esbuild/install.rb index 65bb9df..786d6cf 100644 --- a/lib/install/esbuild/install.rb +++ b/lib/install/esbuild/install.rb @@ -1,3 +1,6 @@ +apply "#{__dir__}/../install.rb" +apply "#{__dir__}/../install_procfile.rb" + say "Install esbuild" run "yarn add esbuild" diff --git a/lib/install/install_node.rb b/lib/install/install_procfile.rb similarity index 100% rename from lib/install/install_node.rb rename to lib/install/install_procfile.rb diff --git a/lib/install/rollup/install.rb b/lib/install/rollup/install.rb index dedeb4f..c2e5f94 100644 --- a/lib/install/rollup/install.rb +++ b/lib/install/rollup/install.rb @@ -1,3 +1,6 @@ +apply "#{__dir__}/../install.rb" +apply "#{__dir__}/../install_procfile.rb" + say "Install rollup with config" copy_file "#{__dir__}/rollup.config.js", "rollup.config.js" run "yarn add rollup @rollup/plugin-node-resolve" diff --git a/lib/install/webpack/install.rb b/lib/install/webpack/install.rb index a8671cb..9a5c5b4 100644 --- a/lib/install/webpack/install.rb +++ b/lib/install/webpack/install.rb @@ -1,3 +1,6 @@ +apply "#{__dir__}/../install.rb" +apply "#{__dir__}/../install_procfile.rb" + say "Install Webpack with config" copy_file "#{__dir__}/webpack.config.js", "webpack.config.js" run "yarn add webpack webpack-cli" diff --git a/lib/tasks/jsbundling/install.rake b/lib/tasks/jsbundling/install.rake index 870802d..9f10a4c 100644 --- a/lib/tasks/jsbundling/install.rake +++ b/lib/tasks/jsbundling/install.rake @@ -1,32 +1,22 @@ namespace :javascript do namespace :install do - desc "Install shared elements for all bundlers" - task :shared do - system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/install.rb", __dir__)}" - end - - desc "Install node-specific elements for bundlers that use node/yarn." - task :node_shared do - system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/install_node.rb", __dir__)}" - end - desc "Install Bun" - task bun: "javascript:install:shared" do + task :bun do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/bun/install.rb", __dir__)}" end desc "Install esbuild" - task esbuild: ["javascript:install:shared", "javascript:install:node_shared"] do + task :esbuild do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/esbuild/install.rb", __dir__)}" end desc "Install rollup.js" - task rollup: ["javascript:install:shared", "javascript:install:node_shared"] do + task :rollup do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/rollup/install.rb", __dir__)}" end desc "Install Webpack" - task webpack: ["javascript:install:shared", "javascript:install:node_shared"] do + task :webpack do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/webpack/install.rb", __dir__)}" end end