Skip to content

Commit 49a0581

Browse files
committed
feat: replace --engines-strict check with arborist query
Closes #149
1 parent 26495f3 commit 49a0581

File tree

10 files changed

+139
-421
lines changed

10 files changed

+139
-421
lines changed

.github/workflows/ci-test-workspace.yml

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,6 @@ on:
1818
- cron: "0 9 * * 1"
1919

2020
jobs:
21-
engines:
22-
name: Engines - ${{ matrix.platform.name }} - ${{ matrix.node-version }}
23-
if: github.repository_owner == 'npm'
24-
strategy:
25-
fail-fast: false
26-
matrix:
27-
platform:
28-
- name: Linux
29-
os: ubuntu-latest
30-
shell: bash
31-
node-version:
32-
- 14.17.0
33-
- 16.13.0
34-
- 18.0.0
35-
runs-on: ${{ matrix.platform.os }}
36-
defaults:
37-
run:
38-
shell: ${{ matrix.platform.shell }}
39-
steps:
40-
- name: Checkout
41-
uses: actions/checkout@v3
42-
- name: Setup Git User
43-
run: |
44-
git config --global user.email "[email protected]"
45-
git config --global user.name "npm CLI robot"
46-
- name: Setup Node
47-
uses: actions/setup-node@v3
48-
with:
49-
node-version: ${{ matrix.node-version }}
50-
- name: Update Windows npm
51-
# node 12 and 14 ship with npm@6, which is known to fail when updating itself in windows
52-
if: matrix.platform.os == 'windows-latest' && (startsWith(matrix.node-version, '12.') || startsWith(matrix.node-version, '14.'))
53-
run: |
54-
curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz
55-
tar xf npm-7.5.4.tgz
56-
cd package
57-
node lib/npm.js install --no-fund --no-audit -g ..\npm-7.5.4.tgz
58-
cd ..
59-
rmdir /s /q package
60-
- name: Install npm@7
61-
if: startsWith(matrix.node-version, '10.')
62-
run: npm i --prefer-online --no-fund --no-audit -g npm@7
63-
- name: Install npm@latest
64-
if: ${{ !startsWith(matrix.node-version, '10.') }}
65-
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
66-
- name: npm Version
67-
run: npm -v
68-
- name: Install Dependencies
69-
run: npm i --ignore-scripts --no-audit --no-fund --engines-strict
70-
7121
lint:
7222
name: Lint
7323
if: github.repository_owner == 'npm'

.github/workflows/ci.yml

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,6 @@ on:
1818
- cron: "0 9 * * 1"
1919

2020
jobs:
21-
engines:
22-
name: Engines - ${{ matrix.platform.name }} - ${{ matrix.node-version }}
23-
if: github.repository_owner == 'npm'
24-
strategy:
25-
fail-fast: false
26-
matrix:
27-
platform:
28-
- name: Linux
29-
os: ubuntu-latest
30-
shell: bash
31-
node-version:
32-
- 14.17.0
33-
- 16.13.0
34-
- 18.0.0
35-
runs-on: ${{ matrix.platform.os }}
36-
defaults:
37-
run:
38-
shell: ${{ matrix.platform.shell }}
39-
steps:
40-
- name: Checkout
41-
uses: actions/checkout@v3
42-
- name: Setup Git User
43-
run: |
44-
git config --global user.email "[email protected]"
45-
git config --global user.name "npm CLI robot"
46-
- name: Setup Node
47-
uses: actions/setup-node@v3
48-
with:
49-
node-version: ${{ matrix.node-version }}
50-
- name: Update Windows npm
51-
# node 12 and 14 ship with npm@6, which is known to fail when updating itself in windows
52-
if: matrix.platform.os == 'windows-latest' && (startsWith(matrix.node-version, '12.') || startsWith(matrix.node-version, '14.'))
53-
run: |
54-
curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz
55-
tar xf npm-7.5.4.tgz
56-
cd package
57-
node lib/npm.js install --no-fund --no-audit -g ..\npm-7.5.4.tgz
58-
cd ..
59-
rmdir /s /q package
60-
- name: Install npm@7
61-
if: startsWith(matrix.node-version, '10.')
62-
run: npm i --prefer-online --no-fund --no-audit -g npm@7
63-
- name: Install npm@latest
64-
if: ${{ !startsWith(matrix.node-version, '10.') }}
65-
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
66-
- name: npm Version
67-
run: npm -v
68-
- name: Install Dependencies
69-
run: npm i --ignore-scripts --no-audit --no-fund --engines-strict
70-
7121
lint:
7222
name: Lint
7323
if: github.repository_owner == 'npm'

lib/check/check-engines.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const semver = require('semver')
2+
const { relative, join } = require('path')
3+
const Arborist = require('@npmcli/arborist')
4+
5+
const run = async ({ root, path, pkg, config: { omitEngines = [] } }) => {
6+
const pkgPath = join(relative(root, path), 'package.json')
7+
const arb = new Arborist({ path })
8+
const tree = await arb.loadActual({ forceActual: true })
9+
10+
const engines = pkg.engines.node
11+
const deps = await tree.querySelectorAll(`#${pkg.name} > .prod:attr(engines, [node])`)
12+
13+
const invalid = []
14+
for (const dep of deps) {
15+
if (omitEngines.includes(dep.name)) {
16+
continue
17+
}
18+
19+
const depEngines = dep.target.package.engines.node
20+
if (!semver.subset(engines, depEngines)) {
21+
invalid.push({
22+
name: `${dep.name}@${dep.version}`,
23+
location: dep.location,
24+
engines: depEngines,
25+
})
26+
}
27+
}
28+
29+
if (invalid.length) {
30+
const title = `The following production dependencies are not compatible with ` +
31+
`\`engines.node: ${engines}\` found in \`${pkgPath}\`:`
32+
return {
33+
title,
34+
body: invalid.map((dep) => [
35+
`${dep.name}:`,
36+
` engines.node: ${dep.engines}`,
37+
` location: ${dep.location}`,
38+
].join('\n')).join('\n'),
39+
solution: 'Remove them or move them to devDependencies.',
40+
}
41+
}
42+
}
43+
44+
module.exports = {
45+
run,
46+
when: ({ pkg, config: c }) => c.applyModule && pkg.engines?.node,
47+
name: 'check-engines',
48+
}

lib/check/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ module.exports = (root) => run(root, [
66
require('./check-unwanted.js'),
77
require('./check-gitignore.js'),
88
require('./check-changelog.js'),
9+
require('./check-engines.js'),
910
])

lib/content/ci.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ on:
44
{{> onCi }}
55

66
jobs:
7-
engines:
8-
{{> jobMatrix
9-
jobName="Engines"
10-
jobDepFlags="--engines-strict"
11-
macCI=false
12-
windowsCI=false
13-
ciVersions=baseCiVersions
14-
}}
15-
167
lint:
178
{{> job jobName="Lint" }}
189
{{> stepLint jobRunFlags=pkgFlags }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@commitlint/cli": "^17.1.1",
3737
"@commitlint/config-conventional": "^17.1.0",
3838
"@isaacs/string-locale-compare": "^1.1.0",
39+
"@npmcli/arborist": "^6.0.0",
3940
"@npmcli/git": "^4.0.0",
4041
"@npmcli/map-workspaces": "^3.0.0",
4142
"@npmcli/package-json": "^3.0.0",

0 commit comments

Comments
 (0)