-
Notifications
You must be signed in to change notification settings - Fork 309
/
node.ts
79 lines (69 loc) · 2.38 KB
/
node.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
import {Errors, Interfaces} from '@oclif/core'
import * as path from 'path'
import * as qq from 'qqjs'
import {log} from '../log'
type Options = {
nodeVersion: string;
output: string;
platform: Interfaces.PlatformTypes;
arch: Interfaces.ArchTypes | 'armv7l';
tmp: string
}
async function checkFor7Zip() {
try {
await qq.x('7z', {stdio: [0, null, 2]})
} catch (error: any) {
if (error.code === 127) Errors.error('install 7-zip to package windows tarball')
else throw error
}
}
export async function fetchNodeBinary({nodeVersion, output, platform, arch, tmp}: Options): Promise<string> {
if (arch === 'arm') arch = 'armv7l'
let nodeBase = `node-v${nodeVersion}-${platform}-${arch}`
let tarball = path.join(tmp, 'node', `${nodeBase}.tar.xz`)
let url = `https://nodejs.org/dist/v${nodeVersion}/${nodeBase}.tar.xz`
if (platform === 'win32') {
await checkFor7Zip()
nodeBase = `node-v${nodeVersion}-win-${arch}`
tarball = path.join(tmp, 'node', `${nodeBase}.7z`)
url = `https://nodejs.org/dist/v${nodeVersion}/${nodeBase}.7z`
output += '.exe'
}
let cache = path.join(tmp, 'cache', `node-v${nodeVersion}-${platform}-${arch}`)
if (platform === 'win32') cache += '.exe'
const download = async () => {
log(`downloading ${nodeBase}`)
const shasums = path.join(tmp, 'cache', nodeVersion, 'SHASUMS256.txt.asc')
if (!await qq.exists(shasums)) {
await qq.download(`https://nodejs.org/dist/v${nodeVersion}/SHASUMS256.txt.asc`, shasums)
}
const basedir = path.dirname(tarball)
await qq.mkdirp(basedir)
await qq.download(url, tarball)
await qq.x(`grep "${path.basename(tarball)}" "${shasums}" | shasum -a 256 -c -`, {cwd: basedir})
}
const extract = async () => {
log(`extracting ${nodeBase}`)
const nodeTmp = path.join(tmp, 'node')
await qq.rm([nodeTmp, nodeBase])
await qq.mkdirp(nodeTmp)
await qq.mkdirp(path.dirname(cache))
if (platform === 'win32') {
qq.pushd(nodeTmp)
await qq.x(`7z x -bd -y "${tarball}" > /dev/null`)
await qq.mv([nodeBase, 'node.exe'], cache)
qq.popd()
} else {
await qq.x(`tar -C "${tmp}/node" -xJf "${tarball}"`)
await qq.mv([nodeTmp, nodeBase, 'bin/node'], cache)
}
}
if (await qq.exists(cache)) {
await qq.cp(cache, output)
} else {
await download()
await extract()
await qq.cp(cache, output)
}
return output
}