Skip to content

Commit

Permalink
feat: replace --engines-strict check with arborist query
Browse files Browse the repository at this point in the history
Closes #149
  • Loading branch information
lukekarrys committed Oct 27, 2022
1 parent 26495f3 commit 49a0581
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 421 deletions.
50 changes: 0 additions & 50 deletions .github/workflows/ci-test-workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,6 @@ on:
- cron: "0 9 * * 1"

jobs:
engines:
name: Engines - ${{ matrix.platform.name }} - ${{ matrix.node-version }}
if: github.repository_owner == 'npm'
strategy:
fail-fast: false
matrix:
platform:
- name: Linux
os: ubuntu-latest
shell: bash
node-version:
- 14.17.0
- 16.13.0
- 18.0.0
runs-on: ${{ matrix.platform.os }}
defaults:
run:
shell: ${{ matrix.platform.shell }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Git User
run: |
git config --global user.email "npm-cli+bot@github.com"
git config --global user.name "npm CLI robot"
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Update Windows npm
# node 12 and 14 ship with npm@6, which is known to fail when updating itself in windows
if: matrix.platform.os == 'windows-latest' && (startsWith(matrix.node-version, '12.') || startsWith(matrix.node-version, '14.'))
run: |
curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz
tar xf npm-7.5.4.tgz
cd package
node lib/npm.js install --no-fund --no-audit -g ..\npm-7.5.4.tgz
cd ..
rmdir /s /q package
- name: Install npm@7
if: startsWith(matrix.node-version, '10.')
run: npm i --prefer-online --no-fund --no-audit -g npm@7
- name: Install npm@latest
if: ${{ !startsWith(matrix.node-version, '10.') }}
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
- name: npm Version
run: npm -v
- name: Install Dependencies
run: npm i --ignore-scripts --no-audit --no-fund --engines-strict

lint:
name: Lint
if: github.repository_owner == 'npm'
Expand Down
50 changes: 0 additions & 50 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,6 @@ on:
- cron: "0 9 * * 1"

jobs:
engines:
name: Engines - ${{ matrix.platform.name }} - ${{ matrix.node-version }}
if: github.repository_owner == 'npm'
strategy:
fail-fast: false
matrix:
platform:
- name: Linux
os: ubuntu-latest
shell: bash
node-version:
- 14.17.0
- 16.13.0
- 18.0.0
runs-on: ${{ matrix.platform.os }}
defaults:
run:
shell: ${{ matrix.platform.shell }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Git User
run: |
git config --global user.email "npm-cli+bot@github.com"
git config --global user.name "npm CLI robot"
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Update Windows npm
# node 12 and 14 ship with npm@6, which is known to fail when updating itself in windows
if: matrix.platform.os == 'windows-latest' && (startsWith(matrix.node-version, '12.') || startsWith(matrix.node-version, '14.'))
run: |
curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz
tar xf npm-7.5.4.tgz
cd package
node lib/npm.js install --no-fund --no-audit -g ..\npm-7.5.4.tgz
cd ..
rmdir /s /q package
- name: Install npm@7
if: startsWith(matrix.node-version, '10.')
run: npm i --prefer-online --no-fund --no-audit -g npm@7
- name: Install npm@latest
if: ${{ !startsWith(matrix.node-version, '10.') }}
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
- name: npm Version
run: npm -v
- name: Install Dependencies
run: npm i --ignore-scripts --no-audit --no-fund --engines-strict

lint:
name: Lint
if: github.repository_owner == 'npm'
Expand Down
48 changes: 48 additions & 0 deletions lib/check/check-engines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const semver = require('semver')
const { relative, join } = require('path')
const Arborist = require('@npmcli/arborist')

const run = async ({ root, path, pkg, config: { omitEngines = [] } }) => {
const pkgPath = join(relative(root, path), 'package.json')
const arb = new Arborist({ path })
const tree = await arb.loadActual({ forceActual: true })

const engines = pkg.engines.node
const deps = await tree.querySelectorAll(`#${pkg.name} > .prod:attr(engines, [node])`)

const invalid = []
for (const dep of deps) {
if (omitEngines.includes(dep.name)) {
continue
}

const depEngines = dep.target.package.engines.node
if (!semver.subset(engines, depEngines)) {
invalid.push({
name: `${dep.name}@${dep.version}`,
location: dep.location,
engines: depEngines,
})
}
}

if (invalid.length) {
const title = `The following production dependencies are not compatible with ` +
`\`engines.node: ${engines}\` found in \`${pkgPath}\`:`
return {
title,
body: invalid.map((dep) => [
`${dep.name}:`,
` engines.node: ${dep.engines}`,
` location: ${dep.location}`,
].join('\n')).join('\n'),
solution: 'Remove them or move them to devDependencies.',
}
}
}

module.exports = {
run,
when: ({ pkg, config: c }) => c.applyModule && pkg.engines?.node,
name: 'check-engines',
}
1 change: 1 addition & 0 deletions lib/check/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ module.exports = (root) => run(root, [
require('./check-unwanted.js'),
require('./check-gitignore.js'),
require('./check-changelog.js'),
require('./check-engines.js'),
])
9 changes: 0 additions & 9 deletions lib/content/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ on:
{{> onCi }}

jobs:
engines:
{{> jobMatrix
jobName="Engines"
jobDepFlags="--engines-strict"
macCI=false
windowsCI=false
ciVersions=baseCiVersions
}}

lint:
{{> job jobName="Lint" }}
{{> stepLint jobRunFlags=pkgFlags }}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@commitlint/cli": "^17.1.1",
"@commitlint/config-conventional": "^17.1.0",
"@isaacs/string-locale-compare": "^1.1.0",
"@npmcli/arborist": "^6.0.0",
"@npmcli/git": "^4.0.0",
"@npmcli/map-workspaces": "^3.0.0",
"@npmcli/package-json": "^3.0.0",
Expand Down
Loading

0 comments on commit 49a0581

Please sign in to comment.