generated from remal-github-actions/template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-dependency-engines.js
31 lines (27 loc) · 1.11 KB
/
check-dependency-engines.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import * as fs from 'fs'
import * as semver from 'semver'
let nodeVersion = fs.readFileSync('.nvmrc', 'utf8').trim()
console.log(`Checking that engines.node in package.json file of all dependencies satisfies Node.js version ${nodeVersion}...`)
if (nodeVersion.match(/^v?\d+$/)) nodeVersion += '.9999.9999'
function processPackageJsonFiles(dir) {
const files = fs.readdirSync(dir)
if (files.includes('package.json')) {
const packageJsonFile = `${dir}/package.json`
const packageJsonContent = fs.readFileSync(packageJsonFile, 'utf8').trim()
const packageJson = JSON.parse(packageJsonContent)
const nodeRange = packageJson.engines?.node
if (nodeRange != null) {
if (!semver.satisfies(nodeVersion, nodeRange)) {
throw new Error(`Incompatible engines.node in ${packageJsonFile}: ${nodeRange}`)
}
}
return
}
for (const file of files) {
const nextDir = `${dir}/${file}`
if (fs.statSync(nextDir).isDirectory()) {
processPackageJsonFiles(nextDir)
}
}
}
processPackageJsonFiles('node_modules')