Skip to content

Commit

Permalink
Add bun support
Browse files Browse the repository at this point in the history
  • Loading branch information
megatux committed Nov 16, 2023
1 parent 6075b96 commit d4560ce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/generators/htmx/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -40,6 +46,17 @@ def manifest(javascript_dir)
Pathname.new(destination_root).join(javascript_dir, 'application.js')
end

def setup_bun
run 'bun add htmx.org'

manifest = manifest('app/javascript')
if manifest.exist?
append_file manifest, "\n#{IMPORTMAP_SETUP}"
else
create_file manifest, IMPORTMAP_SETUP
end
end

def setup_importmap
run 'bin/importmap pin htmx.org'

Expand Down
14 changes: 14 additions & 0 deletions spec/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions spec/support/files_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d4560ce

Please sign in to comment.