Skip to content

Commit 7714b2b

Browse files
Kikobeatsclaude
andcommitted
ci: check the package on its minimum Node.js version
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cb471db commit 7714b2b

1 file changed

Lines changed: 212 additions & 0 deletions

File tree

‎.github/workflows/engines.yml‎

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
name: engines
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
permissions:
10+
contents: read
11+
12+
env:
13+
pnpm_config_dangerously_allow_all_builds: true
14+
SKIP_INSTALL_SIMPLE_GIT_HOOKS: true
15+
16+
jobs:
17+
pack:
18+
name: Pack
19+
runs-on: ubuntu-latest
20+
outputs:
21+
matrix: ${{ steps.pack.outputs.matrix }}
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v7
25+
with:
26+
persist-credentials: false
27+
- name: Setup Node.js
28+
uses: actions/setup-node@v7
29+
with:
30+
node-version: lts/*
31+
- name: Setup PNPM
32+
uses: pnpm/action-setup@v6
33+
with:
34+
version: latest
35+
- name: Install
36+
run: pnpm install
37+
- name: Pack
38+
id: pack
39+
run: |
40+
node --input-type=module - <<'EOF'
41+
import { execFileSync } from 'node:child_process'
42+
import { appendFileSync, mkdirSync, readdirSync, readFileSync } from 'node:fs'
43+
import path from 'node:path'
44+
45+
const OUT = path.resolve('tarballs')
46+
const FLOOR = /^>=\s*(\d+(?:\.\d+){0,2})$/
47+
const WORKSPACE_DEPENDENCY_FIELDS = ['dependencies', 'optionalDependencies']
48+
const WORKSPACE_ALIAS = /^workspace:((?:@[^/@]+\/)?[^@]+)@/
49+
50+
const pnpm = (args, cwd) =>
51+
execFileSync('pnpm', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'inherit'] })
52+
53+
const floorOf = manifest => {
54+
const range = manifest.engines?.node
55+
const match = range?.trim().match(FLOOR)
56+
if (!match) throw new Error(`${manifest.name}: engines.node ${JSON.stringify(range)} is not a ">= x" floor`)
57+
return match[1]
58+
}
59+
60+
const packages = JSON.parse(pnpm(['ls', '--recursive', '--depth', '-1', '--json']))
61+
.filter(({ private: isPrivate }) => !isPrivate)
62+
.map(({ path: dir }) => {
63+
const manifest = JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'))
64+
return { dir, manifest, floor: floorOf(manifest) }
65+
})
66+
67+
for (const { dir, manifest } of packages) {
68+
if (manifest.scripts?.prepublishOnly) pnpm(['run', 'prepublishOnly'], dir)
69+
}
70+
71+
mkdirSync(OUT, { recursive: true })
72+
const tarballOf = new Map()
73+
for (const { dir, manifest } of packages) {
74+
const before = new Set(readdirSync(OUT))
75+
pnpm(['pack', '--pack-destination', OUT], dir)
76+
const [tarball] = readdirSync(OUT).filter(file => !before.has(file))
77+
tarballOf.set(manifest.name, tarball)
78+
}
79+
80+
const workspaceDependencies = manifest =>
81+
WORKSPACE_DEPENDENCY_FIELDS.flatMap(field =>
82+
Object.entries(manifest[field] ?? {})
83+
.filter(([, spec]) => spec.startsWith('workspace:'))
84+
.map(([name, spec]) => spec.match(WORKSPACE_ALIAS)?.[1] ?? name)
85+
)
86+
87+
const manifestOf = new Map(packages.map(({ manifest }) => [manifest.name, manifest]))
88+
const closure = name => {
89+
const seen = new Set()
90+
const visit = current => {
91+
if (seen.has(current)) return
92+
const manifest = manifestOf.get(current)
93+
if (!manifest) throw new Error(`${name} depends on ${current}, a private workspace package that is never published`)
94+
seen.add(current)
95+
workspaceDependencies(manifest).forEach(visit)
96+
}
97+
visit(name)
98+
return [...seen]
99+
}
100+
101+
const include = packages.map(({ manifest, floor }) => ({
102+
name: manifest.name,
103+
floor,
104+
tarballs: closure(manifest.name).map(name => tarballOf.get(name)).join(' ')
105+
}))
106+
107+
console.table(include)
108+
appendFileSync(process.env.GITHUB_OUTPUT, `matrix=${JSON.stringify({ include })}\n`)
109+
EOF
110+
- name: Upload
111+
uses: actions/upload-artifact@v7
112+
with:
113+
name: tarballs
114+
path: tarballs
115+
116+
floor:
117+
name: Node ${{ matrix.floor }} ${{ matrix.name }}
118+
needs: pack
119+
runs-on: ubuntu-latest
120+
timeout-minutes: 10
121+
strategy:
122+
fail-fast: false
123+
matrix: ${{ fromJson(needs.pack.outputs.matrix) }}
124+
steps:
125+
- name: Setup Node.js
126+
uses: actions/setup-node@v7
127+
with:
128+
node-version: ${{ matrix.floor }}
129+
- name: Download
130+
uses: actions/download-artifact@v8
131+
with:
132+
name: tarballs
133+
path: tarballs
134+
- name: Install with engine-strict
135+
env:
136+
TARBALLS: ${{ matrix.tarballs }}
137+
run: |
138+
mkdir consumer
139+
cd consumer
140+
echo '{"private":true}' > package.json
141+
read -ra tarballs <<< "$TARBALLS"
142+
npm install --engine-strict --no-audit --no-fund "${tarballs[@]/#/../tarballs/}"
143+
- name: Load every entry point
144+
working-directory: consumer
145+
env:
146+
PACKAGE: ${{ matrix.name }}
147+
run: |
148+
echo 'module.exports = specifier => import(specifier)' > import.js
149+
cat > smoke.js <<'EOF'
150+
'use strict'
151+
152+
const fs = require('fs')
153+
const path = require('path')
154+
155+
const name = process.argv[2]
156+
const manifest = JSON.parse(fs.readFileSync(path.join('node_modules', name, 'package.json'), 'utf8'))
157+
const TYPE_DECLARATION = /\.d\.[cm]?ts$/
158+
const IMPORT_ONLY = ['ERR_REQUIRE_ESM', 'ERR_REQUIRE_ASYNC_MODULE', 'ERR_PACKAGE_PATH_NOT_EXPORTED']
159+
160+
const targets = (value, condition) => {
161+
if (typeof value === 'string') return condition === 'types' ? [] : [value]
162+
if (!value || typeof value !== 'object') return []
163+
return Object.keys(value).reduce((all, key) => all.concat(targets(value[key], key)), [])
164+
}
165+
166+
const isSubpathMap = value =>
167+
value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).some(key => key[0] === '.')
168+
169+
const entryPoints = exportsField => {
170+
if (exportsField == null) return ['.']
171+
const subpaths = isSubpathMap(exportsField) ? exportsField : { '.': exportsField }
172+
return Object.keys(subpaths).filter(
173+
subpath =>
174+
subpath.indexOf('*') === -1 && targets(subpaths[subpath]).some(target => !TYPE_DECLARATION.test(target))
175+
)
176+
}
177+
178+
const load = specifier => {
179+
try {
180+
return Promise.resolve(require(specifier))
181+
} catch (error) {
182+
if (IMPORT_ONLY.indexOf(error.code) === -1) return Promise.reject(error)
183+
return require('./import.js')(specifier)
184+
}
185+
}
186+
187+
const specifiers = entryPoints(manifest.exports).map(subpath => name + subpath.slice(1))
188+
189+
specifiers
190+
.reduce(
191+
(previous, specifier) =>
192+
previous.then(failures =>
193+
load(specifier)
194+
.then(value => {
195+
if (value === undefined) throw new Error('exports undefined')
196+
})
197+
.then(
198+
() => {
199+
console.log('ok ' + specifier)
200+
return failures
201+
},
202+
error => {
203+
console.log('FAIL ' + specifier + '\n' + (error && error.stack))
204+
return failures + 1
205+
}
206+
)
207+
),
208+
Promise.resolve(0)
209+
)
210+
.then(failures => process.exit(failures ? 1 : 0))
211+
EOF
212+
node smoke.js "$PACKAGE"

0 commit comments

Comments
 (0)