1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const errors_1 = require("@oclif/errors");
|
4 | const path = require("path");
|
5 | const qq = require("qqjs");
|
6 | const log_1 = require("../log");
|
7 | async function checkFor7Zip() {
|
8 | try {
|
9 | await qq.x('7z', { stdio: [0, null, 2] });
|
10 | }
|
11 | catch (error) {
|
12 | if (error.code === 127)
|
13 | errors_1.error('install 7-zip to package windows tarball');
|
14 | else
|
15 | throw error;
|
16 | }
|
17 | }
|
18 | async function fetchNodeBinary({ nodeVersion, output, platform, arch, tmp }) {
|
19 | if (arch === 'arm')
|
20 | arch = 'armv7l';
|
21 | let nodeBase = `node-v${nodeVersion}-${platform}-${arch}`;
|
22 | let tarball = path.join(tmp, 'node', `${nodeBase}.tar.xz`);
|
23 | let url = `https://nodejs.org/dist/v${nodeVersion}/${nodeBase}.tar.xz`;
|
24 | if (platform === 'win32') {
|
25 | await checkFor7Zip();
|
26 |
|
27 | nodeBase = `node-v${nodeVersion}-win-${arch}`;
|
28 | tarball = path.join(tmp, 'node', `${nodeBase}.7z`);
|
29 | url = `https://nodejs.org/dist/v${nodeVersion}/${nodeBase}.7z`;
|
30 | output += '.exe';
|
31 | }
|
32 | let cache = path.join(tmp, 'cache', `node-v${nodeVersion}-${platform}-${arch}`);
|
33 | if (platform === 'win32')
|
34 | cache += '.exe';
|
35 | const download = async () => {
|
36 | log_1.log(`downloading ${nodeBase}`);
|
37 | const shasums = path.join(tmp, 'cache', nodeVersion, 'SHASUMS256.txt.asc');
|
38 | if (!await qq.exists(shasums)) {
|
39 | await qq.download(`https://nodejs.org/dist/v${nodeVersion}/SHASUMS256.txt.asc`, shasums);
|
40 | }
|
41 | const basedir = path.dirname(tarball);
|
42 | await qq.mkdirp(basedir);
|
43 | await qq.download(url, tarball);
|
44 | await qq.x(`grep ${path.basename(tarball)} ${shasums} | shasum -a 256 -c -`, { cwd: basedir });
|
45 | };
|
46 | const extract = async () => {
|
47 | log_1.log(`extracting ${nodeBase}`);
|
48 | const nodeTmp = path.join(tmp, 'node');
|
49 | await qq.rm([nodeTmp, nodeBase]);
|
50 | await qq.mkdirp(nodeTmp);
|
51 | await qq.mkdirp(path.dirname(cache));
|
52 | if (platform === 'win32') {
|
53 | qq.pushd(nodeTmp);
|
54 | await qq.x(`7z x -bd -y ${tarball} > /dev/null`);
|
55 | await qq.mv([nodeBase, 'node.exe'], cache);
|
56 | qq.popd();
|
57 | }
|
58 | else {
|
59 | await qq.x(`tar -C ${tmp}/node -xJf ${tarball}`);
|
60 | await qq.mv([nodeTmp, nodeBase, 'bin/node'], cache);
|
61 | }
|
62 | };
|
63 | if (await qq.exists(cache)) {
|
64 | await qq.cp(cache, output);
|
65 | }
|
66 | else {
|
67 | await download();
|
68 | await extract();
|
69 | await qq.cp(cache, output);
|
70 | }
|
71 | return output;
|
72 | }
|
73 | exports.fetchNodeBinary = fetchNodeBinary;
|