forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-env.ts
More file actions
190 lines (164 loc) · 5.68 KB
/
update-env.ts
File metadata and controls
190 lines (164 loc) · 5.68 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Script to update Node.js and pnpm everywhere.
//
// By default it will keep Node.js major version, but can update to next major
// by `pnpm update-env --major` argument. You can specify version `--major 22`.
//
// If you change script and need to update result without new Node.js version
// run it with `pnpm update-env --force` argument.
import { createHash } from 'node:crypto'
import { globSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { styleText } from 'node:util'
const FORCE = process.argv.includes('--force')
function getMajor(current: string): string | undefined {
for (let i = 0; i < process.argv.length; i++) {
if (process.argv[i] === '--major') {
let next = process.argv[i + 1]
if (next && /^[\d+.]+$/.test(next)) {
return next
} else {
return undefined
}
}
}
return current
}
const ROOT = join(import.meta.dirname, '..')
interface Release {
version: string
}
type Architectures = { arm64: string; x64: string }
async function getLatestNodeVersion(
major: string | undefined
): Promise<string> {
let response = await fetch('https://nodejs.org/dist/index.json')
let data: Release[] = await response.json()
let filtered = major
? data.filter(i => i.version.startsWith(`v${major}.`))
: data
return filtered[0]!.version.slice(1)
}
async function getLatestPnpmVersion(): Promise<string> {
let response = await fetch(
'https://api.github.com/repos/pnpm/pnpm/releases/latest'
)
let data: { tag_name: string } = await response.json()
return data.tag_name.slice(1)
}
async function getNodeSha256(version: string): Promise<Architectures> {
let data = await fetch(`https://nodejs.org/dist/v${version}/SHASUMS256.txt`)
let text = await data.text()
let lines = text.split('\n')
return {
arm64: lines.find(i => i.endsWith('-linux-arm64.tar.xz'))!.split(' ')[0]!,
x64: lines.find(i => i.endsWith('-linux-x64.tar.xz'))!.split(' ')[0]!
}
}
async function getPnpmSha256(
version: string,
arch: 'arm64' | 'x64'
): Promise<string> {
let binary = await fetch(
'https://github.com/pnpm/pnpm/releases/download/' +
`v${version}/pnpm-linux-${arch}`
)
return createHash('sha256')
.update(Buffer.from(await binary.arrayBuffer()))
.digest('hex')
}
function read(file: string): string {
return readFileSync(file, 'utf-8')
}
function updatePackages(cb: (content: string) => string): void {
let files = globSync('**/package.json')
for (let file of files) {
let content = read(file)
let updated = cb(content)
writeFileSync(file, updated)
}
}
function updateProjectDockerfiles(cb: (content: string) => string): void {
let files = globSync(['**/Dockerfile', '.devcontainer/Dockerfile'])
for (let file of files) {
let content = read(file)
let updated = cb(content)
writeFileSync(file, updated)
}
}
function printUpdate(tool: string, prev: string, next: string): void {
process.stderr.write(
`${tool}: ${styleText('red', prev)} → ${styleText('green', next)}\n`
)
}
function replaceEnv(file: string, key: string, value: string): string {
return file.replace(new RegExp(` ${key}=[^\\s]+`, 'g'), ` ${key}=${value}`)
}
function replaceVersionEnv(
content: string,
tool: string,
version: string,
checksums: Architectures
): string {
let fixed = replaceEnv(content, `${tool}_VERSION`, version)
if (content.includes('_CHECKSUM_')) {
for (let [arch, checksum] of Object.entries(checksums)) {
let name = `${tool}_CHECKSUM_${arch.toUpperCase()}`
fixed = replaceEnv(fixed, name, checksum)
}
} else if (content.includes('_CHECKSUM ') && checksums.x64) {
fixed = replaceEnv(fixed, `${tool}_CHECKSUM`, 'sha256:' + checksums.x64)
}
return fixed
}
function replaceKey(file: string, key: string, value: string): string {
return file.replace(
new RegExp(`"${key}": "[^"]+"`, 'g'),
`"${key}": "${value}"`
)
}
let dockerfile = read(join(ROOT, '.devcontainer', 'Dockerfile'))
let currentNode = dockerfile.match(/NODE_VERSION=(\S+)/)![1]!
let currentPnpm = dockerfile.match(/PNPM_VERSION=(\S+)/)![1]!
let latestNode = await getLatestNodeVersion(
getMajor(currentNode.split('.')[0]!)
)
let latestPnpm = await getLatestPnpmVersion()
if (currentNode !== latestNode || FORCE) {
printUpdate('Node.js', currentNode, latestNode)
let checksums = await getNodeSha256(latestNode)
dockerfile = replaceVersionEnv(dockerfile, 'NODE', latestNode, checksums)
writeFileSync(join(ROOT, '.devcontainer', 'Dockerfile'), dockerfile)
writeFileSync(join(ROOT, '.node-version'), latestNode + '\n')
updateProjectDockerfiles(projectDocker => {
return replaceVersionEnv(projectDocker, 'NODE', latestNode, checksums)
})
let minor = latestNode.split('.').slice(0, 2).join('.')
if (currentNode.split('.').slice(0, 2).join('.') !== minor) {
updatePackages(pkg => replaceKey(pkg, 'node', `^${minor}.0`))
}
}
if (currentPnpm !== latestPnpm || FORCE) {
printUpdate('pnpm', currentPnpm, latestPnpm)
let [checksumArm, checksumX86] = await Promise.all([
getPnpmSha256(latestPnpm, 'arm64'),
getPnpmSha256(latestPnpm, 'x64')
])
dockerfile = replaceVersionEnv(dockerfile, 'PNPM', latestPnpm, {
arm64: checksumArm,
x64: checksumX86
})
writeFileSync(join(ROOT, '.devcontainer', 'Dockerfile'), dockerfile)
updatePackages(pkg => {
pkg = replaceKey(pkg, 'packageManager', `pnpm@${latestPnpm}`)
let major = latestPnpm.split('.')[0]
if (currentPnpm.split('.')[0] !== major) {
pkg = replaceKey(pkg, 'pnpm', `^${major}.0.0`)
}
return pkg
})
}
if (currentNode === latestNode && currentPnpm === latestPnpm && !FORCE) {
process.stderr.write(
styleText('gray', 'No Node.js or pnpm updates available\n')
)
}