Skip to content

Commit 1c2d21f

Browse files
authored
refactor: rename methods for adding js dependencies to be more generic (#610)
It's not longer a given that `yarn` is being used, and ultimately we're planning to switch to NPM so might as well change the method names now
1 parent 79ac948 commit 1c2d21f

File tree

12 files changed

+31
-38
lines changed

12 files changed

+31
-38
lines changed

template.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -381,22 +381,15 @@ def cleanup_package_json
381381
package_json.manager.install!
382382
end
383383

384-
# Adds the given <code>packages</code> as dependencies using <code>yarn add</code>
384+
# Adds the given JavaScript <code>packages</code> as dependencies using the
385+
# appropriate package manager
385386
#
386387
# @param [Array<String>] packages
387-
def yarn_add_dependencies(packages)
388-
puts "adding #{packages.join(" ")} as dependencies"
388+
# @param [:production, :dev] type
389+
def add_js_dependencies(packages, type: :production)
390+
puts "adding #{packages.join(" ")} as #{type} dependencies"
389391

390-
package_json.manager.add!(packages)
391-
end
392-
393-
# Adds the given <code>packages</code> as devDependencies using <code>yarn add --dev</code>
394-
#
395-
# @param [Array<String>] packages
396-
def yarn_add_dev_dependencies(packages)
397-
puts "adding #{packages.join(" ")} as dev dependencies"
398-
399-
package_json.manager.add!(packages, type: :dev)
392+
package_json.manager.add!(packages, type:)
400393
end
401394

402395
# Add this template directory to source_paths so that Thor actions like

variants/accessibility/template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
apply "spec/system/home_feature_spec.rb"
66
copy_file "spec/support/shared_examples/an_accessible_page.rb"
77

8-
yarn_add_dev_dependencies %w[lighthouse]
8+
add_js_dependencies %w[lighthouse], type: :dev

variants/frontend-base-typescript/template.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def rename_js_file_to_ts(file)
2222
node@18
2323
].map { |name| "@types/#{name}" }
2424

25-
yarn_add_dependencies types_packages + %w[@babel/preset-typescript typescript]
26-
yarn_add_dev_dependencies %w[
25+
add_js_dependencies types_packages + %w[@babel/preset-typescript typescript]
26+
add_js_dependencies %w[
2727
@stylistic/eslint-plugin-ts@3
2828
@typescript-eslint/eslint-plugin
2929
@typescript-eslint/parser
30-
]
30+
], type: :dev
3131

3232
package_json.manager.remove!(["globals"])
3333

variants/frontend-base/js-lint/template.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
add_yarn_package_extension_dependency("eslint-plugin-prettier", "eslint-config-prettier")
55

6-
yarn_add_dev_dependencies %w[
6+
add_js_dependencies %w[
77
@eslint-community/eslint-plugin-eslint-comments
88
@stylistic/eslint-plugin-js@3
99
@eslint/js
@@ -16,7 +16,7 @@
1616
prettier
1717
prettier-config-ackama
1818
prettier-plugin-packagejson
19-
]
19+
], type: :dev
2020

2121
copy_file "variants/frontend-base/eslint.config.js", "eslint.config.js"
2222
template "variants/frontend-base/.prettierignore.tt", ".prettierignore"
@@ -57,12 +57,12 @@
5757
end
5858

5959
# SCSS Linting
60-
yarn_add_dev_dependencies %w[
60+
add_js_dependencies %w[
6161
postcss
6262
stylelint
6363
stylelint-scss
6464
stylelint-config-recommended-scss
65-
]
65+
], type: :dev
6666
copy_file "variants/frontend-base/.stylelintrc.js", ".stylelintrc.js"
6767
template "variants/frontend-base/.stylelintignore.tt", ".stylelintignore"
6868

variants/frontend-base/sentry/template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Setup Sentry
22
# ############
33

4-
yarn_add_dependencies %w[@sentry/browser dotenv-webpack]
4+
add_js_dependencies %w[@sentry/browser dotenv-webpack]
55

66
prepend_to_file! "app/frontend/packs/application.js", "import * as Sentry from '@sentry/browser';"
77

variants/frontend-base/template.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
# ensure the latest major versions of Shakapacker's peer dependencies are installed
2222
#
2323
# TODO: remove this once https://github.com/shakacode/shakapacker/pull/576 is landed
24-
yarn_add_dependencies [
24+
add_js_dependencies [
2525
"babel-loader@10",
2626
"compression-webpack-plugin@11",
2727
"webpack-assets-manifest@6",
2828
"webpack-cli@6",
2929
"webpack-merge@6"
3030
]
31-
yarn_add_dev_dependencies ["webpack-dev-server@5"]
31+
add_js_dependencies ["webpack-dev-server@5"], type: :dev
3232

3333
# this is added by shakapacker:install, but we've already got one (with some extra tags)
3434
# in our template, so remove theirs otherwise the app will error when rendering this
@@ -107,7 +107,7 @@
107107
gsub_file! "app/views/layouts/application.html.erb", "<body>", body_open_tag_with_img_example, force: true
108108

109109
# shakapacker will automatically configure webpack to use these so long as the dependencies are present
110-
yarn_add_dependencies %w[
110+
add_js_dependencies %w[
111111
css-loader
112112
css-minimizer-webpack-plugin
113113
mini-css-extract-plugin
@@ -116,7 +116,7 @@
116116
]
117117

118118
# Setup Turbo
119-
yarn_add_dependencies %w[
119+
add_js_dependencies %w[
120120
@hotwired/turbo-rails
121121
]
122122
prepend_to_file! "app/frontend/packs/application.js" do
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
source_paths.unshift(File.dirname(__FILE__))
22

3-
yarn_add_dependencies %w[@types/bootstrap]
3+
add_js_dependencies %w[@types/bootstrap]
44

55
rename_js_file_to_ts "app/frontend/js/bootstrap"

variants/frontend-bootstrap/template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source_paths.unshift(File.dirname(__FILE__))
22

3-
yarn_add_dependencies(["bootstrap", "@popperjs/core"])
3+
add_js_dependencies(["bootstrap", "@popperjs/core"])
44

55
copy_file "app/frontend/js/bootstrap.js", force: true
66
insert_into_file! "app/frontend/packs/application.js", "import '../js/bootstrap';", before: 'import "../stylesheets/application.scss";'

variants/frontend-react-typescript/template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
source_paths.unshift(File.dirname(__FILE__))
22

33
package_json.manager.remove!(["prop-types"])
4-
yarn_add_dependencies %w[@types/react @types/react-dom]
4+
add_js_dependencies %w[@types/react @types/react-dom]
55

66
rename_js_file_to_ts "app/frontend/packs/server_rendering"
77

variants/frontend-react/template.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
"'@testing-library/dom'",
1515
"'@testing-library/react'"
1616

17-
yarn_add_dependencies %w[
17+
add_js_dependencies %w[
1818
@babel/preset-react
1919
babel-plugin-transform-react-remove-prop-types
2020
react
2121
react-dom
2222
prop-types
2323
]
2424

25-
yarn_add_dev_dependencies %w[
25+
add_js_dependencies %w[
2626
@testing-library/react
2727
eslint-plugin-react
2828
eslint-plugin-react-hooks
2929
eslint-plugin-jsx-a11y
30-
]
30+
], type: :dev
3131
copy_file "eslint.config.js", force: true
3232
copy_file "babel.config.js", force: true
3333

0 commit comments

Comments
 (0)