rolemodel-rails 2.7.1 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -4
  3. data/exe/rmr +6 -0
  4. data/lib/generators/rolemodel/README.md +2 -0
  5. data/lib/generators/rolemodel/agent_docs/README.md +14 -0
  6. data/lib/generators/rolemodel/agent_docs/USAGE +10 -0
  7. data/lib/generators/rolemodel/agent_docs/agent_docs_generator.rb +22 -0
  8. data/lib/generators/rolemodel/agent_docs/templates/.claude/settings.json +15 -0
  9. data/lib/generators/rolemodel/agent_docs/templates/.github/instructions/conventions.instructions.md +9 -0
  10. data/lib/generators/rolemodel/agent_docs/templates/AGENTS.md.tt +26 -0
  11. data/lib/generators/rolemodel/agent_docs/templates/CLAUDE.md +1 -0
  12. data/lib/generators/rolemodel/agent_docs/templates/docs/CONVENTIONS.md +18 -0
  13. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/callbacks.md +5 -0
  14. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/controllers.md +3 -0
  15. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/eager-loading.md +5 -0
  16. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/logic-in-models.md +7 -0
  17. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/migrations.md +7 -0
  18. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/no-sql-strings.md +11 -0
  19. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/partial-locals.md +3 -0
  20. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/reproduce-the-bug.md +5 -0
  21. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/reuse-scopes.md +3 -0
  22. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/stimulus.md +3 -0
  23. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/string-enums.md +3 -0
  24. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/system-spec-assertions.md +17 -0
  25. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/test-layers.md +3 -0
  26. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/views-and-forms.md +9 -0
  27. data/lib/generators/rolemodel/all_generator.rb +2 -0
  28. data/lib/generators/rolemodel/core_setup/README.md +4 -0
  29. data/lib/generators/rolemodel/core_setup/USAGE +4 -4
  30. data/lib/generators/rolemodel/core_setup/core_setup_generator.rb +4 -1
  31. data/lib/generators/rolemodel/dotenv/README.md +110 -0
  32. data/lib/generators/rolemodel/dotenv/USAGE +15 -0
  33. data/lib/generators/rolemodel/dotenv/dotenv_generator.rb +43 -0
  34. data/lib/generators/rolemodel/dotenv/templates/lib/env_value.rb +66 -0
  35. data/lib/generators/rolemodel/dotenv/templates/spec/lib/env_value_spec.rb +178 -0
  36. data/lib/generators/rolemodel/editors/editors_generator.rb +10 -13
  37. data/lib/generators/rolemodel/good_job/good_job_generator.rb +5 -1
  38. data/lib/generators/rolemodel/good_job/templates/config/initializers/good_job.rb +1 -1
  39. data/lib/generators/rolemodel/heroku/heroku_generator.rb +4 -9
  40. data/lib/generators/rolemodel/heroku/templates/deploy-app/SKILL.md +1 -1
  41. data/lib/generators/rolemodel/linters/eslint/eslint_generator.rb +0 -1
  42. data/lib/generators/rolemodel/linters/rubocop/templates/.rubocop.yml +17 -0
  43. data/lib/generators/rolemodel/mailers/mailers_generator.rb +27 -47
  44. data/lib/generators/rolemodel/mailers/templates/app/mailers/staging_mailer_interceptor.rb +2 -3
  45. data/lib/generators/rolemodel/readme/templates/README.md.erb +3 -0
  46. data/lib/generators/rolemodel/saas/devise/devise_generator.rb +3 -23
  47. data/lib/generators/rolemodel/sentry/sentry_generator.rb +1 -0
  48. data/lib/generators/rolemodel/sentry/templates/config/initializers/sentry.rb +1 -1
  49. data/lib/generators/rolemodel/source_map/README.md +1 -1
  50. data/lib/generators/rolemodel/source_map/source_map_generator.rb +8 -4
  51. data/lib/generators/rolemodel/source_map/templates/lib/middleware/rolemodel/source_map.rb +1 -1
  52. data/lib/generators/rolemodel/testing/jasmine_playwright/jasmine_playwright_generator.rb +0 -1
  53. data/lib/generators/rolemodel/testing/vitest/vitest_generator.rb +0 -1
  54. data/lib/rolemodel/engine.rb +2 -0
  55. data/lib/rolemodel/file_content_helper.rb +191 -0
  56. data/lib/rolemodel/generator_base.rb +41 -2
  57. data/lib/rolemodel/new_app/template.rb +7 -0
  58. data/lib/rolemodel/new_app.rb +22 -0
  59. data/lib/rolemodel/version.rb +1 -1
  60. metadata +34 -4
  61. data/lib/generators/rolemodel/editors/vscode_helpers.rb +0 -22
  62. data/lib/rolemodel/replace_content_helper.rb +0 -29
