forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-versions.ts
83 lines (72 loc) · 2.6 KB
/
check-versions.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Script to check that:
// - All dependencies has "1.2.3" requirement and not "^1.2.3" used by npm.
// It prevents unexpected updates on lock file issues.
// - Node.js and pnpm versions are the same in all configs
// (Dockerfile, package.json, .node-version).
// - Workflow’s actions has full version.
import { globSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { styleText } from 'node:util'
const ROOT = join(import.meta.dirname, '..')
function read(file: string): string {
return readFileSync(join(ROOT, file)).toString()
}
function error(msg: string): void {
process.stderr.write(styleText('red', `${msg}\n`))
process.exit(1)
}
function getVersion(content: string, regexp: RegExp): string {
return content.match(regexp)![1]!
}
let dockerfile = read('.devcontainer/Dockerfile')
let nodeFull = getVersion(dockerfile, /NODE_VERSION (\d+\.\d+\.\d+)/)
let nodeMinor = nodeFull.split('.').slice(0, 2).join('.')
let pnpmFull = getVersion(dockerfile, /PNPM_VERSION (\d+\.\d+\.\d+)/)
let pnpmMajor = pnpmFull.split('.')[0]
let nodeVersion = read('.node-version').trim()
if (nodeVersion !== nodeFull) {
error(
'.devcontainer/Dockerfile and .node-version have different Node.js version'
)
}
let packageManager = getVersion(
read('package.json'),
/"packageManager": "pnpm@(\d+\.\d+\.\d+)"/
)
if (packageManager !== pnpmFull) {
error('.devcontainer/Dockerfile and package.json have different pnpm version')
}
for (let file of globSync(['./*/package.json', 'package.json'])) {
let content = read(file)
if (!content.includes(`"node": "^${nodeMinor}.`)) {
error(`.devcontainer/Dockerfile and ${file} have different Node.js version`)
}
if (!content.includes(`"pnpm": "^${pnpmMajor}.`)) {
error(`.devcontainer/Dockerfile and ${file} have different pnpm version`)
}
let match = content.match(/"[^"]+": "[\^~][^"]+"/g)
for (let version of match || []) {
if (!version.startsWith('"node":') && !version.startsWith('"pnpm":')) {
let line = content.split('\n').findIndex(i => i.includes(version)) + 1
error(
`Not locked version in ${file}:${line}: ${styleText('yellow', version)}`
)
}
}
}
for (let file of globSync('**/Dockerfile')) {
let match = read(file).match(/NODE_VERSION\s+(\d+\.\d+\.\d+)/)
if (match && match[1] !== nodeFull) {
error(
`Different Node.js version in ${file}: ${styleText('yellow', match[1]!)}`
)
}
}
for (let file of globSync('.github/**/*.yml')) {
let match = read(file).match(/\w+\/\w+@v?\d+$/m)
if (match) {
error(
`Non-locked action version in ${file}: ${styleText('yellow', match[0])}`
)
}
}