Skip to content
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

Use bun for installation if applicable #125

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/install/stimulus_with_bun.rb
Original file line number Diff line number Diff line change
@@ -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"
24 changes: 21 additions & 3 deletions lib/tasks/stimulus_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
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.config.js").exist?
end
end
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? && Stimulus::Tasks.using_bun?
Rake::Task["stimulus:install:bun"].invoke
elsif Rails.root.join("package.json").exist?
Rake::Task["stimulus:install:node"].invoke
else
Expand All @@ -17,12 +30,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
Stimulus::Tasks.run_stimulus_install_template "stimulus_with_bun"
end
end

Expand Down