Skip to content

Commit 1c237f5

Browse files
committed
feat: use npm for managing js packages
1 parent f9be057 commit 1c237f5

File tree

19 files changed

+72
-155
lines changed

19 files changed

+72
-155
lines changed

.github/workflows/ci.yml

+6-28
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ jobs:
4949
- uses: actions/setup-node@v4
5050
with:
5151
node-version-file: '.node-version'
52-
cache: 'yarn'
53-
- run: yarn install
54-
- run: yarn run format-check
52+
cache: 'npm'
53+
- run: npm ci
54+
- run: npm run format-check
5555

5656
rubocop:
5757
permissions:
@@ -79,24 +79,6 @@ jobs:
7979
# how many fail)
8080
fail-fast: false
8181
matrix:
82-
js_package_manager:
83-
- name: npm
84-
installer: npm
85-
- name: yarn_berry
86-
installer: yarn
87-
linker: pnp
88-
- name: yarn_berry
89-
installer: yarn
90-
linker: node-modules
91-
- name: yarn_berry
92-
installer: yarn
93-
linker: pnpm
94-
- name: yarn_classic
95-
installer: yarn
96-
- name: pnpm
97-
installer: pnpm
98-
- name: bun
99-
installer: bun
10082
variant:
10183
- name: defaults
10284
config_path: 'ackama_rails_template.config.yml'
@@ -173,8 +155,8 @@ jobs:
173155
with:
174156
node-version-file: '.node-version'
175157

176-
- name: install package manager
177-
run: npm i -g ${{ matrix.js_package_manager.installer }}
158+
- name: Install latest version of npm
159+
run: npm i -g npm
178160

179161
# We don't cache gems or JS packages because we are actually testing how
180162
# installation and setup works in this project so, while caching would
@@ -192,8 +174,7 @@ jobs:
192174
git config --global user.email "you@example.com"
193175
git config --global user.name "Your Name"
194176
195-
# prettier-ignore
196-
- run: ./ci/bin/create-fake-js-package-managers ${{ matrix.js_package_manager.installer }}
177+
- run: ./ci/bin/create-fake-js-package-managers npm
197178

198179
- name: Run CI script
199180
env:
@@ -207,7 +188,4 @@ jobs:
207188
PGUSER: postgres
208189
PGPASSWORD: postgres
209190
PGHOST: localhost
210-
PACKAGE_JSON_FALLBACK_MANAGER: ${{ matrix.js_package_manager.name }}
211-
PACKAGE_JSON_YARN_BERRY_LINKER:
212-
${{ matrix.js_package_manager.linker }}
213191
run: ./ci/bin/build-and-test

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Where possible we stick to Rails defaults.
3333

