stimulus_reflex 3.5.0.pre9 → 3.5.0.pre10

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 (103) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/Gemfile.lock +122 -127
  4. data/README.md +10 -16
  5. data/app/assets/javascripts/stimulus_reflex.js +710 -505
  6. data/app/assets/javascripts/stimulus_reflex.min.js +1 -1
  7. data/app/assets/javascripts/stimulus_reflex.min.js.map +1 -1
  8. data/app/assets/javascripts/stimulus_reflex.umd.js +660 -500
  9. data/app/assets/javascripts/stimulus_reflex.umd.min.js +660 -500
  10. data/app/assets/javascripts/stimulus_reflex.umd.min.js.map +1 -1
  11. data/app/channels/stimulus_reflex/channel.rb +9 -7
  12. data/bin/console +0 -2
  13. data/bin/standardize +2 -1
  14. data/lib/generators/stimulus_reflex/stimulus_reflex_generator.rb +72 -7
  15. data/lib/generators/stimulus_reflex/templates/app/controllers/examples_controller.rb.tt +9 -0
  16. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/consumer.js.tt +6 -0
  17. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.esbuild.tt +4 -0
  18. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.importmap.tt +2 -0
  19. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.shakapacker.tt +5 -0
  20. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.vite.tt +1 -0
  21. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.webpacker.tt +5 -0
  22. data/lib/generators/stimulus_reflex/templates/app/javascript/config/cable_ready.js.tt +4 -0
  23. data/lib/generators/stimulus_reflex/templates/app/javascript/config/index.js.tt +2 -0
  24. data/lib/generators/stimulus_reflex/templates/app/javascript/config/mrujs.js.tt +9 -0
  25. data/lib/generators/stimulus_reflex/templates/app/javascript/config/stimulus_reflex.js.tt +5 -0
  26. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/%file_name%_controller.js.tt +141 -0
  27. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application.js.tt +11 -0
  28. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application_controller.js.tt +74 -0
  29. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.esbuild.tt +7 -0
  30. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.importmap.tt +5 -0
  31. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.shakapacker.tt +5 -0
  32. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.vite.tt +5 -0
  33. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.webpacker.tt +5 -0
  34. data/{test/tmp/app/reflexes/user_reflex.rb → lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt} +38 -9
  35. data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +27 -0
  36. data/lib/generators/stimulus_reflex/templates/app/views/examples/show.html.erb.tt +207 -0
  37. data/lib/generators/stimulus_reflex/templates/config/initializers/cable_ready.rb +22 -0
  38. data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +18 -13
  39. data/lib/generators/stimulus_reflex/templates/esbuild.config.mjs.tt +94 -0
  40. data/lib/install/action_cable.rb +155 -0
  41. data/lib/install/broadcaster.rb +90 -0
  42. data/lib/install/bundle.rb +56 -0
  43. data/lib/install/compression.rb +41 -0
  44. data/lib/install/config.rb +87 -0
  45. data/lib/install/development.rb +110 -0
  46. data/lib/install/esbuild.rb +114 -0
  47. data/lib/install/example.rb +22 -0
  48. data/lib/install/importmap.rb +133 -0
  49. data/lib/install/initializers.rb +25 -0
  50. data/lib/install/mrujs.rb +133 -0
  51. data/lib/install/npm_packages.rb +25 -0
  52. data/lib/install/reflexes.rb +25 -0
  53. data/lib/install/shakapacker.rb +64 -0
  54. data/lib/install/spring.rb +54 -0
  55. data/lib/install/updatable.rb +34 -0
  56. data/lib/install/vite.rb +64 -0
  57. data/lib/install/webpacker.rb +90 -0
  58. data/lib/install/yarn.rb +55 -0
  59. data/lib/stimulus_reflex/broadcasters/broadcaster.rb +15 -8
  60. data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +7 -8
  61. data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +10 -10
  62. data/lib/stimulus_reflex/broadcasters/update.rb +3 -0
  63. data/lib/stimulus_reflex/cable_readiness.rb +29 -0
  64. data/lib/stimulus_reflex/cable_ready_channels.rb +5 -5
  65. data/lib/stimulus_reflex/callbacks.rb +17 -1
  66. data/lib/stimulus_reflex/concern_enhancer.rb +6 -4
  67. data/lib/stimulus_reflex/configuration.rb +12 -2
  68. data/lib/stimulus_reflex/dataset.rb +11 -1
  69. data/lib/stimulus_reflex/engine.rb +20 -9
  70. data/lib/stimulus_reflex/html/document.rb +59 -0
  71. data/lib/stimulus_reflex/html/document_fragment.rb +13 -0
  72. data/lib/stimulus_reflex/importmap.rb +3 -0
  73. data/lib/stimulus_reflex/installer.rb +274 -0
  74. data/lib/stimulus_reflex/open_struct_fix.rb +2 -0
  75. data/lib/stimulus_reflex/reflex.rb +25 -25
  76. data/lib/stimulus_reflex/reflex_data.rb +15 -3
  77. data/lib/stimulus_reflex/reflex_factory.rb +4 -2
  78. data/lib/stimulus_reflex/request_parameters.rb +2 -0
  79. data/lib/stimulus_reflex/utils/logger.rb +10 -0
  80. data/lib/stimulus_reflex/utils/sanity_checker.rb +8 -48
  81. data/lib/stimulus_reflex/version.rb +1 -1
  82. data/lib/stimulus_reflex.rb +2 -0
  83. data/lib/tasks/stimulus_reflex/stimulus_reflex.rake +252 -0
  84. data/package.json +34 -28
  85. data/{rollup.config.js → rollup.config.mjs} +8 -7
  86. data/stimulus_reflex.gemspec +16 -19
  87. data/yarn.lock +1320 -742
  88. metadata +124 -77
  89. data/LATEST +0 -1
  90. data/lib/generators/stimulus_reflex/initializer_generator.rb +0 -14
  91. data/test/broadcasters/broadcaster_test.rb +0 -11
  92. data/test/broadcasters/broadcaster_test_case.rb +0 -39
  93. data/test/broadcasters/nothing_broadcaster_test.rb +0 -31
  94. data/test/broadcasters/page_broadcaster_test.rb +0 -79
  95. data/test/broadcasters/selector_broadcaster_test.rb +0 -173
  96. data/test/callbacks_test.rb +0 -652
  97. data/test/concern_enhancer_test.rb +0 -54
  98. data/test/element_test.rb +0 -254
  99. data/test/generators/stimulus_reflex_generator_test.rb +0 -58
  100. data/test/reflex_test.rb +0 -43
  101. data/test/test_helper.rb +0 -71
  102. data/test/tmp/app/reflexes/application_reflex.rb +0 -12
  103. data/yarn-error.log +0 -4964
