lib/rails/generators/channel/channel_generator.rb in actioncable-7.0.8.6 vs lib/rails/generators/channel/channel_generator.rb in actioncable-7.1.0.beta1

- old
+ new

@@ -22,11 +22,11 @@ create_shared_channel_javascript_files import_channels_in_javascript_entrypoint if using_importmap? pin_javascript_dependencies - elsif using_node? + elsif using_js_runtime? install_javascript_dependencies end end create_channel_javascript_file @@ -55,36 +55,39 @@ end def create_channel_javascript_file channel_js_path = File.join("app/javascript/channels", class_path, "#{file_name}_channel") js_template "javascript/channel", channel_js_path - gsub_file "#{channel_js_path}.js", /\.\/consumer/, "channels/consumer" unless using_node? + gsub_file "#{channel_js_path}.js", /\.\/consumer/, "channels/consumer" unless using_js_runtime? end def import_channels_in_javascript_entrypoint append_to_file "app/javascript/application.js", - using_node? ? %(import "./channels"\n) : %(import "channels"\n) + using_js_runtime? ? %(import "./channels"\n) : %(import "channels"\n) end def import_channel_in_javascript_entrypoint append_to_file "app/javascript/channels/index.js", - using_node? ? %(import "./#{file_name}_channel"\n) : %(import "channels/#{file_name}_channel"\n) + using_js_runtime? ? %(import "./#{file_name}_channel"\n) : %(import "channels/#{file_name}_channel"\n) end def install_javascript_dependencies say "Installing JavaScript dependencies", :green - run "yarn add @rails/actioncable" + if using_bun? + run "bun add @rails/actioncable" + elsif using_node? + run "yarn add @rails/actioncable" + end end def pin_javascript_dependencies append_to_file "config/importmap.rb", <<-RUBY pin "@rails/actioncable", to: "actioncable.esm.js" pin_all_from "app/javascript/channels", under: "channels" RUBY end - def file_name @_file_name ||= super.sub(/_channel\z/i, "") end def first_setup_required? @@ -93,11 +96,22 @@ def using_javascript? @using_javascript ||= options[:assets] && root.join("app/javascript").exist? end + def using_js_runtime? + @using_js_runtime ||= root.join("package.json").exist? + end + + def using_bun? + # Cannot assume bun.lockb has been generated yet so we look for + # a file known to be generated by the jsbundling-rails gem + @using_bun ||= using_js_runtime? && root.join("bun.config.js").exist? + end + def using_node? - @using_node ||= root.join("package.json").exist? + # Bun is the only runtime that _isn't_ node. + @using_node ||= using_js_runtime? && !root.join("bun.config.js").exist? end def using_importmap? @using_importmap ||= root.join("config/importmap.rb").exist? end