UNPKG

@easy-install/easy-install

Version:
103 lines (102 loc) 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EI_BIN_PATH = exports.EI_DIR = exports.CLI_DIR = exports.DIR_NAME = exports.NAME = void 0; exports.getFetchOption = getFetchOption; exports.downloadBinary = downloadBinary; exports.getBinName = getBinName; exports.setupEi = setupEi; exports.runEi = runEi; const detect_targets_1 = require("detect-targets"); const path_1 = require("path"); const fs_1 = require("fs"); const child_process_1 = require("child_process"); const easy_archive_1 = require("@easy-install/easy-archive"); const os_1 = require("os"); exports.NAME = 'ei'; exports.DIR_NAME = '.easy-install'; exports.CLI_DIR = (0, path_1.join)((0, path_1.dirname)(process.argv[1]), exports.DIR_NAME).replaceAll('\\', '/'); exports.EI_DIR = (0, path_1.join)((0, os_1.homedir)(), exports.DIR_NAME).replaceAll('\\', '/'); exports.EI_BIN_PATH = (0, path_1.join)(exports.CLI_DIR, getBinName(exports.NAME)).replaceAll('\\', '/'); function getFetchOption() { const headers = { 'User-Agent': 'GitHub Actions', Connection: 'close', }; if (process.env.GITHUB_TOKEN) { headers.Authorization = `token ${process.env.GITHUB_TOKEN}`; } return { headers, }; } async function downloadBinary(url) { const response = await fetch(url, getFetchOption()); return await response.arrayBuffer(); } function getBinName(bin) { return process.platform === 'win32' && !bin.endsWith('.exe') && !bin.includes('.') ? `${bin}.exe` : bin; } const FilenameMap = { 'aarch64-apple-darwin': `ei-aarch64-apple-darwin.tar.gz`, 'x86_64-apple-darwin': `ei-x86_64-apple-darwin.tar.gz`, 'x86_64-pc-windows-gnu': `ei-x86_64-pc-windows-gnu.zip`, 'x86_64-pc-windows-msvc': `ei-x86_64-pc-windows-msvc.zip`, 'x86_64-unknown-linux-gnu': `ei-x86_64-unknown-linux-gnu.tar.gz`, 'x86_64-unknown-linux-musl': `ei-x86_64-unknown-linux-musl.tar.gz`, }; function getUrl(target) { const v = FilenameMap[target]; if (!v) { console.log('Not support target: ' + target); return; } return `https://github.com/easy-install/easy-install/releases/latest/download/${v}`; } async function setupEi() { for (const t of (0, detect_targets_1.detectTargets)()) { const url = getUrl(t); if (!url) { continue; } const bin = new Uint8Array(await downloadBinary(url)); const fmt = (0, easy_archive_1.guess)(url); if (fmt === undefined) { continue; } const file = (0, easy_archive_1.decode)(fmt, bin)?.[0]; if (!file) { continue; } const { mode = 0, buffer } = file; const dir = (0, path_1.dirname)(exports.EI_BIN_PATH); if (!(0, fs_1.existsSync)(dir)) { (0, fs_1.mkdirSync)(dir, { recursive: true }); } (0, fs_1.writeFileSync)(exports.EI_BIN_PATH, buffer); if (mode) { (0, fs_1.chmodSync)(exports.EI_BIN_PATH, mode); } return exports.EI_BIN_PATH; } } async function runEi(args = process.argv.slice(2), quiet = false) { if (!(0, fs_1.existsSync)(exports.EI_BIN_PATH)) { const r = await setupEi(); if (!r) { throw new Error('Failed to prepare'); } } try { (0, child_process_1.execFileSync)(exports.EI_BIN_PATH, args, { stdio: quiet ? 'ignore' : 'inherit', cwd: process.cwd(), }); } catch (e) { // FIXME: Ignore js errors // console.log(e) } }