Module: JetUi::Generators::JsRuntime
- Defined in:
- lib/generators/jet_ui/js_runtime.rb
Overview
Detects how a host application manages its JavaScript, so the install generator can wire JetUi the right way without asking: pin controllers via importmap, or point the app at the npm package for Vite.
Constant Summary collapse
- VITE_CONFIGS =
%w[ vite.config.js vite.config.mjs vite.config.ts config/vite.json ].freeze
- PACKAGE_MANAGERS =
Ordered: the first lockfile found wins.
{ 'bun.lockb' => :bun, 'pnpm-lock.yaml' => :pnpm, 'yarn.lock' => :yarn, 'package-lock.json' => :npm }.freeze
Class Method Summary collapse
-
.detect(root) ⇒ Object
Vite wins when both are present: a working Vite config is a stronger signal than a leftover config/importmap.rb.
- .importmap?(root) ⇒ Boolean
- .install_command(root, package) ⇒ Object
- .package_json_vite?(root) ⇒ Boolean
- .package_manager(root) ⇒ Object
-
.package_manager_field(root) ⇒ Object
corepack's
"packageManager": "yarn@4.5.0"— the declared manager when there's no lockfile yet (e.g. a fresh vite_rails clone). - .vite?(root) ⇒ Boolean
Class Method Details
.detect(root) ⇒ Object
Vite wins when both are present: a working Vite config is a stronger signal than a leftover config/importmap.rb.
28 29 30 31 32 33 |
# File 'lib/generators/jet_ui/js_runtime.rb', line 28 def detect(root) return :vite if vite?(root) return :importmap if importmap?(root) :unknown end |
.importmap?(root) ⇒ Boolean
40 41 42 |
# File 'lib/generators/jet_ui/js_runtime.rb', line 40 def importmap?(root) File.exist?(File.join(root, 'config/importmap.rb')) end |
.install_command(root, package) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/generators/jet_ui/js_runtime.rb', line 63 def install_command(root, package) manager = package_manager(root) verb = manager == :npm ? 'install' : 'add' "#{manager} #{verb} #{package}" end |
.package_json_vite?(root) ⇒ Boolean
70 71 72 73 74 75 76 77 78 |
# File 'lib/generators/jet_ui/js_runtime.rb', line 70 def package_json_vite?(root) path = File.join(root, 'package.json') return false unless File.exist?(path) deps = JSON.parse(File.read(path)).values_at('dependencies', 'devDependencies').compact deps.flat_map(&:keys).include?('vite') rescue JSON::ParserError false end |
.package_manager(root) ⇒ Object
44 45 46 47 |
# File 'lib/generators/jet_ui/js_runtime.rb', line 44 def package_manager(root) PACKAGE_MANAGERS.each { |lock, name| return name if File.exist?(File.join(root, lock)) } package_manager_field(root) || :npm end |
.package_manager_field(root) ⇒ Object
corepack's "packageManager": "yarn@4.5.0" — the declared manager when
there's no lockfile yet (e.g. a fresh vite_rails clone).
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/generators/jet_ui/js_runtime.rb', line 51 def package_manager_field(root) path = File.join(root, 'package.json') return unless File.exist?(path) field = JSON.parse(File.read(path))['packageManager'] return unless field PACKAGE_MANAGERS.values.find { |name| name.to_s == field.split('@').first } rescue JSON::ParserError nil end |
.vite?(root) ⇒ Boolean
35 36 37 38 |
# File 'lib/generators/jet_ui/js_runtime.rb', line 35 def vite?(root) VITE_CONFIGS.any? { |file| File.exist?(File.join(root, file)) } || package_json_vite?(root) end |