11module RubyUI
22 module Generators
33 module JavascriptUtils
4+ TW_ANIMATE_CSS_VERSION = "1.4.0"
5+
46 def install_js_package ( package )
57 if using_importmap?
68 pin_with_importmap ( package )
@@ -21,6 +23,8 @@ def pin_with_importmap(package)
2123 case package
2224 when "motion"
2325 pin_motion
26+ when "tw-animate-css"
27+ pin_tw_animate_css
2428 when "tippy.js"
2529 pin_tippy_js
2630 else
@@ -29,23 +33,33 @@ def pin_with_importmap(package)
2933 end
3034
3135 def using_importmap?
32- File . exist? ( Rails . root . join ( "config/importmap.rb" ) ) && File . exist? ( Rails . root . join ( "bin/importmap" ) )
36+ File . exist? ( rails_root . join ( "config/importmap.rb" ) ) && File . exist? ( rails_root . join ( "bin/importmap" ) )
3337 end
3438
35- def using_bun? = File . exist? ( Rails . root . join ( "bun.lock" ) )
39+ def using_bun? = File . exist? ( rails_root . join ( "bun.lock" ) )
40+
41+ def using_npm? = File . exist? ( rails_root . join ( "package-lock.json" ) )
3642
37- def using_npm ? = File . exist? ( Rails . root . join ( "package -lock.json " ) )
43+ def using_pnpm ? = File . exist? ( rails_root . join ( "pnpm -lock.yaml " ) )
3844
39- def using_pnpm ? = File . exist? ( Rails . root . join ( "pnpm-lock.yaml " ) )
45+ def using_yarn ? = File . exist? ( rails_root . join ( "yarn.lock " ) )
4046
41- def using_yarn? = File . exist? ( Rails . root . join ( "yarn.lock" ) )
47+ def pin_tw_animate_css
48+ say <<~TEXT
49+ WARNING: Installing tw-animate-css as a CSS asset because Importmap cannot pin CSS-only package exports.
50+ TEXT
51+
52+ empty_directory rails_root . join ( "vendor/javascript" )
53+ get "https://cdn.jsdelivr.net/npm/tw-animate-css@#{ TW_ANIMATE_CSS_VERSION } /dist/tw-animate.css" ,
54+ rails_root . join ( "vendor/javascript/tw-animate-css.css" )
55+ end
4256
4357 def pin_motion
4458 say <<~TEXT
4559 WARNING: Installing motion from CDN because `bin/importmap pin motion` doesn't download the correct file.
4660 TEXT
4761
48- inject_into_file Rails . root . join ( "config/importmap.rb" ) , <<~RUBY
62+ inject_into_file rails_root . join ( "config/importmap.rb" ) , <<~RUBY
4963 pin "motion", to: "https://cdn.jsdelivr.net/npm/motion@11.11.17/+esm"\n
5064 RUBY
5165 end
@@ -55,11 +69,13 @@ def pin_tippy_js
5569 WARNING: Installing tippy.js from CDN because `bin/importmap pin tippy.js` doesn't download the correct file.
5670 TEXT
5771
58- inject_into_file Rails . root . join ( "config/importmap.rb" ) , <<~RUBY
72+ inject_into_file rails_root . join ( "config/importmap.rb" ) , <<~RUBY
5973 pin "tippy.js", to: "https://cdn.jsdelivr.net/npm/tippy.js@6.3.7/+esm"
6074 pin "@popperjs/core", to: "https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/+esm"\n
6175 RUBY
6276 end
77+
78+ def rails_root = Rails . root
6379 end
6480 end
6581end
0 commit comments