oclif
Version:
oclif: create your own CLI
82 lines (81 loc) • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchNodeBinary = void 0;
const core_1 = require("@oclif/core");
const path = require("path");
const fs = require("fs-extra");
const node_stream_1 = require("node:stream");
const log_1 = require("../log");
const node_child_process_1 = require("node:child_process");
const node_util_1 = require("node:util");
const got_1 = require("got");
const pipeline = (0, node_util_1.promisify)(node_stream_1.pipeline);
const exec = (0, node_util_1.promisify)(node_child_process_1.exec);
async function checkFor7Zip() {
try {
await exec('7z');
}
catch (error) {
if (error.code === 127)
core_1.Errors.error('install 7-zip to package windows tarball');
else
throw error;
}
}
async function fetchNodeBinary({ nodeVersion, output, platform, arch, tmp }) {
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 () => {
(0, log_1.log)(`downloading ${nodeBase}`);
await Promise.all([
fs.ensureDir(path.join(tmp, 'cache', nodeVersion)),
fs.ensureDir(path.join(tmp, 'node')),
]);
const shasums = path.join(tmp, 'cache', nodeVersion, 'SHASUMS256.txt.asc');
if (!fs.existsSync(shasums)) {
await pipeline(got_1.default.stream(`https://nodejs.org/dist/v${nodeVersion}/SHASUMS256.txt.asc`), fs.createWriteStream(shasums));
}
const basedir = path.dirname(tarball);
await fs.promises.mkdir(basedir, { recursive: true });
await pipeline(got_1.default.stream(url), fs.createWriteStream(tarball));
if (platform !== 'win32')
await exec(`grep "${path.basename(tarball)}" "${shasums}" | shasum -a 256 -c -`, { cwd: basedir });
};
const extract = async () => {
(0, log_1.log)(`extracting ${nodeBase}`);
const nodeTmp = path.join(tmp, 'node');
await fs.promises.mkdir(nodeTmp, { recursive: true });
await fs.promises.mkdir(path.dirname(cache), { recursive: true });
if (platform === 'win32') {
await exec(`7z x -bd -y "${tarball}"`, { cwd: nodeTmp });
await fs.move(path.join(nodeTmp, nodeBase, 'node.exe'), path.join(cache, 'node.exe'));
}
else {
await exec(`tar -C "${tmp}/node" -xJf "${tarball}"`);
await fs.move(path.join(nodeTmp, nodeBase, 'bin', 'node'), path.join(cache, 'node'));
}
};
if (!fs.existsSync(cache)) {
await download();
await extract();
}
await fs.copy(path.join(cache, getFilename(platform)), output);
return output;
}
exports.fetchNodeBinary = fetchNodeBinary;
const getFilename = (platform) => {
return platform === 'win32' ? 'node.exe' : 'node';
};