From c234d9e7466f8c3f6359070432332a8611ca64d2 Mon Sep 17 00:00:00 2001 From: Jason Meller Date: Tue, 12 Sep 2023 09:46:49 -0400 Subject: [PATCH 1/3] Add Bun support --- lib/install/stimulus_with_bun.rb | 18 ++++++++++++++++++ lib/tasks/stimulus_tasks.rake | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 lib/install/stimulus_with_bun.rb diff --git a/lib/install/stimulus_with_bun.rb b/lib/install/stimulus_with_bun.rb new file mode 100644 index 0000000..e6250b8 --- /dev/null +++ b/lib/install/stimulus_with_bun.rb @@ -0,0 +1,18 @@ +say "Create controllers directory" +empty_directory "app/javascript/controllers" +copy_file "#{__dir__}/app/javascript/controllers/index_for_node.js", + "app/javascript/controllers/index.js" +copy_file "#{__dir__}/app/javascript/controllers/application.js", + "app/javascript/controllers/application.js" +copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js", + "app/javascript/controllers/hello_controller.js" + +if (Rails.root.join("app/javascript/application.js")).exist? + say "Import Stimulus controllers" + append_to_file "app/javascript/application.js", %(import "./controllers"\n) +else + say %(Couldn't find "app/javascript/application.js".\nYou must import "./controllers" in your JavaScript entrypoint file), :red +end + +say "Install Stimulus" +run "bun add @hotwired/stimulus" diff --git a/lib/tasks/stimulus_tasks.rake b/lib/tasks/stimulus_tasks.rake index 389dccc..0ca1dc9 100644 --- a/lib/tasks/stimulus_tasks.rake +++ b/lib/tasks/stimulus_tasks.rake @@ -1,12 +1,24 @@ require "stimulus/manifest" -def run_stimulus_install_template(path) system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}" end +def run_stimulus_install_template(path) + system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}" +end + +def using_bun? + Rails.root.join("bun.lockb").exist? || (tool_exists?('bun') && !Rails.root.join("yarn.lock").exist?) +end + +def tool_exists?(tool) + system "command -v #{tool} > /dev/null" +end namespace :stimulus do desc "Install Stimulus into the app" task :install do if Rails.root.join("config/importmap.rb").exist? Rake::Task["stimulus:install:importmap"].invoke + elsif Rails.root.join("package.json").exist? && using_bun? + Rake::Task["stimulus:install:bun"].invoke elsif Rails.root.join("package.json").exist? Rake::Task["stimulus:install:node"].invoke else @@ -24,6 +36,11 @@ namespace :stimulus do task :node do run_stimulus_install_template "stimulus_with_node" end + + desc "Install Stimulus on an app running bun" + task :bun do + run_stimulus_install_template "stimulus_with_bun" + end end namespace :manifest do From d879fa78ef2e99f3db15ce62ca02a805de666c9b Mon Sep 17 00:00:00 2001 From: Jason Meller Date: Wed, 13 Sep 2023 09:14:33 -0400 Subject: [PATCH 2/3] Namespace tasks so they don't conflict --- lib/tasks/stimulus_tasks.rake | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/tasks/stimulus_tasks.rake b/lib/tasks/stimulus_tasks.rake index 0ca1dc9..bcd6530 100644 --- a/lib/tasks/stimulus_tasks.rake +++ b/lib/tasks/stimulus_tasks.rake @@ -1,15 +1,20 @@ require "stimulus/manifest" -def run_stimulus_install_template(path) - system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}" -end +module Stimulus + module Tasks + extend self + def run_stimulus_install_template(path) + system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}" + end -def using_bun? - Rails.root.join("bun.lockb").exist? || (tool_exists?('bun') && !Rails.root.join("yarn.lock").exist?) -end + def using_bun? + Rails.root.join("bun.lockb").exist? || (tool_exists?('bun') && !Rails.root.join("yarn.lock").exist?) + end -def tool_exists?(tool) - system "command -v #{tool} > /dev/null" + def tool_exists?(tool) + system "command -v #{tool} > /dev/null" + end + end end namespace :stimulus do @@ -17,7 +22,7 @@ namespace :stimulus do task :install do if Rails.root.join("config/importmap.rb").exist? Rake::Task["stimulus:install:importmap"].invoke - elsif Rails.root.join("package.json").exist? && using_bun? + elsif Rails.root.join("package.json").exist? && Stimulus::Tasks.using_bun? Rake::Task["stimulus:install:bun"].invoke elsif Rails.root.join("package.json").exist? Rake::Task["stimulus:install:node"].invoke @@ -29,17 +34,17 @@ namespace :stimulus do namespace :install do desc "Install Stimulus on an app running importmap-rails" task :importmap do - run_stimulus_install_template "stimulus_with_importmap" + Stimulus::Tasks.run_stimulus_install_template "stimulus_with_importmap" end desc "Install Stimulus on an app running node" task :node do - run_stimulus_install_template "stimulus_with_node" + Stimulus::Tasks.run_stimulus_install_template "stimulus_with_node" end desc "Install Stimulus on an app running bun" task :bun do - run_stimulus_install_template "stimulus_with_bun" + Stimulus::Tasks.run_stimulus_install_template "stimulus_with_bun" end end From f903176f3bcf67390f6a1ceb19836c8c68e8b7ed Mon Sep 17 00:00:00 2001 From: Jason Meller Date: Sat, 23 Sep 2023 09:28:39 -0400 Subject: [PATCH 3/3] detect bun usage via presence of bun.config.js --- lib/tasks/stimulus_tasks.rake | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/tasks/stimulus_tasks.rake b/lib/tasks/stimulus_tasks.rake index bcd6530..8878a13 100644 --- a/lib/tasks/stimulus_tasks.rake +++ b/lib/tasks/stimulus_tasks.rake @@ -8,11 +8,7 @@ module Stimulus end def using_bun? - Rails.root.join("bun.lockb").exist? || (tool_exists?('bun') && !Rails.root.join("yarn.lock").exist?) - end - - def tool_exists?(tool) - system "command -v #{tool} > /dev/null" + Rails.root.join("bun.config.js").exist? end end end