From 73db964c938f11f0123be91730cc2479a070a4e7 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Thu, 26 Oct 2023 21:00:01 -0700 Subject: [PATCH] chore: add check engines script to CI --- .github/scripts/check-engines.js | 42 ++++++++++++++++++++++++++++++++ .github/workflows/tests.yml | 17 +++++++++++++ package.json | 3 ++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 .github/scripts/check-engines.js diff --git a/.github/scripts/check-engines.js b/.github/scripts/check-engines.js new file mode 100644 index 0000000000..45ad1f72b7 --- /dev/null +++ b/.github/scripts/check-engines.js @@ -0,0 +1,42 @@ +const { join } = require('path') +const semver = require('semver') +const Arborist = require('@npmcli/arborist') + +const run = async (path, useEngines) => { + const pkgPath = join(path, 'package.json') + const pkg = require(pkgPath) + + const engines = useEngines || pkg.engines.node + + const arb = new Arborist({ path }) + const tree = await arb.loadActual({ forceActual: true }) + const deps = await tree.querySelectorAll(`#${pkg.name} > .prod:attr(engines, [node])`) + + const invalid = [] + for (const dep of deps) { + + 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 msg = `The following production dependencies are not compatible with ` + + `\`engines.node: ${engines}\` found in \`${pkgPath}\`:\n` + invalid.map((dep) => [ + `${dep.name}:`, + ` engines.node: ${dep.engines}`, + ` location: ${dep.location}`, + ].join('\n')).join('\n') + throw new Error(msg) + } +} + +run(process.cwd(), ...process.argv.slice(2)).then(() => console.log('Success')).catch((err) => { + console.error(err) + process.exitCode = 1 +}) \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e952db4be1..24a1435d77 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,6 +18,23 @@ jobs: - uses: actions/checkout@v3 - run: pip install --user ruff - run: ruff --format=github --select="E,F,PLC,PLE,UP,W,YTT" --ignore="PLC1901,S101,UP031" --target-version=py37 . + Engines: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + - name: Use Node.js 20.x + uses: actions/setup-node@v3 + with: + node-version: 20.x + - name: Install Dependencies + run: | + npm install --no-progress + - name: Check Engines + # Override the actual engines being checked with a slight modification, changing + # ^14.13 to ^14.15. This is to get this test in place to stop future regressions + # but there are already engines in the dependencies that conflict. + run: node .github/scripts/check-engines.js "^12.13 || ^14.15 || >=16" Tests: strategy: fail-fast: false diff --git a/package.json b/package.json index 4a7dc7efff..b029d8866a 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "nopt": "^6.0.0", "npmlog": "^6.0.0", "rimraf": "^3.0.2", - "semver": "^7.3.5", "tar": "^6.1.2", "which": "^2.0.2" }, @@ -38,10 +37,12 @@ "node": "^12.13 || ^14.13 || >=16" }, "devDependencies": { + "@npmcli/arborist": "^7.2.0", "bindings": "^1.5.0", "mocha": "^10.2.0", "nan": "^2.14.2", "require-inject": "^1.4.4", + "semver": "^7.5.4", "standard": "^14.3.4" }, "scripts": {