Class: RubyCms::ImportmapWiring

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_cms/importmap_wiring.rb

Overview

Adds the importmap pins the admin JS needs to the host app's config/importmap.rb: the "admin" entrypoint plus the CDN-hosted libraries the admin Stimulus controllers and ruby_ui components import. No-op for apps without importmap (esbuild/bun) or when every pin is already present.

Constant Summary collapse

PINS =

name => full pin line. Order preserved on append.

{
  "admin" => 'pin "admin"',
  "@floating-ui/dom" => 'pin "@floating-ui/dom", to: "https://cdn.jsdelivr.net/npm/@floating-ui/dom/+esm"',
  "sortablejs" => 'pin "sortablejs", to: "https://cdn.jsdelivr.net/npm/sortablejs@1.15.7/+esm"',
  "maska" => 'pin "maska", to: "https://ga.jspm.io/npm:maska@3.1.1/dist/maska.mjs"',
  "motion" => 'pin "motion", to: "https://cdn.jsdelivr.net/npm/motion@11.11.17/+esm"',
  "fuse.js" => 'pin "fuse.js", to: "https://cdn.jsdelivr.net/npm/fuse.js@7.0.0/+esm"',
  "tippy.js" => 'pin "tippy.js", to: "https://cdn.jsdelivr.net/npm/tippy.js@6.3.7/+esm"',
  "@popperjs/core" => 'pin "@popperjs/core", to: "https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/+esm"',
  "embla-carousel" => 'pin "embla-carousel", to: "https://cdn.jsdelivr.net/npm/embla-carousel@8.5.2/+esm"',
  "mustache" => 'pin "mustache", to: "https://cdn.jsdelivr.net/npm/mustache@4.2.0/+esm"'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app_root:) ⇒ ImportmapWiring

Returns a new instance of ImportmapWiring.



25
26
27
# File 'lib/ruby_cms/importmap_wiring.rb', line 25

def initialize(app_root:)
  @path = Pathname.new(app_root).join("config/importmap.rb")
end

Instance Method Details

#apply!Object

rubocop:disable Naming/PredicateMethod



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_cms/importmap_wiring.rb', line 29

def apply! # rubocop:disable Naming/PredicateMethod
  return false unless @path.exist?

  content = @path.read
  missing = PINS.reject {|name, _| content.match?(/^\s*pin\s+["']#{Regexp.escape(name)}["']/) }
  return false if missing.empty?

  additions = missing.values.join("\n")
  @path.write("#{content.rstrip}\n\n# RubyCMS admin importmap pins\n#{additions}\n")
  true
end