@@ -0,0 +1,191 @@
1
+ require 'prism'
2
+
3
+ module Rolemodel
4
+ module FileContentHelper
5
+ # Finds a block for inject_into_block. `pattern` matches a Prism node by its structure, not its text.
6
+ # `example` is code the pattern matches. Its first line describes the matcher in error messages,
7
+ # and a spec checks that the pattern matches it. Build one with for_class, for_module or for_call.
8
+ BlockMatcher = Data.define(:example, :pattern) do
9
+ # A matcher for a class, by its name ('User', 'Admin::User'), its superclass
10
+ # ('Rails::Application'), or both. A leading :: on either name does not matter.
11
+ def self.for_class(name: nil, superclass: nil)
12
+ parent = constant_argument(superclass)
13
+ definition(Prism::ClassNode, 'class', name, suffix: (" < #{parent}" if parent)) do |node|
14
+ parent.nil? || constant_name(node.superclass) == parent
15
+ end
16
+ end
17
+
18
+ # A matcher for a module, by its name ('Admin', 'Admin::Reports'). A leading :: does not matter.
19
+ def self.for_module(name)
20
+ definition(Prism::ModuleNode, 'module', name)
21
+ end
22
+
23
+ # A matcher for a method call with a do ... end block, given as the names in its chain, such as
24
+ # for_call(:Rails, :application, :configure). The first name may be a constant, such as :Rails or
25
+ # 'Admin::Settings', with or without a leading ::. Without one, the chain must start with a call
26
+ # that has no receiver, as in for_call(:configure). It does not compare arguments.
27
+ def self.for_call(first, *rest)
28
+ names = [constant_argument(first), *rest.map(&:to_s)]
29
+ new(
30
+ example: "#{names.join('.')} do\nend\n",
31
+ pattern: lambda do |node|
32
+ node.is_a?(Prism::CallNode) &&
33
+ node.block.is_a?(Prism::BlockNode) &&
34
+ same_chain?(node, names)
35
+ end
36
+ )
37
+ end
38
+
39
+ # A matcher for a class or module definition: a node of `type` whose name is `name`, when given,
40
+ # and that passes the block, when given.
41
+ def self.definition(type, keyword, name, suffix: nil, &extra)
42
+ name = constant_argument(name)
43
+ new(
44
+ example: "#{keyword} #{name || 'Example'}#{suffix}\nend\n",
45
+ pattern: lambda do |node|
46
+ node.is_a?(type) &&
47
+ (name.nil? || constant_name(node.constant_path) == name) &&
48
+ (extra.nil? || extra.call(node))
49
+ end
50
+ )
51
+ end
52
+
53
+ # A constant name given to a factory, as a String without a leading ::, or nil when not given.
54
+ def self.constant_argument(name)
55
+ name&.to_s&.delete_prefix('::')
56
+ end
57
+
58
+ # A constant's full name without a leading ::, or nil when the node is not a plain constant,
59
+ # such as `Struct.new(:x)` or `foo::Bar`.
60
+ def self.constant_name(node)
61
+ node.full_name.delete_prefix('::') if node.respond_to?(:full_name)
62
+ rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError
63
+ nil
64
+ end
65
+
66
+ # Whether `node` and its receivers are the chain `names`, read from the end: each call's name
67
+ # in turn, then either a constant with the first name or no receiver at all.
68
+ def self.same_chain?(node, names)
69
+ return node.nil? if names.empty?
70
+
71
+ *rest, last = names
72
+ if node.is_a?(Prism::CallNode)
73
+ node.name.to_s == last && same_chain?(node.receiver, rest)
74
+ else
75
+ rest.empty? && constant_name(node) == last
76
+ end
77
+ end
78
+ private_class_method :definition, :constant_argument, :constant_name, :same_chain?
79
+
80
+ def call(node) = pattern.call(node)
81
+ def to_s = example.lines.first.strip
82
+
83
+ # The first node in `source` that the pattern matches, or nil.
84
+ def find_in(source) = Prism.parse(source).value.breadth_first_search { call(it) }
85
+ end
86
+
87
+ private
88
+
89
+ def replace_content(relative_path)
90
+ source = File.expand_path(relative_path.to_s, destination_root)
91
+ content = File.exist?(source) ? File.open(source) { it.binmode.read } : ''
92
+
93
+ remove_file relative_path
94
+ create_file relative_path, yield(content)
95
+ end
96
+
97
+ def modify_json_file(relative_path)
98
+ replace_content(relative_path) do |content|
99
+ # Editor config files such as .vscode/*.json allow comments and trailing commas.
100
+ # Writing the file back drops the comments.
101
+ hash = JSON.parse(
102
+ content.presence || '{}',
103
+ allow_comments: true,
104
+ allow_trailing_comma: true
105
+ )
106
+ yield(hash)
107
+ JSON.pretty_generate(hash) + "\n"
108
+ end
109
+ end
110
+
111
+ def add_package_json_script(command_name, command)
112
+ modify_json_file('package.json') do |hash|
113
+ hash['scripts'] ||= {}
114
+ hash['scripts'][command_name] = command
115
+ hash
116
+ end
117
+ end
118
+
119
+ # Adds code as the last lines of the `Rails.application.configure` block in an environment file,
120
+ # indented 2 spaces, so it overrides the Rails defaults above it.
121
+ # Rails' own `environment(code, env:)` adds it at the top instead.
122
+ def append_to_environment(env, code)
123
+ inject_into_block(
124
+ "config/environments/#{env}.rb",
125
+ BlockMatcher.for_call(:Rails, :application, :configure),
126
+ optimize_indentation(code, 2),
127
+ at: :end
128
+ )
129
+ end
130
+
131
+ # Overrides Thor's inject_into_class, with the same arguments and options. See inject_into_block.
132
+ def inject_into_class(path, klass, *args, &block)
133
+ inject_into_block(path, BlockMatcher.for_class(name: klass), *args, &block)
134
+ end
135
+
136
+ # Overrides Thor's inject_into_module, with the same arguments and options. See inject_into_block.
137
+ def inject_into_module(path, module_name, *args, &block)
138
+ inject_into_block(path, BlockMatcher.for_module(module_name), *args, &block)
139
+ end
140
+
141
+ # Adds data inside the first block in a file that `matcher`, a BlockMatcher, accepts. A block is any
142
+ # code that closes with `end`: a class, module, method, or a do ... end block.
143
+ #
144
+ # Works like Thor's inject_into_class: data as a string or a block, the :verbose and :force options,
145
+ # and no change to the data's indentation. It adds one option: at: :start (the default) adds the data
146
+ # right after the block's opening line, and at: :end adds it just before the block's `end`.
147
+ #
148
+ # Prism finds the block, so spacing, comments and code around it do not change where the data goes,
149
+ # and it raises when the block is missing, where Thor prints "unchanged" and goes on. Thor's
150
+ # insert_into_file still writes the data, so it skips data the file already has, honors --pretend,
151
+ # and `rails destroy` can remove it.
152
+ def inject_into_block(path, matcher, *args, &block)
153
+ config = args.extract_options!
154
+ at = config.delete(:at) || :start
155
+ unless %i[start end].include?(at)
156
+ raise ArgumentError, "at: must be :start or :end, not #{at.inspect}"
157
+ end
158
+
159
+ content = File.read(File.join(destination_root, path))
160
+ opening_line, closing = block_lines(matcher.find_in(content))
161
+ unless closing
162
+ raise Thor::Error, "Could not find `#{matcher}` with `end` on its own line in #{path}"
163
+ end
164
+
165
+ lines_before = content.lines.first(at == :start ? opening_line : closing.start_line - 1).join
166
+ config[:after] = /\A#{Regexp.escape(lines_before)}/
167
+ insert_into_file(path, *(args << config), &block)
168
+ end
169
+
170
+ # For a block, the last line of its opening (such as `class User < ApplicationRecord`, or
171
+ # `configure do |config|` when the opening wraps), and the location of the `end` that closes it.
172
+ # Returns nil unless the `end` keyword starts its own line. A `{ }` block has no `end`.
173
+ def block_lines(node)
174
+ opening, closing = case node
175
+ in Prism::ClassNode
176
+ [node.superclass || node.constant_path, node.end_keyword_loc]
177
+ in Prism::ModuleNode
178
+ [node.constant_path, node.end_keyword_loc]
179
+ in Prism::DefNode
180
+ [node.rparen_loc || node.name_loc, node.end_keyword_loc]
181
+ in Prism::CallNode[block: Prism::BlockNode => block]
182
+ [block.parameters || block.opening_loc, block.closing_loc]
183
+ else return
184
+ end
185
+ return unless closing.slice == 'end' && closing.start_line_slice.strip.empty?
186
+
187
+ opening = opening.location if opening.respond_to?(:location)
188
+ [opening.end_line, closing]
189
+ end
190
+ end
191
+ end
@@ -2,13 +2,13 @@
2
2
 
