stimulus_reflex 3.5.0.pre9 → 3.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of stimulus_reflex might be problematic. Click here for more details.

Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/Gemfile.lock +122 -127
  4. data/README.md +13 -19
  5. data/app/assets/javascripts/stimulus_reflex.js +1017 -523
  6. data/app/assets/javascripts/stimulus_reflex.umd.js +940 -496
  7. data/app/channels/stimulus_reflex/channel.rb +9 -24
  8. data/bin/console +0 -2
  9. data/bin/standardize +2 -1
  10. data/lib/generators/stimulus_reflex/stimulus_reflex_generator.rb +68 -9
  11. data/lib/generators/stimulus_reflex/templates/app/controllers/examples_controller.rb.tt +9 -0
  12. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/consumer.js.tt +6 -0
  13. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.esbuild.tt +4 -0
  14. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.importmap.tt +2 -0
  15. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.shakapacker.tt +5 -0
  16. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.vite.tt +1 -0
  17. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.webpacker.tt +5 -0
  18. data/lib/generators/stimulus_reflex/templates/app/javascript/config/cable_ready.js.tt +4 -0
  19. data/lib/generators/stimulus_reflex/templates/app/javascript/config/index.js.tt +2 -0
  20. data/lib/generators/stimulus_reflex/templates/app/javascript/config/mrujs.js.tt +9 -0
  21. data/lib/generators/stimulus_reflex/templates/app/javascript/config/stimulus_reflex.js.tt +5 -0
  22. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/%file_name%_controller.js.tt +141 -0
  23. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application.js.tt +11 -0
  24. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application_controller.js.tt +74 -0
  25. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.esbuild.tt +7 -0
  26. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.importmap.tt +5 -0
  27. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.shakapacker.tt +5 -0
  28. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.vite.tt +5 -0
  29. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.webpacker.tt +5 -0
  30. data/{test/tmp/app/reflexes/user_reflex.rb → lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt} +38 -9
  31. data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +27 -0
  32. data/lib/generators/stimulus_reflex/templates/app/views/examples/show.html.erb.tt +207 -0
  33. data/lib/generators/stimulus_reflex/templates/config/initializers/cable_ready.rb +27 -0
  34. data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +18 -13
  35. data/lib/generators/stimulus_reflex/templates/esbuild.config.mjs.tt +94 -0
  36. data/lib/install/action_cable.rb +155 -0
  37. data/lib/install/broadcaster.rb +90 -0
  38. data/lib/install/bundle.rb +56 -0
  39. data/lib/install/compression.rb +41 -0
  40. data/lib/install/config.rb +87 -0
  41. data/lib/install/development.rb +110 -0
  42. data/lib/install/esbuild.rb +114 -0
  43. data/lib/install/example.rb +22 -0
  44. data/lib/install/importmap.rb +133 -0
  45. data/lib/install/initializers.rb +25 -0
  46. data/lib/install/mrujs.rb +133 -0
  47. data/lib/install/npm_packages.rb +25 -0
  48. data/lib/install/reflexes.rb +25 -0
  49. data/lib/install/shakapacker.rb +64 -0
  50. data/lib/install/spring.rb +54 -0
  51. data/lib/install/updatable.rb +34 -0
  52. data/lib/install/vite.rb +64 -0
  53. data/lib/install/webpacker.rb +90 -0
  54. data/lib/install/yarn.rb +55 -0
  55. data/lib/stimulus_reflex/broadcasters/broadcaster.rb +15 -8
  56. data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +7 -8
  57. data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +10 -10
  58. data/lib/stimulus_reflex/broadcasters/update.rb +3 -0
  59. data/lib/stimulus_reflex/cable_readiness.rb +29 -0
  60. data/lib/stimulus_reflex/cable_ready_channels.rb +6 -5
  61. data/lib/stimulus_reflex/callbacks.rb +17 -1
  62. data/lib/stimulus_reflex/concern_enhancer.rb +6 -4
  63. data/lib/stimulus_reflex/configuration.rb +12 -2
  64. data/lib/stimulus_reflex/dataset.rb +11 -1
  65. data/lib/stimulus_reflex/engine.rb +16 -9
  66. data/lib/stimulus_reflex/html/document.rb +59 -0
  67. data/lib/stimulus_reflex/html/document_fragment.rb +13 -0
  68. data/lib/stimulus_reflex/importmap.rb +6 -3
  69. data/lib/stimulus_reflex/installer.rb +274 -0
  70. data/lib/stimulus_reflex/open_struct_fix.rb +2 -0
  71. data/lib/stimulus_reflex/reflex.rb +40 -31
  72. data/lib/stimulus_reflex/reflex_data.rb +19 -3
  73. data/lib/stimulus_reflex/reflex_factory.rb +6 -3
  74. data/lib/stimulus_reflex/request_parameters.rb +2 -0
  75. data/lib/stimulus_reflex/utils/logger.rb +10 -0
  76. data/lib/stimulus_reflex/utils/sanity_checker.rb +8 -48
  77. data/lib/stimulus_reflex/version.rb +1 -1
  78. data/lib/stimulus_reflex/version_checker.rb +54 -0
  79. data/lib/stimulus_reflex.rb +2 -0
  80. data/lib/tasks/stimulus_reflex/stimulus_reflex.rake +250 -0
  81. data/package.json +36 -28
  82. data/{rollup.config.js → rollup.config.mjs} +6 -24
  83. data/stimulus_reflex.gemspec +16 -19
  84. data/yarn.lock +1331 -748
  85. metadata +129 -79
  86. data/LATEST +0 -1
  87. data/app/assets/javascripts/stimulus_reflex.min.js +0 -2
  88. data/app/assets/javascripts/stimulus_reflex.min.js.map +0 -1
  89. data/app/assets/javascripts/stimulus_reflex.umd.min.js +0 -905
  90. data/app/assets/javascripts/stimulus_reflex.umd.min.js.map +0 -1
  91. data/lib/generators/stimulus_reflex/initializer_generator.rb +0 -14
  92. data/test/broadcasters/broadcaster_test.rb +0 -11
  93. data/test/broadcasters/broadcaster_test_case.rb +0 -39
  94. data/test/broadcasters/nothing_broadcaster_test.rb +0 -31
  95. data/test/broadcasters/page_broadcaster_test.rb +0 -79
  96. data/test/broadcasters/selector_broadcaster_test.rb +0 -173
  97. data/test/callbacks_test.rb +0 -652
  98. data/test/concern_enhancer_test.rb +0 -54
  99. data/test/element_test.rb +0 -254
  100. data/test/generators/stimulus_reflex_generator_test.rb +0 -58
  101. data/test/reflex_test.rb +0 -43
  102. data/test/test_helper.rb +0 -71
  103. data/test/tmp/app/reflexes/application_reflex.rb +0 -12
  104. data/yarn-error.log +0 -4964
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ hash = gemfile_hash
6
+
7
+ # run bundle only when gems are waiting to be added or removed
8
+ add = add_gem_list.exist? ? add_gem_list.readlines.map(&:chomp) : []
9
+ remove = remove_gem_list.exist? ? remove_gem_list.readlines.map(&:chomp) : []
10
+
11
+ if add.present? || remove.present?
12
+ lines = gemfile_path.readlines
13
+
14
+ remove.each do |name|
15
+ index = lines.index { |line| line =~ /gem ['"]#{name}['"]/ }
16
+ if index
17
+ if /^[^#]*gem ['"]#{name}['"]/.match?(lines[index])
18
+ lines[index] = "# #{lines[index]}"
19
+ say "✅ #{name} gem has been disabled"
20
+ else
21
+ say "⏩ #{name} gem is already disabled. Skipping."
22
+ end
23
+ end
24
+ end
25
+
26
+ add.each do |package|
27
+ matches = package.match(/(.+)@(.+)/)
28
+ name, version = matches[1], matches[2]
29
+
30
+ index = lines.index { |line| line =~ /gem ['"]#{name}['"]/ }
31
+ if index
32
+ if !lines[index].match(/^[^#]*gem ['"]#{name}['"].*#{version}['"]/)
33
+ lines[index] = "\ngem \"#{name}\", \"#{version}\"\n"
34
+ say "✅ #{name} gem has been installed"
35
+ else
36
+ say "⏩ #{name} gem is already installed. Skipping."
37
+ end
38
+ else
39
+ lines << "\ngem \"#{name}\", \"#{version}\"\n"
40
+ end
41
+ end
42
+
43
+ gemfile_path.write lines.join
44
+
45
+ bundle_command("install --quiet", "BUNDLE_IGNORE_MESSAGES" => "1") if hash != gemfile_hash
46
+ else
47
+ say "⏩ No rubygems depedencies to install. Skipping."
48
+ end
49
+
50
+ FileUtils.cp(development_working_path, development_path)
51
+ say "✅ development environment configuration installed"
52
+
53
+ FileUtils.cp(action_cable_initializer_working_path, action_cable_initializer_path)
54
+ say "✅ Action Cable initializer installed"
55
+
56
+ complete_step :bundle
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ initializer = action_cable_initializer_working_path.read
6
+
7
+ if gemfile.match?(/gem ['"]permessage_deflate['"]/)
8
+ say "⏩ permessage_deflate already present in Gemfile. Skipping."
9
+ else
10
+ add_gem "permessage_deflate@>= 0.1"
11
+ end
12
+
13
+ # add permessage_deflate config to Action Cable initializer
14
+ if initializer.exclude? "PermessageDeflate.configure"
15
+ create_or_append(action_cable_initializer_working_path, verbose: false) do
16
+ <<~RUBY
17
+ module ActionCable
18
+ module Connection
19
+ class ClientSocket
20
+ alias_method :old_initialize, :initialize
21
+ def initialize(env, event_target, event_loop, protocols)
22
+ old_initialize(env, event_target, event_loop, protocols)
23
+ @driver.add_extension(
24
+ PermessageDeflate.configure(
25
+ level: Zlib::BEST_COMPRESSION,
26
+ max_window_bits: 13
27
+ )
28
+ )
29
+ end
30
+ end
31
+ end
32
+ end
33
+ RUBY
34
+ end
35
+
36
+ say "✅ Action Cable initializer patched to deflate websocket traffic"
37
+ else
38
+ say "⏩ Action Cable initializer is already patched to deflate websocket traffic. Skipping."
39
+ end
40
+
41
+ complete_step :compression
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ step_path = "/app/javascript/config/"
8
+ index_src = fetch(step_path, "index.js.tt")
9
+ index_path = config_path / "index.js"
10
+ friendly_index_path = index_path.relative_path_from(Rails.root).to_s
11
+ stimulus_reflex_src = fetch(step_path, "stimulus_reflex.js.tt")
12
+ stimulus_reflex_path = config_path / "stimulus_reflex.js"
13
+ friendly_stimulus_reflex_path = stimulus_reflex_path.relative_path_from(Rails.root).to_s
14
+ cable_ready_src = fetch(step_path, "cable_ready.js.tt")
15
+ cable_ready_path = config_path / "cable_ready.js"
16
+
17
+ empty_directory config_path unless config_path.exist?
18
+
19
+ if index_path.exist?
20
+ say "⏩ #{friendly_index_path} already exists. Skipping"
21
+ else
22
+ backup(index_path, delete: true) do
23
+ copy_file(index_src, index_path)
24
+ end
25
+ say "✅ Created #{friendly_index_path}"
26
+ end
27
+
28
+ index_pattern = /import ['"](\.\.\/|\.\/)?config['"]/
29
+ index_commented_pattern = /\s*\/\/\s*#{index_pattern}/
30
+ index_import = "import \"#{prefix}config\"\n"
31
+
32
+ if pack.match?(index_pattern)
33
+ if pack.match?(index_commented_pattern)
34
+ lines = pack_path.readlines
35
+ matches = lines.select { |line| line =~ index_commented_pattern }
36
+ lines[lines.index(matches.last).to_i] = index_import
37
+ pack_path.write lines.join
38
+
39
+ say "✅ Uncommented StimulusReflex and CableReady configs imports in #{friendly_pack_path}"
40
+ else
41
+ say "⏩ StimulusReflex and CableReady configs are already being imported in #{friendly_pack_path}. Skipping"
42
+ end
43
+ else
44
+ lines = pack_path.readlines
45
+ matches = lines.select { |line| line =~ /^import / }
46
+ lines.insert lines.index(matches.last).to_i + 1, index_import
47
+ pack_path.write lines.join
48
+
49
+ say "✅ StimulusReflex and CableReady configs will be imported in #{friendly_pack_path}"
50
+ end
51
+
52
+ # create entrypoint/config/cable_ready.js and make sure it's imported in application.js
53
+ copy_file(cable_ready_src, cable_ready_path) unless cable_ready_path.exist?
54
+
55
+ # create entrypoint/config/stimulus_reflex.js and make sure it's imported in application.js
56
+ copy_file(stimulus_reflex_src, stimulus_reflex_path) unless stimulus_reflex_path.exist?
57
+
58
+ if stimulus_reflex_path.read.include?("StimulusReflex.debug =")
59
+ say "⏩ Development environment options are already set in #{friendly_stimulus_reflex_path}. Skipping"
60
+ else
61
+ if ["webpacker", "shakapacker"].include?(bundler)
62
+ append_file(stimulus_reflex_path, <<~JS, verbose: false)
63
+
64
+ if (process.env.RAILS_ENV === 'development') {
65
+ StimulusReflex.debug = true
66
+ }
67
+ JS
68
+ elsif bundler == "vite"
69
+ append_file(stimulus_reflex_path, <<~JS, verbose: false) unless stimulus_reflex_path.read.include?("StimulusReflex.debug")
70
+
71
+ if (import.meta.env.MODE === "development") {
72
+ StimulusReflex.debug = true
73
+ }
74
+ JS
75
+ else
76
+ append_file(stimulus_reflex_path, <<~JS, verbose: false)
77
+
78
+ // consider removing these options in production
79
+ StimulusReflex.debug = true
80
+ // end remove
81
+ JS
82
+ end
83
+
84
+ say "✅ Set useful development environment options in #{friendly_stimulus_reflex_path}"
85
+ end
86
+
87
+ complete_step :config
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ # mutate working copy of development.rb to avoid bundle alerts
6
+ FileUtils.cp(development_path, development_working_path)
7
+
8
+ # add default_url_options to development.rb for Action Mailer
9
+ if defined?(ActionMailer)
10
+ lines = development_working_path.readlines
11
+ if lines.find { |line| line.include?("config.action_mailer.default_url_options") }
12
+ say "⏩ Action Mailer default_url_options already defined. Skipping."
13
+ else
14
+ index = lines.index { |line| line =~ /^Rails.application.configure do/ }
15
+ lines.insert index + 1, " config.action_mailer.default_url_options = {host: \"localhost\", port: 3000}\n\n"
16
+ development_working_path.write lines.join
17
+
18
+ say "✅ Action Mailer default_url_options defined"
19
+ end
20
+ end
21
+
22
+ # add default_url_options to development.rb for Action Controller
23
+ lines = development_working_path.readlines
24
+ if lines.find { |line| line.include?("config.action_controller.default_url_options") }
25
+ say "⏩ Action Controller default_url_options already defined. Skipping."
26
+ else
27
+ index = lines.index { |line| line =~ /^Rails.application.configure do/ }
28
+ lines.insert index + 1, " config.action_controller.default_url_options = {host: \"localhost\", port: 3000}\n"
29
+ development_working_path.write lines.join
30
+
31
+ say "✅ Action Controller default_url_options defined"
32
+ end
33
+
34
+ # halt with instructions if using cookie store, otherwise, nudge towards Redis
35
+ lines = development_working_path.readlines
36
+
37
+ if (index = lines.index { |line| line =~ /^[^#]*config.session_store/ })
38
+ if /^[^#]*cookie_store/.match?(lines[index])
39
+ write_redis_recommendation(development_working_path, lines, index, gemfile)
40
+ halt "StimulusReflex does not support session cookies. See https://docs.stimulusreflex.com/hello-world/setup#session-storage"
41
+ return
42
+ elsif /^[^#]*redis_session_store/.match?(lines[index])
43
+ say "⏩ Already using redis-session-store for session storage. Skipping."
44
+ else
45
+ write_redis_recommendation(development_working_path, lines, index, gemfile)
46
+ say "🤷 We recommend using redis-session-store for session storage. See https://docs.stimulusreflex.com/hello-world/setup#session-storage"
47
+ end
48
+ # no session store defined, so let's opt-in to redis-session-store
49
+ else
50
+ # add redis-session-store to Gemfile
51
+ if !gemfile.match?(/gem ['"]redis-session-store['"]/)
52
+ if ActionCable::VERSION::MAJOR >= 7
53
+ add_gem "redis-session-store@~> 0.11.5"
54
+ else
55
+ add_gem "redis-session-store@0.11.4"
56
+ end
57
+ end
58
+
59
+ index = lines.index { |line| line =~ /^Rails.application.configure do/ }
60
+ lines.insert index + 1, <<~RUBY
61
+
62
+ config.session_store :redis_session_store,
63
+ serializer: :json,
64
+ on_redis_down: ->(*a) { Rails.logger.error("Redis down! \#{a.inspect}") },
65
+ redis: {
66
+ expire_after: 120.minutes,
67
+ key_prefix: "session:",
68
+ url: ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" }
69
+ }
70
+ RUBY
71
+ development_working_path.write lines.join
72
+ say "✅ Using redis-session-store for session storage"
73
+ end
74
+
75
+ # switch to redis for caching if using memory store, otherwise nudge with a comment
76
+ lines = development_working_path.readlines
77
+
78
+ if (index = lines.index { |line| line =~ /^[^#]*config.cache_store = :memory_store/ })
79
+ lines[index] = <<~RUBY
80
+ config.cache_store = :redis_cache_store, {
81
+ url: ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" }
82
+ }
83
+ RUBY
84
+ development_working_path.write lines.join
85
+ say "✅ Using Redis for caching"
86
+ elsif lines.index { |line| line =~ /^[^#]*config.cache_store = :redis_cache_store/ }
87
+ say "⏩ Already using Redis for caching. Skipping."
88
+ else
89
+ if !lines.index { |line| line.include?("We couldn't identify your cache store") }
90
+ lines.insert find_index(lines), <<~RUBY
91
+
92
+ # We couldn't identify your cache store, but recommend using Redis:
93
+
94
+ # config.cache_store = :redis_cache_store, {
95
+ # url: ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" }
96
+ # }
97
+ RUBY
98
+ development_working_path.write lines.join
99
+ end
100
+ say "🤷 We couldn't identify your cache store, but recommend using Redis. See https://docs.stimulusreflex.com/appendices/deployment#use-redis-as-your-cache-store"
101
+ end
102
+
103
+ if Rails.root.join("tmp", "caching-dev.txt").exist?
104
+ say "⏩ Already caching in development. Skipping."
105
+ else
106
+ system "rails dev:cache"
107
+ say "✅ Enabled caching in development"
108
+ end
109
+
110
+ complete_step :development
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ # verify that all critical dependencies are up to date; if not, queue for later
8
+ lines = package_json.readlines
9
+
10
+ if !lines.index { |line| line =~ /^\s*["']esbuild-rails["']: ["']\^1.0.3["']/ }
11
+ add_package "esbuild-rails@^1.0.3"
12
+ else
13
+ say "⏩ esbuild-rails npm package is already present. Skipping."
14
+ end
15
+
16
+ # copy esbuild.config.mjs to app root
17
+ esbuild_src = fetch("/", "esbuild.config.mjs.tt")
18
+ esbuild_path = Rails.root.join("esbuild.config.mjs")
19
+
20
+ if esbuild_path.exist?
21
+ if esbuild_path.read == esbuild_src.read
22
+ say "⏩ esbuild.config.mjs already present in app root. Skipping."
23
+ else
24
+ backup(esbuild_path) do
25
+ template(esbuild_src, esbuild_path, verbose: false, entrypoint: entrypoint)
26
+ end
27
+ say "✅ updated esbuild.config.mjs in app root"
28
+ end
29
+ else
30
+ template(esbuild_src, esbuild_path, entrypoint: entrypoint)
31
+ say "✅ Created esbuild.config.mjs in app root"
32
+ end
33
+
34
+ step_path = "/app/javascript/controllers/"
35
+ application_controller_src = fetch(step_path, "application_controller.js.tt")
36
+ application_controller_path = controllers_path / "application_controller.js"
37
+ application_js_src = fetch(step_path, "application.js.tt")
38
+ application_js_path = controllers_path / "application.js"
39
+ index_src = fetch(step_path, "index.js.esbuild.tt")
40
+ index_path = controllers_path / "index.js"
41
+ friendly_index_path = index_path.relative_path_from(Rails.root).to_s
42
+
43
+ # create entrypoint/controllers, if necessary
44
+ empty_directory controllers_path unless controllers_path.exist?
45
+
46
+ # copy application_controller.js, if necessary
47
+ copy_file(application_controller_src, application_controller_path) unless application_controller_path.exist?
48
+
49
+ # configure Stimulus application superclass to import Action Cable consumer
50
+ friendly_application_js_path = application_js_path.relative_path_from(Rails.root).to_s
51
+
52
+ if application_js_path.exist?
53
+ backup(application_js_path) do
54
+ if application_js_path.read.include?("import consumer")
55
+ say "⏩ #{friendly_application_js_path} is already present. Skipping."
56
+ else
57
+ inject_into_file application_js_path, "import consumer from \"../channels/consumer\"\n", after: "import { Application } from \"@hotwired/stimulus\"\n", verbose: false
58
+ inject_into_file application_js_path, "application.consumer = consumer\n", after: "application.debug = false\n", verbose: false
59
+ say "✅ #{friendly_application_js_path} has been updated to import the Action Cable consumer"
60
+ end
61
+ end
62
+ else
63
+ copy_file(application_js_src, application_js_path)
64
+ say "✅ #{friendly_application_js_path} has been created"
65
+ end
66
+
67
+ if index_path.exist?
68
+ if index_path.read == index_src.read
69
+ say "⏩ #{friendly_index_path} already present. Skipping."
70
+ else
71
+ backup(index_path, delete: true) do
72
+ copy_file(index_src, index_path, verbose: false)
73
+ end
74
+
75
+ say "✅ #{friendly_index_path} has been updated"
76
+ end
77
+ else
78
+ copy_file(index_src, index_path)
79
+ say "✅ #{friendly_index_path} has been created"
80
+ end
81
+
82
+ controllers_pattern = /import ['"].\/controllers['"]/
83
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
84
+
85
+ if pack.match?(controllers_pattern)
86
+ if pack.match?(controllers_commented_pattern)
87
+ proceed = if options.key? "uncomment"
88
+ options["uncomment"]
89
+ else
90
+ !no?("✨ Stimulus seems to be commented out in your application.js. Do you want to import your controllers? (Y/n)")
91
+ end
92
+
93
+ if proceed
94
+ # uncomment_lines only works with Ruby comments 🙄
95
+ lines = pack_path.readlines
96
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
97
+ lines[lines.index(matches.last).to_i] = "import \".\/controllers\"\n" # standard:disable Style/RedundantStringEscape
98
+ pack_path.write lines.join
99
+ say "✅ Uncommented Stimulus controllers import in #{friendly_pack_path}"
100
+ else
101
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
102
+ end
103
+ else
104
+ say "⏩ Stimulus controllers are already being imported in #{friendly_pack_path}. Skipping."
105
+ end
106
+ else
107
+ lines = pack_path.readlines
108
+ matches = lines.select { |line| line =~ /^import / }
109
+ lines.insert lines.index(matches.last).to_i + 1, "import \".\/controllers\"\n" # standard:disable Style/RedundantStringEscape
110
+ pack_path.write lines.join
111
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
112
+ end
113
+
114
+ complete_step :esbuild
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ proceed = false
6
+ if !Rails.root.join("app/reflexes/example_reflex.rb").exist?
7
+ proceed = if options.key? "example"
8
+ options["example"]
9
+ else
10
+ !no?("✨ Generate an example Reflex with a quick demo? You can remove it later with a single command. (Y/n)")
11
+ end
12
+ else
13
+ say "⏩ app/reflexes/example_reflex.rb already exists."
14
+ end
15
+
16
+ if proceed
17
+ generate("stimulus_reflex", "example")
18
+ else
19
+ say "⏩ Skipping."
20
+ end
21
+
22
+ complete_step :example
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ if !importmap_path.exist?
8
+ halt "#{friendly_importmap_path} is missing. You need a valid importmap config file to proceed."
9
+ return
10
+ end
11
+
12
+ importmap = importmap_path.read
13
+
14
+ backup(importmap_path) do
15
+ if !importmap.include?("pin_all_from \"#{entrypoint}/controllers\"")
16
+ append_file(importmap_path, <<~RUBY, verbose: false)
17
+ pin_all_from "#{entrypoint}/controllers", under: "controllers"
18
+ RUBY
19
+ say "✅ pin_all_from controllers"
20
+ else
21
+ say "⏩ pin_all_from controllers already pinned. Skipping."
22
+ end
23
+
24
+ if !importmap.include?("pin_all_from \"#{entrypoint}/channels\"")
25
+ append_file(importmap_path, <<~RUBY, verbose: false)
26
+ pin_all_from "#{entrypoint}/channels", under: "channels"
27
+ RUBY
28
+ say "✅ pin_all_from channels"
29
+ else
30
+ say "⏩ pin_all_from channels already pinned. Skipping."
31
+ end
32
+
33
+ if !importmap.include?("pin_all_from \"#{entrypoint}/config\"")
34
+ append_file(importmap_path, <<~RUBY, verbose: false)
35
+ pin_all_from "#{entrypoint}/config", under: "config"
36
+ RUBY
37
+ say "✅ pin_all_from config"
38
+ else
39
+ say "⏩ pin_all_from config already pinned. Skipping."
40
+ end
41
+
42
+ if !importmap.include?("pin \"@rails/actioncable\"")
43
+ append_file(importmap_path, <<~RUBY, verbose: false)
44
+ pin "@rails/actioncable", to: "actioncable.esm.js", preload: true
45
+ RUBY
46
+ say "✅ pin @rails/actioncable"
47
+ else
48
+ say "⏩ @rails/actioncable already pinned. Skipping."
49
+ end
50
+
51
+ if !importmap.include?("pin \"@hotwired/stimulus\"")
52
+ append_file(importmap_path, <<~RUBY, verbose: false)
53
+ pin "@hotwired/stimulus", to: "stimulus.js", preload: true
54
+ RUBY
55
+ say "✅ pin @hotwired/stimulus"
56
+ else
57
+ say "⏩ @hotwired/stimulus already pinned. Skipping."
58
+ end
59
+
60
+ if !importmap.include?("pin \"morphdom\"")
61
+ append_file(importmap_path, <<~RUBY, verbose: false)
62
+ pin "morphdom", to: "https://ga.jspm.io/npm:morphdom@2.6.1/dist/morphdom.js", preload: true
63
+ RUBY
64
+ say "✅ pin morphdom"
65
+ else
66
+ say "⏩ morphdom already pinned. Skipping."
67
+ end
68
+
69
+ if !importmap.include?("pin \"cable_ready\"")
70
+ append_file(importmap_path, <<~RUBY, verbose: false)
71
+ pin "cable_ready", to: "cable_ready.js", preload: true
72
+ RUBY
73
+ say "✅ pin cable_ready"
74
+ else
75
+ say "⏩ cable_ready already pinned. Skipping."
76
+ end
77
+
78
+ if !importmap.include?("pin \"stimulus_reflex\"")
79
+ append_file(importmap_path, <<~RUBY, verbose: false)
80
+ pin "stimulus_reflex", to: "stimulus_reflex.js", preload: true
81
+ RUBY
82
+ say "✅ pin stimulus_reflex"
83
+ else
84
+ say "⏩ stimulus_reflex already pinned. Skipping."
85
+ end
86
+ end
87
+
88
+ application_controller_src = fetch("/", "app/javascript/controllers/application_controller.js.tt")
89
+ application_controller_path = controllers_path / "application_controller.js"
90
+ application_js_src = fetch("/", "app/javascript/controllers/application.js.tt")
91
+ application_js_path = controllers_path / "application.js"
92
+ index_src = fetch("/", "app/javascript/controllers/index.js.importmap.tt")
93
+ index_path = controllers_path / "index.js"
94
+
95
+ # create entrypoint/controllers, as well as the index, application and application_controller
96
+ empty_directory controllers_path unless controllers_path.exist?
97
+
98
+ copy_file(application_controller_src, application_controller_path) unless application_controller_path.exist?
99
+
100
+ # configure Stimulus application superclass to import Action Cable consumer
101
+ backup(application_js_path) do
102
+ if application_js_path.exist?
103
+ friendly_application_js_path = application_js_path.relative_path_from(Rails.root).to_s
104
+ if application_js_path.read.include?("import consumer")
105
+ say "⏩ #{friendly_application_js_path} is present. Skipping."
106
+ else
107
+ inject_into_file application_js_path, "import consumer from \"../channels/consumer\"\n", after: "import { Application } from \"@hotwired/stimulus\"\n", verbose: false
108
+ inject_into_file application_js_path, "application.consumer = consumer\n", after: "application.debug = false\n", verbose: false
109
+ say "✅ #{friendly_application_js_path} has been updated to import the Action Cable consumer"
110
+ end
111
+ else
112
+ copy_file(application_js_src, application_js_path)
113
+ say "✅ #{friendly_application_js_path} has been created"
114
+ end
115
+ end
116
+
117
+ if index_path.exist?
118
+ friendly_index_path = index_path.relative_path_from(Rails.root).to_s
119
+
120
+ if index_path.read == index_src.read
121
+ say "⏩ #{friendly_index_path} is present. Skipping"
122
+ else
123
+ backup(index_path, delete: true) do
124
+ copy_file(index_src, index_path, verbose: false)
125
+ end
126
+ say "✅ #{friendly_index_path} has been updated"
127
+ end
128
+ else
129
+ copy_file(index_src, index_path)
130
+ say "✅ #{friendly_index_path} has been created."
131
+ end
132
+
133
+ complete_step :importmap
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ sr_initializer_src = fetch("/", "config/initializers/stimulus_reflex.rb")
6
+ sr_initializer_path = Rails.root.join("config/initializers/stimulus_reflex.rb")
7
+
8
+ cr_initializer_src = fetch("/", "config/initializers/cable_ready.rb")
9
+ cr_initializer_path = Rails.root.join("config/initializers/cable_ready.rb")
10
+
11
+ if !sr_initializer_path.exist?
12
+ copy_file(sr_initializer_src, sr_initializer_path, verbose: false)
13
+ say "✅ StimulusReflex initializer created at config/initializers/stimulus_reflex.rb"
14
+ else
15
+ say "⏩ config/initializers/stimulus_reflex.rb already exists. Skipping."
16
+ end
17
+
18
+ if !cr_initializer_path.exist?
19
+ copy_file(cr_initializer_src, cr_initializer_path, verbose: false)
20
+ say "✅ CableReady initializer created at config/initializers/cable_ready.rb"
21
+ else
22
+ say "⏩ config/initializers/cable_ready.rb already exists. Skipping."
23
+ end
24
+
25
+ complete_step :initializers