From 3c02cda4ae4cc80ba11679326a77b070fd09c731 Mon Sep 17 00:00:00 2001 From: Cristian Molina Date: Thu, 16 Nov 2023 18:55:23 -0300 Subject: [PATCH 1/5] Add bun support --- lib/generators/htmx/install_generator.rb | 46 ++++++++++++----------- spec/generators/install_generator_spec.rb | 30 +++++++++++++++ spec/support/files_helper.rb | 5 +++ 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/lib/generators/htmx/install_generator.rb b/lib/generators/htmx/install_generator.rb index 3cafd74..e5634bc 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,38 +46,34 @@ def manifest(javascript_dir) Pathname.new(destination_root).join(javascript_dir, 'application.js') end - def setup_importmap - run 'bin/importmap pin htmx.org' - - manifest = manifest('app/javascript') - + def add_to_manifest(manifest, text) if manifest.exist? - append_file manifest, "\n#{IMPORTMAP_SETUP}" + append_file manifest, "\n#{text}" else - create_file manifest, IMPORTMAP_SETUP + create_file manifest, text end end - def setup_sprockets - manifest = manifest('app/assets/javascripts') + def setup_bun + run 'bun add htmx.org' - if manifest.exist? - append_file manifest, "\n#{SPROCKETS_SETUP}" - else - create_file manifest, SPROCKETS_SETUP - end + add_to_manifest(manifest('app/javascript'), IMPORTMAP_SETUP) + end + + def setup_importmap + run 'bin/importmap pin htmx.org' + + add_to_manifest(manifest('app/javascript'), IMPORTMAP_SETUP) + end + + def setup_sprockets + add_to_manifest(manifest('app/assets/javascripts'), SPROCKETS_SETUP) end def setup_webpacker run 'yarn add htmx.org' - 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(webpack_source_path), 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..eaa078a 100644 --- a/spec/generators/install_generator_spec.rb +++ b/spec/generators/install_generator_spec.rb @@ -114,6 +114,36 @@ end end + context 'with bun configured' do + before do + generate_bun_config + end + + context 'when `application.js` exists' do + before do + generate_application_js('/app/javascript') + 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 'when `application.js` does not exists' do + it 'creates `application.js` file with htmx require' do + run_generator + assert_file( + 'app/javascript/application.js', + Htmx::Generators::InstallGenerator::IMPORTMAP_SETUP + ) + end + 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 From c084524dcc2414fe9d3d8492e28a6c74968bb731 Mon Sep 17 00:00:00 2001 From: Cristian Molina Date: Mon, 27 Nov 2023 09:56:52 -0300 Subject: [PATCH 2/5] Use version constant to download & install js Co-authored-by: Julian Pasquale --- lib/generators/htmx/install_generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/htmx/install_generator.rb b/lib/generators/htmx/install_generator.rb index e5634bc..17e77bc 100644 --- a/lib/generators/htmx/install_generator.rb +++ b/lib/generators/htmx/install_generator.rb @@ -55,7 +55,7 @@ def add_to_manifest(manifest, text) end def setup_bun - run 'bun add htmx.org' + run "bun add htmx.org@#{Htmx::Rails::HTMX_VERSION}" add_to_manifest(manifest('app/javascript'), IMPORTMAP_SETUP) end From 5a87452137fba53ac19b580f3460008d3a69ad61 Mon Sep 17 00:00:00 2001 From: Cristian Molina Date: Mon, 27 Nov 2023 10:09:13 -0300 Subject: [PATCH 3/5] Rename method to follow convention --- lib/generators/htmx/install_generator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/htmx/install_generator.rb b/lib/generators/htmx/install_generator.rb index 17e77bc..9528b34 100644 --- a/lib/generators/htmx/install_generator.rb +++ b/lib/generators/htmx/install_generator.rb @@ -11,7 +11,7 @@ class InstallGenerator < ::Rails::Generators::Base # Setup HTMX def setup - if bun_configured? + if bun? setup_bun elsif importmap? setup_importmap @@ -26,7 +26,7 @@ def setup private - def bun_configured? + def bun? Pathname.new(destination_root).join('bun.config.js').exist? end From 29f0e0788554a4d7def721ff2aa0af53e8e0e762 Mon Sep 17 00:00:00 2001 From: Cristian Molina Date: Mon, 27 Nov 2023 10:10:01 -0300 Subject: [PATCH 4/5] Use defined version constant to download & install the library --- lib/generators/htmx/install_generator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/htmx/install_generator.rb b/lib/generators/htmx/install_generator.rb index 9528b34..b9c7dfa 100644 --- a/lib/generators/htmx/install_generator.rb +++ b/lib/generators/htmx/install_generator.rb @@ -61,7 +61,7 @@ def setup_bun end def setup_importmap - run 'bin/importmap pin htmx.org' + run "bin/importmap pin htmx.org#{Htmx::Rails::HTMX_VERSION}" add_to_manifest(manifest('app/javascript'), IMPORTMAP_SETUP) end @@ -71,7 +71,7 @@ def setup_sprockets end def setup_webpacker - run 'yarn add htmx.org' + run "yarn add htmx.org#{Htmx::Rails::HTMX_VERSION}" add_to_manifest(manifest(webpack_source_path), WEBPACKER_SETUP) end From 1fa9e6bca4594f80b53bc2e7202964043e610c0f Mon Sep 17 00:00:00 2001 From: Cristian Molina Date: Mon, 27 Nov 2023 10:16:29 -0300 Subject: [PATCH 5/5] Add constant to make clear usage --- lib/generators/htmx/install_generator.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/generators/htmx/install_generator.rb b/lib/generators/htmx/install_generator.rb index b9c7dfa..22e48d8 100644 --- a/lib/generators/htmx/install_generator.rb +++ b/lib/generators/htmx/install_generator.rb @@ -6,6 +6,7 @@ class InstallGenerator < ::Rails::Generators::Base WEBPACKER_SETUP = "require('htmx.org')\n" SPROCKETS_SETUP = "//= require htmx\n" IMPORTMAP_SETUP = "import \"htmx.org\"\n" + BUN_SETUP = IMPORTMAP_SETUP desc 'Prep application.js to include HTMX installation for Webpacker, Sprockets or Importmap' @@ -57,7 +58,7 @@ def add_to_manifest(manifest, text) def setup_bun run "bun add htmx.org@#{Htmx::Rails::HTMX_VERSION}" - add_to_manifest(manifest('app/javascript'), IMPORTMAP_SETUP) + add_to_manifest(manifest('app/javascript'), BUN_SETUP) end def setup_importmap