Skip to content

Commit

Permalink
Use bun for installation if applicable (#494)
Browse files Browse the repository at this point in the history
* Use bun for installation if applicable

* Namespace rake helpers so they don't conflict

* Implement review suggestions
  • Loading branch information
terracatta authored Sep 25, 2023
1 parent 037a307 commit 8585e6a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
9 changes: 9 additions & 0 deletions lib/install/turbo_with_bun.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if (js_entrypoint_path = Rails.root.join("app/javascript/application.js")).exist?
say "Import Turbo"
append_to_file "app/javascript/application.js", %(import "@hotwired/turbo-rails"\n)
else
say "You must import @hotwired/turbo-rails in your JavaScript entrypoint file", :red
end

say "Install Turbo"
run "bun add @hotwired/turbo-rails"
53 changes: 35 additions & 18 deletions lib/tasks/turbo_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
def run_turbo_install_template(path)
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}"
end
module Turbo
module Tasks
extend self
def run_turbo_install_template(path)
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}"
end

def redis_installed?
Gem.win_platform? ?
system('where redis-server > NUL 2>&1') :
system('which redis-server > /dev/null')
end
def redis_installed?
Gem.win_platform? ?
system('where redis-server > NUL 2>&1') :
system('which redis-server > /dev/null')
end

def switch_on_redis_if_available
if redis_installed?
Rake::Task["turbo:install:redis"].invoke
else
puts "Run turbo:install:redis to switch on Redis and use it in development for turbo streams"
def switch_on_redis_if_available
if redis_installed?
Rake::Task["turbo:install:redis"].invoke
else
puts "Run turbo:install:redis to switch on Redis and use it in development for turbo streams"
end
end

def using_bun?
Rails.root.join("bun.config.js").exist?
end
end
end

Expand All @@ -21,6 +30,8 @@ namespace :turbo do
task :install do
if Rails.root.join("config/importmap.rb").exist?
Rake::Task["turbo:install:importmap"].invoke
elsif Rails.root.join("package.json").exist? && Turbo::Tasks.using_bun?
Rake::Task["turbo:install:bun"].invoke
elsif Rails.root.join("package.json").exist?
Rake::Task["turbo:install:node"].invoke
else
Expand All @@ -31,19 +42,25 @@ namespace :turbo do
namespace :install do
desc "Install Turbo into the app with asset pipeline"
task :importmap do
run_turbo_install_template "turbo_with_importmap"
switch_on_redis_if_available
Turbo::Tasks.run_turbo_install_template "turbo_with_importmap"
Turbo::Tasks.switch_on_redis_if_available
end

desc "Install Turbo into the app with webpacker"
task :node do
run_turbo_install_template "turbo_with_node"
switch_on_redis_if_available
Turbo::Tasks.run_turbo_install_template "turbo_with_node"
Turbo::Tasks.switch_on_redis_if_available
end

desc "Install Turbo into the app with bun"
task :bun do
Turbo::Tasks.run_turbo_install_template "turbo_with_bun"
Turbo::Tasks.switch_on_redis_if_available
end

desc "Switch on Redis and use it in development"
task :redis do
run_turbo_install_template "turbo_needs_redis"
Turbo::Tasks.run_turbo_install_template "turbo_needs_redis"
end
end
end

0 comments on commit 8585e6a

Please sign in to comment.