-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-node-version.mjs
More file actions
39 lines (31 loc) · 1.06 KB
/
check-node-version.mjs
File metadata and controls
39 lines (31 loc) · 1.06 KB
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
32
33
34
35
36
37
38
39
#!/usr/bin/env node
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import semver from 'semver'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// Step 1: Read package.json
const packageJsonPath = path.resolve(__dirname, '../package.json')
let packageJson
try {
const raw = fs.readFileSync(packageJsonPath, 'utf-8')
packageJson = JSON.parse(raw)
} catch (err) {
console.error('❌ Failed to read package.json:', err.message)
process.exit(1)
}
// Step 2: Extract engines.node
const requiredNode = packageJson.engines?.node
if (!requiredNode) {
console.warn('⚠️ No "engines.node" field found in package.json.')
process.exit(0) // No check needed
}
// Step 3: Check current version
const currentVersion = process.versions.node
if (semver.satisfies(currentVersion, requiredNode)) {
console.log(`✅ Node.js ${currentVersion} satisfies "${requiredNode}".`)
process.exit(0)
} else {
console.error(`❌ Node.js ${currentVersion} does NOT satisfy "engines.node": "${requiredNode}".`)
process.exit(1)
}