3
3
  require 'rails/generators'
4
4
  require 'rails/generators/bundle_helper'
5
- require_relative 'replace_content_helper'
5
+ require_relative 'file_content_helper'
6
6
  require_relative 'skills_helper'
7
7
  require 'rolemodel/yarn'
8
8
 
9
9
  module Rolemodel
10
10
  class GeneratorBase < ::Rails::Generators::Base
11
- include ::Rails::Generators::BundleHelper, ReplaceContentHelper, SkillsHelper
11
+ include ::Rails::Generators::BundleHelper, FileContentHelper, SkillsHelper
12
12
 
13
13
  private
14
14
 
@@ -38,6 +38,45 @@ module Rolemodel
38
38
  invoke 'rolemodel:yarn:setup', [], options.slice(:pretend)
39
39
  end
40
40
 
41
+ # Adds a setting that reads ENV through EnvValue as the last line of `class Application`
42
+ # in config/application.rb, for example
43
+ # add_setting("config.x.sentry.dsn = EnvValue.string('SENTRY_DSN')").
44
+ # Runs rolemodel:dotenv first when the app has no EnvValue yet.
45
+ def add_setting(line)
46
+ ensure_env_value
47
+ inject_into_class 'config/application.rb', 'Application', optimize_indentation(line, 4), at: :end
48
+ end
49
+
50
+ # Sets every URL built outside a request, such as links and assets in emails, from DEFAULT_URL.
51
+ # Heroku apps with no DEFAULT_URL, such as review apps, use their herokuapp.com address,
52
+ # and local apps use http://localhost:3000. Safe to call from several generators.
53
+ def add_default_url
54
+ # Rails splits a full URL given as host: into its protocol, host and port.
55
+ add_setting <<~'RUBY'
56
+ # Links in emails, and URLs built outside a request, use DEFAULT_URL.
57
+ config.x.default_url = EnvValue.url('DEFAULT_URL') do
58
+ heroku_app = EnvValue.string('HEROKU_APP_NAME')
59
+ heroku_app ? "https://#{heroku_app}.herokuapp.com" : 'http://localhost:3000'
60
+ end
61
+ config.action_mailer.default_url_options = { host: config.x.default_url }
62
+ config.action_mailer.asset_host = config.x.default_url
63
+ Rails.application.routes.default_url_options = config.action_mailer.default_url_options
64
+ RUBY
65
+
66
+ # New Rails apps set their own default_url_options in development.rb and production.rb, and
67
+ # those files run after application.rb, so they would win over DEFAULT_URL. Comment them out.
68
+ # test.rb keeps its own, so tests still build URLs for example.com.
69
+ %w[development production].each do |env|
70
+ comment_lines "config/environments/#{env}.rb", /config\.action_mailer\.default_url_options\s*=/
71
+ end
72
+ end
73
+
74
+ def ensure_env_value
75
+ return if File.exist?(File.join(destination_root, 'lib/env_value.rb'))
76
+
77
+ invoke 'rolemodel:dotenv', [], options.slice(:pretend)
78
+ end
79
+
41
80
  def application_stylesheet_path
