forked from aredridel/node-bin-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·282 lines (239 loc) · 6.65 KB
/
index.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env node
"use strict";
import { open, unlink, mkdir, writeFile, readFile } from "node:fs/promises";
import { Writable, Transform } from "node:stream";
import { join, resolve, dirname } from "node:path";
import { debuglog, promisify } from "node:util";
import { execFile as execFile_, spawn } from "node:child_process";
import { fileURLToPath } from "url";
import verr from "verror";
import {rimraf} from "rimraf";
import zlib from "zlib";
import yargs from "yargs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const execFile = promisify(execFile_);
const { VError } = verr;
const argparser = yargs(process.argv.slice(2));
argparser.option("skip-binaries", {
describe: "Skip downloading the binaries",
boolean: true,
});
argparser.option("only", { describe: "Only download this binary package" });
argparser.option("package-name", {
alias: "n",
describe: "Use this as the main package name",
default: "node",
});
argparser.option("verbose", { describe: "output messages", boolean: true });
argparser.version();
argparser.demandCommand(
1,
2,
"You must specify version, and optionally a prerelease"
);
argparser.help("help").wrap(76);
const argv = argparser.argv;
const versionprime = argv._[0];
const pre = argv._[1];
if (!versionprime) {
console.warn("Use: " + argv.$0 + " version [pre]");
process.exit(1);
}
const ndebug = debuglog("node-bin-gen");
function debug(...args) {
if (argv.verbose) {
console.warn(...args);
}
ndebug(...args);
}
const version = versionprime[0] != "v" ? "v" + versionprime : versionprime;
async function buildArchPackage(os, cpu, version, pre) {
debug("building architecture specific package", os, cpu, version, pre);
const platform = os == "win" ? "win32" : os;
const arch = os == "win" && cpu == "ia32" ? "x86" : cpu;
const executable = os == "win" ? "bin/node.exe" : "bin/node";
const dir = "node-" + os + "-" + cpu;
const base = "node-" + version + "-" + os + "-" + cpu;
const filename = base + (os == "win" ? ".zip" : ".tar.gz");
const pkg = {
name:
(os == "darwin" && cpu == "arm64" ? "node-bin" : "node") +
"-" +
os +
"-" +
cpu,
version: version + (pre != null ? "-" + pre : ""),
description: "node",
bin: {
node: executable,
},
files: [
os == "win" ? "bin/node.exe" : "bin/node",
"share",
"include",
"*.md",
"LICENSE",
],
os: platform,
cpu: arch,
};
debug("removing", dir);
await rimraf(dir, { glob: false });
debug("creating", dir);
await mkdir(dir).catch((e) => {
if (e.code != "EEXIST") throw e;
});
const url =
"https://nodejs.org" +
(/rc/.test(version)
? "/download/rc/"
: /test/.test(version)
? "/download/test/"
: "/dist/") +
version +
"/" +
filename;
debug("Fetching", url);
const res = await fetch(url);
if (res.status != 200 && res.status != 304) {
throw new VError("not ok: fetching %j got status code %s", url, res.status);
}
debug("Unpacking into", dir);
if (os == "win") {
const f = await open(filename, "w");
await res.body.pipeTo(Writable.toWeb(f.createWriteStream()));
const running = await execFile("unzip", [
"-d",
`${dir}/bin`,
"-o",
"-j",
filename,
`${base}/node.exe`,
]);
if (running.stderr) console.warn("error from unzip", running.stderr);
await unlink(filename);
} else {
const c = spawn("tar", ["--strip-components=1", "-C", dir, "-x"], {
stdio: ["pipe", process.stdout, process.stderr],
});
const unzip = Transform.toWeb(zlib.createGunzip());
await res.body.pipeThrough(unzip).pipeTo(Writable.toWeb(c.stdin));
}
debug("Finished unpacking into", dir);
await writeFile(join(dir, "package.json"), JSON.stringify(pkg, null, 2));
return pkg;
}
async function fetchManifest(version) {
const base = "http://nodejs.org";
const url = (function () {
if (/rc/.test(version)) {
return `${base}/download/rc/index.json`;
} else if (/test/.test(version)) {
return `${base}/download/test/index.json`;
} else {
return `${base}/dist/index.json`;
}
})();
const res = await fetch(url);
return res.json();
}
main().catch((err) => {
console.warn(err);
process.exit(1);
});
async function main() {
const manifest = argv["skip-binaries"] ? [] : await fetchManifest(version);
const v = manifest
.filter(function (ver) {
return ver.version == version;
})
.shift();
if (!v) {
throw new VError("No such version '%s'", version);
}
debug("manifest", v);
if (!v.files || !v.files.length) {
debug("No files, defaulting");
v.files = [
"darwin-x64",
"darwin-arm64",
"linux-arm64",
"linux-armv7l",
"linux-ppc64",
"linux-ppc64le",
"linux-s390x",
"linux-x64",
"linux-x86",
"sunos-x64",
"win-arm64",
"win-x64",
"win-x86",
];
}
const files = argv.only ? [argv.only] : v.files;
const binaries = files
.filter(function (f) {
return (
!/^headers|^src/.test(f) &&
!/pkg$/.test(f) &&
!/^win-([^-]+)-(exe|msi|7z)/.test(f)
);
})
.map(function (f) {
const bits = f.split("-");
return {
os: bits[0].replace(/^osx$/, "darwin"),
cpu: bits[1],
format: bits[2] || "tar.gz",
};
});
for (const v of binaries) {
await buildArchPackage(v.os, v.cpu, version, pre)
}
const pkg = await buildMetapackage(version + (pre != null ? "-" + pre : ""));
try {
await mkdir(pkg.name);
} catch (err) {
if (err && err.code != "EEXIST") {
throw err;
}
}
const script = `require('node-bin-setup')("${pkg.version}", require)`;
await Promise.all([
readFile(resolve(__dirname, "node-bin-README.md"), "utf8").then((readme) =>
writeFile(
resolve(pkg.name, "README.md"),
readme.replace(/\$\{packagename\}/g, pkg.name)
)
),
writeFile(resolve(pkg.name, "package.json"), JSON.stringify(pkg, null, 2)),
writeFile(resolve(pkg.name, "installArchSpecificPackage.js"), script),
]);
}
async function buildMetapackage(version) {
const pkg = {
name: argv["package-name"],
version: version.replace(/^v/, ""),
description: "node",
main: "index.js",
keywords: ["runtime"],
repository: JSON.parse(await readFile(`${__dirname}/package.json`))
.repository,
scripts: {
preinstall: "node installArchSpecificPackage",
},
bin: {
node: "bin/node",
},
dependencies: {
"node-bin-setup": "^1.0.0",
},
license: "ISC",
author: "",
engines: {
npm: ">=5.0.0",
},
};
return pkg;
}