3434
- General
3535
- [puma](https://github.com/puma/puma) as application server
36-
- [Yarn](https://yarnpkg.com/) for managing JS packages
36+
- [npm](https://www.npmjs.com/) for managing JS packages
3737
- PostgreSQL as database. This template only supports PostgreSQL.
3838
- A much-improved `bin/setup` script
3939
- Install [dotenv](https://github.com/bkeepers/dotenv)
@@ -155,7 +155,7 @@ our other templates:
155155
Before running this template, you must have the following installed on your
156156
machine:
157157

158-
- Yarn v1.21.0 or later
158+
- NPM v10.0.0 or later
159159
- Rails 7.1.x
160160

161161
The following are not strictly required to run the template but you will need it

package-lock.json

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

template.rb

+5-69
Original file line numberDiff line numberDiff line change
@@ -264,67 +264,7 @@ def apply_prettier_all_over
264264
git commit: ". -m 'Run prettier one last time'"
265265
end
266266

267-
# Sets Yarn Berry up in the project directory by initializing it with what is probably Yarn Classic.
268-
#
269-
# This is required as the Berry binary is actually downloaded and committed to the codebase, and
270-
# the global yarn command passes through to it when detected (even if its Yarn Classic).
271-
#
272-
# This also requires us to temporarily create a package.json as otherwise Yarn Berry will
273-
# look up the file tree and initialize itself in every directory that has a yarn.lock
274-
def setup_yarn_berry
275-
# safeguard against parent directories having a yarn.lock
276-
File.write("package.json", "{}") unless File.exist?("package.json")
277-
278-
run "yarn init -2"
279-
run "yarn config set enableGlobalCache true"
280-
run "yarn config set nodeLinker #{ENV.fetch("PACKAGE_JSON_YARN_BERRY_LINKER", "node-modules")}"
281-
282-
ignores = <<~YARN
283-
.pnp.*
284-
.yarn/*
285-
!.yarn/patches
286-
!.yarn/plugins
287-
!.yarn/releases
288-
!.yarn/sdks
289-
!.yarn/versions
290-
YARN
291-
File.write(".gitignore", ignores, mode: "a")
292-
293-
# this will be properly (re)created later
294-
File.unlink("package.json")
295-
end
296-
297-
# Bun uses a binary-based lockfile which cannot be parsed by shakapacker or
298-
# osv-detector, so we want to configure bun to always write a yarn.lock
299-
# in addition so that such tools can check it
300-
def setup_bun
301-
File.write("bunfig.toml", <<~TOML)
302-
[install.lockfile]
303-
print = "yarn"
304-
TOML
305-
end
306-
307-
def add_yarn_package_extension_dependency(name, dependency)
308-
return unless File.exist?(".yarnrc.yml")
309-
310-
require "yaml"
311-
312-
yarnrc = YAML.load_file(".yarnrc.yml")
313-
314-
yarnrc["packageExtensions"] ||= {}
315-
yarnrc["packageExtensions"]["#{name}@*"] ||= {}
316-
yarnrc["packageExtensions"]["#{name}@*"]["dependencies"] ||= {}
317-
yarnrc["packageExtensions"]["#{name}@*"]["dependencies"][dependency] = "*"
318-
319-
File.write(".yarnrc.yml", yarnrc.to_yaml)
320-
end
321-
322267
def package_json
323-
if @package_json.nil?
324-
setup_yarn_berry if ENV.fetch("PACKAGE_JSON_FALLBACK_MANAGER", nil) == "yarn_berry"
325-
setup_bun if ENV.fetch("PACKAGE_JSON_FALLBACK_MANAGER", nil) == "bun"
326-
end
327-
328268
@package_json ||= PackageJson.new
329269
end
330270

@@ -346,8 +286,7 @@ def build_engines_field(existing)
346286
node_version = File.read("./.node-version").strip
347287

348288
existing.merge({
349-
"node" => "^#{node_version}",
350-
"yarn" => "^1.0.0"
289+
"node" => "^#{node_version}"
351290
})
352291
end
353292

@@ -361,26 +300,23 @@ def cleanup_package_json
361300
}
362301
end
363302

364-
# TODO: this doesn't work when using pnpm even though it shouldn't matter? anyway, replace with 'exec' support
365-
# run "npx -y sort-package-json"
366-
367303
# ensure the lockfile is up to date with any changes we've made to package.json
368304
package_json.manager.install!
369305
end
370306

371-
# Adds the given <code>packages</code> as dependencies using <code>yarn add</code>
307+
# Adds the given <code>packages</code> as dependencies using the preferred package manager
372308
#
373309
# @param [Array<String>] packages
374-
def yarn_add_dependencies(packages)
310+
def add_js_dependencies(packages)
375311
puts "adding #{packages.join(" ")} as dependencies"
376312

377313
package_json.manager.add!(packages)
378314
end
379315

380-
# Adds the given <code>packages</code> as devDependencies using <code>yarn add --dev</code>
316+
# Adds the given <code>packages</code> as devDependencies using the preferred package manager
381317
#
382318
# @param [Array<String>] packages
383-
def yarn_add_dev_dependencies(packages)
319+
def add_js_dev_dependencies(packages)
384320
puts "adding #{packages.join(" ")} as dev dependencies"
385321

386322
package_json.manager.add!(packages, type: :dev)

variants/accessibility/spec/rails_helper.rb

-8
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,3 @@
1212
Lighthouse::Matchers.chrome_flags = %w[headless=new no-sandbox]
1313
OPTIONS
1414
end
15-
16-
if File.exist?(".yarnrc.yml")
17-
insert_into_file "spec/rails_helper.rb", after: /# Lighthouse Matcher options\n/ do
18-
<<~OPTIONS
19-
Lighthouse::Matchers.lighthouse_cli = "yarn run lighthouse"
20-
OPTIONS
21-
end
22-
end

variants/accessibility/template.rb

+1-1
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_dev_dependencies %w[lighthouse]

variants/backend-base/Dockerfile

+2-5
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ RUN apt-get update -qq &&\
3939
/usr/share/locales \
4040
/var/cache/apt/archives
4141

42-
# Use NPM to install Yarn.
43-
RUN npm install -g yarn
44-
4542
# Install gem and NPM dependencies. These are baked into the image so the image can run
4643
# standalone provided valid configuration. When running in docker-compose, these
4744
# dependencies are stored in a volume so the image does not need rebuilding when the
@@ -56,10 +53,10 @@ USER deploy
5653

5754
# Add just the dependency manifests before installing.
5855
# This reduces the chance that bundler or NPM will get a cold cache because some kind of application file changed.
59-
ADD Gemfile* package.json yarn.lock /usr/src/app/
56+
ADD Gemfile* package.json package-lock.json /usr/src/app/
6057
WORKDIR /usr/src/app
6158
RUN bundle check || bundle install &&\
62-
yarn install --frozen-lockfile
59+
npm ci
6360

6461
# Add all application code to /usr/src/app and set this as the working directory
6562
# of the container

variants/frontend-base-typescript/template.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ 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_dev_dependencies %w[
2727
@typescript-eslint/eslint-plugin@7
2828
@typescript-eslint/parser@7
2929
]

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# Javascript code linting and formatting
22
# ######################################
33

4-
add_yarn_package_extension_dependency("eslint-plugin-prettier", "eslint-config-prettier")
5-
6-
yarn_add_dev_dependencies %w[
4+
add_js_dev_dependencies %w[
75
eslint@8
86
eslint-config-ackama
97
eslint-plugin-node
@@ -54,7 +52,7 @@
5452
end
5553

5654
# SCSS Linting
57-
yarn_add_dev_dependencies %w[
55+
add_js_dev_dependencies %w[
5856
postcss
5957
stylelint
6058
stylelint-scss

variants/frontend-base/sentry/template.rb

+1-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
gsub_file! "app/views/layouts/application.html.erb", "<body>", body_open_tag_with_img_example, force: true
9696

9797
# shakapacker will automatically configure webpack to use these so long as the dependencies are present
98-
yarn_add_dependencies %w[
98+
add_js_dependencies %w[
9999
css-loader
100100
css-minimizer-webpack-plugin
101101
mini-css-extract-plugin
@@ -104,7 +104,7 @@
104104
]
105105

106106
# Setup Turbo
107-
yarn_add_dependencies %w[
107+
add_js_dependencies %w[
108108
@hotwired/turbo-rails
109109
]
110110
prepend_to_file "app/frontend/packs/application.js" do
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

+1-1
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

+1-1
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

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77

88
run "rails generate react:install"
99

10-
add_yarn_package_extension_dependency("@testing-library/user-event", "@testing-library/dom")
11-
1210
# prefer importing from the more specific package for consistency
1311
gsub_file! "app/frontend/test/stimulus/controllers/add_class_controller.test.js",
1412
"'@testing-library/dom'",
1513
"'@testing-library/react'"
1614

17-
yarn_add_dependencies %w[
15+
add_js_dependencies %w[
1816
@babel/preset-react
1917
babel-plugin-transform-react-remove-prop-types
2018
react
2119
react-dom
2220
prop-types
2321
]
2422

25-
yarn_add_dev_dependencies %w[
23+
add_js_dev_dependencies %w[
2624
@testing-library/react
2725
eslint-plugin-react
2826
eslint-plugin-react-hooks

variants/frontend-stimulus-typescript/template.rb

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
source_paths.unshift(File.dirname(__FILE__))
22

3-
add_yarn_package_extension_dependency("@testing-library/jest-dom", "@types/aria-query")
4-
add_yarn_package_extension_dependency("ts-jest", "@jest/transform")
5-
add_yarn_package_extension_dependency("ts-jest", "@jest/types")
6-
add_yarn_package_extension_dependency("@jest/test-result", "jest-haste-map")
7-
add_yarn_package_extension_dependency("@jest/test-result", "jest-resolve")
8-
add_yarn_package_extension_dependency("jest-cli", "@types/yargs")
9-
10-
yarn_add_dependencies ["@babel/plugin-transform-typescript"]
11-
yarn_add_dev_dependencies [
3+
add_js_dependencies ["@babel/plugin-transform-typescript"]
4+
add_js_dev_dependencies [
125
"@types/jest@#{JEST_MAJOR_VERSION}",
136
"ts-jest@#{JEST_MAJOR_VERSION}",
147
"ts-node"

0 commit comments

Comments
 (0)