42
81
  Dir.glob('app/assets/stylesheets/application.*').first
43
82
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ gem 'rolemodel-rails'
4
+
5
+ after_bundle do
6
+ rails_command 'generate rolemodel:core_setup'
7
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/command'
4
+ require 'rails/commands/application/application_command'
5
+
6
+ module Rolemodel
7
+ module NewApp
8
+ DEFAULTS = %W[
9
+ --database=postgresql
10
+ --javascript=webpack
11
+ --skip-kamal
12
+ --skip-solid
13
+ --skip-test
14
+ --template=#{File.expand_path('new_app/template.rb', __dir__)}
15
+ ].freeze
16
+
17
+ def self.argv(argv)
18
+ scrubbed = Rails::Generators::ARGVScrubber.new(argv).prepare!
19
+ scrubbed.take(1) + DEFAULTS + scrubbed.drop(1)
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rolemodel
4
- VERSION = '2.7.1'
4
+ VERSION = '2.9.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rolemodel-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.1
4
+ version: 2.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - RoleModel Software Inc
@@ -181,23 +181,51 @@ description: A collection of executable best practices for common aspects of Rai
181
181
  application development.
182
182
  email:
183
183
  - it-support@rolemodelsoftware.com
184
- executables: []
184
+ executables:
185
+ - rmr
185
186
  extensions: []