@@ -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_path)
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_path)
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.min.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.min.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.min.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
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ mrujs_path = config_path / "mrujs.js"
8
+
9
+ proceed = false
10
+
11
+ if !File.exist?(mrujs_path)
12
+ proceed = if options.key? "mrujs"
13
+ options["mrujs"]
14
+ else
15
+ !no?("✨ Would you like to install and enable mrujs? It's a modern, drop-in replacement for rails-ujs (Y/n)")
16
+ end
17
+ end
18
+
19
+ if proceed
20
+ if bundler == "importmap"
21
+
22
+ if !importmap_path.exist?
23
+ halt "#{friendly_importmap_path} is missing. You need a valid importmap config file to proceed."
24
+ return
25
+ end
26
+
27
+ importmap = importmap_path.read
28
+
29
+ if importmap.include?("pin \"mrujs\"")
30
+ say "⏩ mrujs already pinned. Skipping."
31
+ else
32
+ append_file(importmap_path, <<~RUBY, verbose: false)
33
+ pin "mrujs", to: "https://ga.jspm.io/npm:mrujs@0.10.1/dist/index.module.js"
34
+ RUBY
35
+ say "✅ pin mrujs"
36
+ end
37
+
38
+ if importmap.include?("pin \"mrujs/plugins\"")
39
+ say "⏩ mrujs/plugins already pinned. Skipping."
40
+ else
41
+ append_file(importmap_path, <<~RUBY, verbose: false)
42
+ pin "mrujs/plugins", to: "https://ga.jspm.io/npm:mrujs@0.10.1/plugins/dist/plugins.module.js"
43
+ RUBY
44
+ say "✅ pin mrujs/plugins"
45
+ end
46
+ else
47
+ # queue mrujs for installation
48
+ if package_json.read.include?('"mrujs":')
49
+ say "⏩ mrujs already present. Skipping."
50
+ else
51
+ add_package "mrujs@^0.10.1"
52
+ end
53
+
54
+ # queue @rails/ujs for removal
55
+ if package_json.read.include?('"@rails/ujs":')
56
+ drop_package "@rails/ujs"
57
+ else
58
+ say "⏩ @rails/ujs not present. Skipping."
59
+ end
60
+ end
61
+
62
+ step_path = "/app/javascript/config/"
63
+ mrujs_src = fetch(step_path, "mrujs.js.tt")
64
+
65
+ # create entrypoint/config/mrujs.js if necessary
66
+ copy_file(mrujs_src, mrujs_path) unless mrujs_path.exist?
67
+
68
+ # import mrujs config in entrypoint/config/index.js
69
+ index_path = config_path / "index.js"
70
+ index = index_path.read
71
+ friendly_index_path = index_path.relative_path_from(Rails.root).to_s
72
+ mrujs_pattern = /import ['"].\/mrujs['"]/
73
+ mrujs_import = "import '.\/mrujs'\n" # standard:disable Style/RedundantStringEscape
74
+
75
+ if index.match?(mrujs_pattern)
76
+ say "⏩ mrujs alredy imported in #{friendly_index_path}. Skipping."
77
+ else
78
+ append_file(index_path, mrujs_import, verbose: false)
79
+ say "✅ mrujs imported in #{friendly_index_path}"
80
+ end
81
+
82
+ # remove @rails/ujs from application.js
83
+ rails_ujs_pattern = /import Rails from ['"]@rails\/ujs['"]/
84
+
85
+ lines = pack_path.readlines
86
+ if lines.index { |line| line =~ rails_ujs_pattern }
87
+ gsub_file pack_path, rails_ujs_pattern, "", verbose: false
88
+ say "✅ @rails/ujs removed from #{friendly_pack_path}"
89
+ else
90
+ say "⏩ @rails/ujs not present in #{friendly_pack_path}. Skipping."
91
+ end
92
+
93
+ # set Action View to generate remote forms when using form_with
94
+ application_path = Rails.root.join("config/application.rb")
95
+ application_pattern = /^[^#]*config\.action_view\.form_with_generates_remote_forms = true/
96
+ defaults_pattern = /config\.load_defaults \d\.\d/
97
+
98
+ lines = application_path.readlines
99
+ backup(application_path) do
100
+ if !lines.index { |line| line =~ application_pattern }
101
+ if (index = lines.index { |line| line =~ /^[^#]*#{defaults_pattern}/ })
102
+ gsub_file application_path, /\s*#{defaults_pattern}\n/, verbose: false do
103
+ <<-RUBY
104
+ \n#{lines[index]}
105
+ # form_with helper will generate remote forms by default (mrujs)
106
+ config.action_view.form_with_generates_remote_forms = true
107
+ RUBY
108
+ end
109
+ else
110
+ insert_into_file application_path, after: "class Application < Rails::Application" do
111
+ <<-RUBY
112
+
113
+ # form_with helper will generate remote forms by default (mrujs)
114
+ config.action_view.form_with_generates_remote_forms = true
115
+ RUBY
116
+ end
117
+ end
118
+ end
119
+ say "✅ form_with_generates_remote_forms set to true in config/application.rb"
120
+ end
121
+
122
+ # remove turbolinks from Gemfile because it's incompatible with mrujs (and unnecessary)
123
+ turbolinks_pattern = /^[^#]*gem ["']turbolinks["']/
124
+
125
+ lines = gemfile_path.readlines
126
+ if lines.index { |line| line =~ turbolinks_pattern }
127
+ remove_gem :turbolinks
128
+ else
129
+ say "⏩ turbolinks is not present in Gemfile. Skipping."
130
+ end
131
+ end
132
+
133
+ complete_step :mrujs
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ lines = package_json.readlines
6
+
7
+ if !lines.index { |line| line =~ /^\s*["']cable_ready["']: ["'].*#{cr_npm_version}["']/ }
8
+ add_package "cable_ready@#{cr_npm_version}"
9
+ else
10
+ say "⏩ cable_ready npm package is already present. Skipping."
11
+ end
12
+
13
+ if !lines.index { |line| line =~ /^\s*["']stimulus_reflex["']: ["'].*#{sr_npm_version}["']/ }
14
+ add_package "stimulus_reflex@#{sr_npm_version}"
15
+ else
16
+ say "⏩ stimulus_reflex npm package is already present. Skipping."
17
+ end
18
+
19
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus["']:/ }
20
+ add_package "@hotwired/stimulus@^3.2"
21
+ else
22
+ say "⏩ @hotwired/stimulus npm package is already present. Skipping."
23
+ end
24
+
25
+ complete_step :npm_packages
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ reflexes_path = Rails.root.join("app/reflexes")
6
+ step_path = "/app/reflexes/"
7
+ application_reflex_path = reflexes_path / "application_reflex.rb"
8
+ application_reflex_src = fetch(step_path, "application_reflex.rb.tt")
9
+
10
+ # verify app/reflexes exists and create if necessary
11
+ if reflexes_path.exist?
12
+ say "⏩ app/reflexes directory already exists. Skipping."
13
+ else
14
+ empty_directory reflexes_path
15
+ say "✅ Created app/reflexes directory"
16
+ end
17
+
18
+ if application_reflex_path.exist?
19
+ say "⏩ app/reflexes/application_reflex.rb is alredy present. Skipping."
20
+ else
21
+ copy_file application_reflex_src, application_reflex_path
22
+ say "✅ Created app/reflexes/application_reflex.rb"
23
+ end
24
+
25
+ complete_step :reflexes
@@ -0,0 +1,64 @@
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*["']@hotwired\/stimulus-webpack-helpers["']: ["']\^1.0.1["']/ }
11
+ add_package "@hotwired/stimulus-webpack-helpers@^1.0.1"
12
+ else
13
+ say "⏩ @hotwired/stimulus-webpack-helpers npm package is already present. Skipping."
14
+ end
15
+
16
+ step_path = "/app/javascript/controllers/"
17
+ # controller_templates_path = File.expand_path(template_src + "/app/javascript/controllers", File.join(File.dirname(__FILE__)))
18
+ application_controller_src = fetch(step_path, "application_controller.js.tt")
19
+ application_controller_path = controllers_path / "application_controller.js"
20
+ application_js_src = fetch(step_path, "application.js.tt")
21
+ application_js_path = controllers_path / "application.js"
22
+ index_src = fetch(step_path, "index.js.shakapacker.tt")
23
+ index_path = controllers_path / "index.js"
24
+
25
+ # create entrypoint/controllers, as well as the index, application and application_controller
26
+ empty_directory controllers_path unless controllers_path.exist?
27
+
28
+ copy_file(application_controller_src, application_controller_path) unless application_controller_path.exist?
29
+ copy_file(application_js_src, application_js_path) unless application_js_path.exist?
30
+ copy_file(index_src, index_path) unless index_path.exist?
31
+
32
+ controllers_pattern = /import ['"]controllers['"]/
33
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
34
+
35
+ if pack.match?(controllers_pattern)
36
+ if pack.match?(controllers_commented_pattern)
37
+ proceed = if options.key? "uncomment"
38
+ options["uncomment"]
39
+ else
40
+ !no?("✨ Do you want to import your Stimulus controllers in application.js? (Y/n)")
41
+ end
42
+
43
+ if proceed
44
+ # uncomment_lines only works with Ruby comments 🙄
45
+ lines = pack_path.readlines
46
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
47
+ lines[lines.index(matches.last).to_i] = "import \"controllers\"\n"
48
+ pack_path.write lines.join
49
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
50
+ else
51
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
52
+ end
53
+ else
54
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
55
+ end
56
+ else
57
+ lines = pack_path.readlines
58
+ matches = lines.select { |line| line =~ /^import / }
59
+ lines.insert lines.index(matches.last).to_i + 1, "import \"controllers\"\n"
60
+ pack_path.write lines.join
61
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
62
+ end
63
+
64
+ complete_step :shakapacker