-
Notifications
You must be signed in to change notification settings - Fork 37
/
update-env.ts
167 lines (143 loc) · 4.99 KB
/
update-env.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
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
// Script to update Node.js and pnpm everywhere
import { createHash } from 'node:crypto'
import { globSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { styleText } from 'node:util'
const ROOT = join(import.meta.dirname, '..')
interface Release {
version: string
}
type Architecture = 'arm64' | 'x64'
type Architectures = Record<Architecture, string>
async function getLatestNodeVersion(major: string): Promise<string> {
let response = await fetch('https://nodejs.org/dist/index.json')
let data: Release[] = await response.json()
let filtered = data.filter(i => i.version.startsWith(`v${major}.`))
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 = await response.json()
return data.tag_name.slice(1)
}
async function getNodeSha256(version: string): Promise<Architectures> {
let response = await fetch(
`https://nodejs.org/dist/v${version}/SHASUMS256.txt`
)
let data = await response.text()
let lines = data.split('\n')
return {
arm64: lines.find(i => i.endsWith('-linux-arm64.tar.gz'))!.split(' ')[0]!,
x64: lines.find(i => i.endsWith('-linux-x64.tar.gz'))!.split(' ')[0]!
}
}
async function getPnpmSha256(
version: string,
arch: Architecture
): 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').toString()
}
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(`ENV ${key} .+`, 'g'), `ENV ${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 ')) {
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 (.+)/)![1]!
let currentPnpm = dockerfile.match(/PNPM_VERSION (.+)/)![1]!
let latestNode = await getLatestNodeVersion(
process.argv[2] ?? currentNode.split('.')[0]!
)
let latestPnpm = await getLatestPnpmVersion()
if (currentNode !== latestNode) {
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) {
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) {
process.stderr.write(
styleText('gray', 'No Node.js or pnpm updates available\n')
)
}