186
187
  extra_rdoc_files: []
187
188
  files:
188
189
  - LICENSE.txt
189
190
  - README.md
190
191
  - Rakefile
192
+ - exe/rmr
191
193
  - lib/generators/rolemodel/README.md
192
194
  - lib/generators/rolemodel/USAGE
195
+ - lib/generators/rolemodel/agent_docs/README.md
196
+ - lib/generators/rolemodel/agent_docs/USAGE
197
+ - lib/generators/rolemodel/agent_docs/agent_docs_generator.rb
198
+ - lib/generators/rolemodel/agent_docs/templates/.claude/settings.json
199
+ - lib/generators/rolemodel/agent_docs/templates/.github/instructions/conventions.instructions.md
200
+ - lib/generators/rolemodel/agent_docs/templates/AGENTS.md.tt
201
+ - lib/generators/rolemodel/agent_docs/templates/CLAUDE.md
202
+ - lib/generators/rolemodel/agent_docs/templates/docs/CONVENTIONS.md
203
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/callbacks.md
204
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/controllers.md
205
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/eager-loading.md
206
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/logic-in-models.md
207
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/migrations.md
208
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/no-sql-strings.md
209
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/partial-locals.md
210
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/reproduce-the-bug.md
211
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/reuse-scopes.md
212
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/stimulus.md
213
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/string-enums.md
214
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/system-spec-assertions.md
215
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/test-layers.md
216
+ - lib/generators/rolemodel/agent_docs/templates/docs/conventions/views-and-forms.md
193
217
  - lib/generators/rolemodel/all_generator.rb
