From 9cc1553638117add7804c63b50100f760ba097b4 Mon Sep 17 00:00:00 2001 From: Cristian Molina Date: Thu, 16 Nov 2023 18:55:23 -0300 Subject: [PATCH] Add bun support --- lib/generators/htmx/install_generator.rb | 41 ++++++++++++++--------- spec/generators/install_generator_spec.rb | 14 ++++++++ spec/support/files_helper.rb | 5 +++ 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/lib/generators/htmx/install_generator.rb b/lib/generators/htmx/install_generator.rb index 3cafd74..4830803 100644 --- a/lib/generators/htmx/install_generator.rb +++ b/lib/generators/htmx/install_generator.rb @@ -11,7 +11,9 @@ class InstallGenerator < ::Rails::Generators::Base # Setup HTMX def setup - if importmap? + if bun_configured? + setup_bun + elsif importmap? setup_importmap elsif webpacker? setup_webpacker @@ -24,6 +26,10 @@ def setup private + def bun_configured? + Pathname.new(destination_root).join('bun.config.js').exist? + end + def webpacker? !!defined?(Webpacker) end @@ -40,26 +46,33 @@ def manifest(javascript_dir) Pathname.new(destination_root).join(javascript_dir, 'application.js') end + def add_to_manifest(manifest, text) + if manifest.exist? + append_file manifest, "\n#{text}" + else + create_file manifest, text + end + end + + def setup_bun + run 'bun add htmx.org' + + manifest = manifest('app/javascript') + add_to_manifest(manifest, IMPORTMAP_SETUP) + end + def setup_importmap run 'bin/importmap pin htmx.org' manifest = manifest('app/javascript') - if manifest.exist? - append_file manifest, "\n#{IMPORTMAP_SETUP}" - else - create_file manifest, IMPORTMAP_SETUP - end + add_to_manifest(manifest, IMPORTMAP_SETUP) end def setup_sprockets manifest = manifest('app/assets/javascripts') - if manifest.exist? - append_file manifest, "\n#{SPROCKETS_SETUP}" - else - create_file manifest, SPROCKETS_SETUP - end + add_to_manifest(manifest, SPROCKETS_SETUP) end def setup_webpacker @@ -67,11 +80,7 @@ def setup_webpacker manifest = manifest(webpack_source_path) - if manifest.exist? - append_file(manifest, "\n#{WEBPACKER_SETUP}") - else - create_file(manifest, WEBPACKER_SETUP) - end + add_to_manifest(manifest, WEBPACKER_SETUP) end def webpack_source_path diff --git a/spec/generators/install_generator_spec.rb b/spec/generators/install_generator_spec.rb index 716d9d8..bea29e9 100644 --- a/spec/generators/install_generator_spec.rb +++ b/spec/generators/install_generator_spec.rb @@ -114,6 +114,20 @@ end end + context 'with bun configured' do + before do + generate_bun_config + end + + it 'updates file with htmx import' do + run_generator + assert_file( + 'app/javascript/application.js', + "\n#{Htmx::Generators::InstallGenerator::IMPORTMAP_SETUP}" + ) + end + end + context 'with no asset pipeline' do before do hide_const('Webpacker') diff --git a/spec/support/files_helper.rb b/spec/support/files_helper.rb index 29c6a5f..f8256dc 100644 --- a/spec/support/files_helper.rb +++ b/spec/support/files_helper.rb @@ -7,5 +7,10 @@ def generate_application_js(location_folder) FileUtils.mkdir_p(pathname) File.write("#{pathname}/application.js", '') end + + def generate_bun_config + FileUtils.mkdir_p(destination_root) + File.write("#{destination_root}/bun.config.js", "// Some JS\n") + end end end