22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/generators/ultimate_static_modal/install_generator.rb', line 22
def update_controllers_index
index_path = Rails.root.join("app", "javascript", "controllers", "index.js")
unless File.exist?(index_path)
say_manual_snippet(index_path)
return
end
content = File.read(index_path)
static_import = "import StaticModalController from \"./static_modal_controller\"\n"
static_register = "application.register(\"static-modal\", StaticModalController)\n"
if content.include?("StaticModalController")
say "⏩ static-modal controller already registered.", :blue
else
if content.match?(/import .* from ["'](?:@hotwired\/stimulus|\.\/application)["']\n/)
insert_into_file index_path.to_s, static_import,
after: /import .* from ["'](?:@hotwired\/stimulus|\.\/application)["']\n/
else
prepend_to_file index_path.to_s, static_import
end
append_to_file index_path.to_s, static_register
say "✅ Registered `static-modal` Stimulus controller.", :green
end
content = File.read(index_path)
utmr_pattern = /import\s*\{\s*UltimateTurboModalController\s*\}\s*from\s*["']ultimate_turbo_modal["']\s*\n\s*application\.register\(\s*["']modal["']\s*,\s*UltimateTurboModalController\s*\)\s*\n/
fork_block = <<~JS
import ModalController from "./modal_controller"
// Side-effect import: turbo:frame-missing / before-frame-render / before-cache handlers
import "ultimate_turbo_modal"
application.register("modal", ModalController)
JS
if content.include?('import ModalController from "./modal_controller"')
say "⏩ Forked modal controller already registered.", :blue
elsif content.match?(utmr_pattern)
gsub_file index_path.to_s, utmr_pattern, fork_block
say "✅ Swapped UTMR modal registration for the forked null-safe controller (UTMR npm package still imported for side-effects).", :green
else
say "⚠️ Could not find UTMR's modal registration to swap.", :yellow
say " Add these lines to controllers/index.js manually, replacing any existing UTMR modal registration:", :yellow
fork_block.each_line { |line| say " #{line.rstrip}", :cyan }
end
end
|