194
218
  - lib/generators/rolemodel/core_setup/README.md
195
219
  - lib/generators/rolemodel/core_setup/USAGE
196
220
  - lib/generators/rolemodel/core_setup/core_setup_generator.rb
221
+ - lib/generators/rolemodel/dotenv/README.md
222
+ - lib/generators/rolemodel/dotenv/USAGE
223
+ - lib/generators/rolemodel/dotenv/dotenv_generator.rb
224
+ - lib/generators/rolemodel/dotenv/templates/lib/env_value.rb
225
+ - lib/generators/rolemodel/dotenv/templates/spec/lib/env_value_spec.rb
197
226
  - lib/generators/rolemodel/editors/README.md
198
227
  - lib/generators/rolemodel/editors/USAGE
199
228
  - lib/generators/rolemodel/editors/editors_generator.rb
200
- - lib/generators/rolemodel/editors/vscode_helpers.rb
201
229
  - lib/generators/rolemodel/github/README.md
202
230
  - lib/generators/rolemodel/github/USAGE
203
231
  - lib/generators/rolemodel/github/github_generator.rb
@@ -449,7 +477,10 @@ files:
449
477
  - lib/generators/rolemodel/webpack/webpack_generator.rb
450
478
  - lib/rolemodel-rails.rb
451
479
  - lib/rolemodel/engine.rb
480
+ - lib/rolemodel/file_content_helper.rb
452
481
  - lib/rolemodel/generator_base.rb
482
+ - lib/rolemodel/new_app.rb
483
+ - lib/rolemodel/new_app/template.rb
453
484
  - lib/rolemodel/optics.rb
454
485
  - lib/rolemodel/optics/custom_icon_builder.rb
455
486
  - lib/rolemodel/optics/feather_icon_builder.rb
@@ -458,7 +489,6 @@ files:
458
489
  - lib/rolemodel/optics/material_icon_builder.rb
459
490
  - lib/rolemodel/optics/phosphor_icon_builder.rb
460
491
  - lib/rolemodel/optics/tabler_icon_builder.rb
461
- - lib/rolemodel/replace_content_helper.rb
462
492
  - lib/rolemodel/resource_for/controller_extension.rb
463
493
  - lib/rolemodel/skills_helper.rb
464
494
  - lib/rolemodel/utility.rb
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rolemodel
4
- module VSCodeHelpers
5
- private
6
-
7
- # Create an extensions file for VSCode and provide an empty
8
- # recommendations array if it doesn't exist.
9
- def ensure_extensions_config
10
- return if File.exist?('.vscode/extensions.json')
11
-
12
- create_file('.vscode/extensions.json') do |file|
13
- <<~JSON
14
- {
15
- "recommendations": [
16
- ]
17
- }
18
- JSON
19
- end
20
- end
21
- end
22
- end
@@ -1,29 +0,0 @@
1
- module Rolemodel
2
- module ReplaceContentHelper
3
- private
4
-
5
- def replace_content(relative_path, &block)
6
- source = File.expand_path(relative_path.to_s, destination_root)
7
- content = File.exist?(source) ? File.open(source) { it.binmode.read } : ''
8
-
9
- remove_file relative_path
10
- create_file relative_path, yield(content)
11
- end
12
-
13
- def modify_json_file(relative_path, &block)
14
- replace_content(relative_path) do |content|
15
- hash = JSON.parse(content.presence || '{}')
16
- yield(hash)
17
- JSON.pretty_generate(hash) + "\n"
18
- end
19
- end
20
-
21
- def add_package_json_script(command_name, command)
22
- modify_json_file('package.json') do |hash|
23
- hash['scripts'] ||= {}
24
- hash['scripts'][command_name] = command
25
- hash
26
- end
27
- end
28
- end
29
- end