-
Notifications
You must be signed in to change notification settings - Fork 518
/
update-nodejs-versions.js
105 lines (93 loc) · 2.81 KB
/
update-nodejs-versions.js
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
// This script creates output that is copy/pasted into /internal/node/node_versions.bzl to
// add all published NodeJS versions >= 8.0.0.
// See the update-nodejs-versions script in package.json
const https = require("https");
const MIN_VERSION = [8, 0, 0];
const MAX_VERSION = [14, 99, 99];
const REPOSITORY_TYPES = {
"darwin-x64.tar.gz": "darwin_amd64",
"linux-x64.tar.xz": "linux_amd64",
"linux-arm64.tar.xz": "linux_arm64",
"linux-s390x.tar.xz": "linux_s390x",
"win-x64.zip": "windows_amd64",
};
function getText(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
if (res.statusCode !== 200) {
return reject(new Error());
}
const body = [];
res.on("data", (chunk) => body.push(String(chunk)));
res.on("end", () => resolve(body.join("")));
});
});
}
async function getJson(url) {
return JSON.parse(await getText(url));
}
function versionCompare(lhs, rhs) {
if (lhs[0] !== rhs[0]) {
return lhs[0] - rhs[0];
}
if (lhs[1] !== rhs[1]) {
return lhs[1] - rhs[1];
}
return lhs[2] - rhs[2];
}
async function getNodeJsVersions() {
const json = await getJson("https://nodejs.org/dist/index.json");
return (json.map(({version}) => version.slice(1).split('.').map(Number))
.filter(
(version) => versionCompare(version, MIN_VERSION) >= 0 &&
versionCompare(version, MAX_VERSION) <= 0)
.sort(versionCompare));
}
async function getNodeJsVersion(version) {
const text = await getText(
`https://nodejs.org/dist/v${version.join(".")}/SHASUMS256.txt`
);
return {
version,
repositories: text
.split("\n")
.filter(Boolean)
.map((line) => {
const [sha, filename] = line.split(/\s+/);
const type = REPOSITORY_TYPES[filename.replace(/^node-v[\d.]+-/, "")];
return type ? { filename, sha, type } : undefined;
})
.filter(Boolean),
};
}
async function main() {
const versions = await getNodeJsVersions();
const nodeRepositories = await Promise.all(versions.map(getNodeJsVersion));
console.log('""" Generated code; do not edit');
console.log('Update by running yarn update-nodejs-versions\n"""\n');
// Suppress buildifier
console.log('# @unsorted-dict-items');
console.log('NODE_VERSIONS = {');
nodeRepositories.forEach(({ version, repositories }) => {
console.log(
[
`# ${version.join(".")}`,
...repositories.map(
({ filename, sha, type }) =>
`"${version.join(
"."
)}-${type}": ("${filename}", "${filename.replace(
/(\.tar)?\.[^.]+$/,
""
)}", "${sha}"),`
),
]
.map((line) => ` ${line}`)
.join("\n")
);
});
console.log("}");
}
if (require.main === module) {
main();
}