| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #!/usr/bin/env node
- /**
- * Download the prebuilt luatos-cli binary for the current platform.
- *
- * Usage:
- * node scripts/fetch-cli.mjs
- * node scripts/fetch-cli.mjs --version v1.11.0
- * node scripts/fetch-cli.mjs --platform darwin --arch arm64
- *
- * The binary is placed in resources/bin/ and marked executable on Unix.
- */
- import { createWriteStream } from 'node:fs';
- import fs from 'node:fs/promises';
- import os from 'node:os';
- import path from 'node:path';
- import { pipeline } from 'node:stream/promises';
- import { execFile } from 'node:child_process';
- import { promisify } from 'node:util';
- import { fileURLToPath } from 'node:url';
- const execFileAsync = promisify(execFile);
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
- const projectRoot = path.resolve(__dirname, '..');
- function parseArgs(argv) {
- const args = { version: 'v1.11.0', platform: process.platform, arch: process.arch };
- for (let i = 0; i < argv.length; i += 1) {
- const arg = argv[i];
- if (arg === '--version') args.version = argv[++i];
- else if (arg === '--platform') args.platform = argv[++i];
- else if (arg === '--arch') args.arch = argv[++i];
- else if (arg === '--help' || arg === '-h') {
- console.log('Usage: node scripts/fetch-cli.mjs [--version v1.11.0] [--platform darwin|win32|linux] [--arch arm64|x64]');
- process.exit(0);
- }
- }
- return args;
- }
- function assetFor(platform, arch) {
- if (platform === 'darwin') {
- // Universal binary works on both Intel and Apple Silicon.
- return 'luatos-cli-universal-apple-darwin.tar.gz';
- }
- if (platform === 'win32') {
- const a = arch === 'arm64' ? 'aarch64' : 'x86_64';
- return `luatos-cli-${a}-pc-windows-msvc.zip`;
- }
- if (platform === 'linux') {
- const a = arch === 'arm64' ? 'aarch64' : 'x86_64';
- return `luatos-cli-${a}-unknown-linux-gnu.tar.gz`;
- }
- throw new Error(`不支持的平台:${platform}(支持 darwin / win32 / linux)`);
- }
- async function download(url, destination) {
- console.log(`下载 ${url}`);
- try {
- const response = await fetch(url, { redirect: 'follow' });
- if (!response.ok || !response.body) {
- throw new Error(`HTTP ${response.status} ${response.statusText}`);
- }
- await pipeline(response.body, createWriteStream(destination));
- return;
- } catch (error) {
- // Some corporate proxies only work through the system curl. Fall back to
- // curl (macOS/Linux built-in, Windows 10+ ships curl.exe).
- console.log(`Node fetch 失败(${error.message}),尝试 curl…`);
- const curl = process.platform === 'win32' ? 'curl.exe' : 'curl';
- await fs.rm(destination, { force: true }).catch(() => {});
- await execFileAsync(
- curl,
- ['-L', '--fail', '--retry', '2', '--connect-timeout', '20', '-o', destination, url],
- { windowsHide: true, maxBuffer: 10 * 1024 * 1024 },
- );
- }
- }
- async function findBinary(root, fileName) {
- const entries = await fs.readdir(root, { withFileTypes: true });
- for (const entry of entries) {
- const full = path.join(root, entry.name);
- if (entry.isFile() && entry.name === fileName) return full;
- if (entry.isDirectory()) {
- const found = await findBinary(full, fileName).catch(() => null);
- if (found) return found;
- }
- }
- return null;
- }
- async function main() {
- const args = parseArgs(process.argv.slice(2));
- const asset = assetFor(args.platform, args.arch);
- const url = `https://github.com/wendal/luatos-cli/releases/download/${args.version}/${asset}`;
- const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'luatos-cli-'));
- const archive = path.join(tempDir, asset);
- try {
- await download(url, archive);
- console.log('解压…');
- await execFileAsync('tar', ['-xf', archive, '-C', tempDir], { windowsHide: true });
- const binaryName = args.platform === 'win32' ? 'luatos-cli.exe' : 'luatos-cli';
- const found = await findBinary(tempDir, binaryName);
- if (!found) throw new Error(`压缩包内没有找到 ${binaryName}`);
- const binDir = path.join(projectRoot, 'resources', args.platform === 'win32' ? 'win' : 'bin');
- await fs.mkdir(binDir, { recursive: true });
- const destination = path.join(binDir, binaryName);
- await fs.copyFile(found, destination);
- if (args.platform !== 'win32') await fs.chmod(destination, 0o755);
- console.log(`已安装:${destination}`);
- try {
- const { stdout } = await execFileAsync(destination, ['--version'], { windowsHide: true });
- console.log(stdout.trim());
- } catch (error) {
- console.warn(`已安装,但 --version 执行失败:${error.message}`);
- console.warn('macOS 上如果提示“无法验证开发者”,请在 App 内首次运行时允许,或执行:');
- console.warn(` xattr -cr "${destination}" && codesign --force --sign - "${destination}"`);
- }
- } finally {
- await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {});
- }
- }
- main().catch((error) => {
- console.error(`\n获取 luatos-cli 失败:${error.message}`);
- console.error('\n你可以手动下载:');
- console.error(' https://github.com/wendal/luatos-cli/releases');
- console.error('然后把 luatos-cli(macOS)或 luatos-cli.exe(Windows)放到 resources/bin/。');
- process.exit(1);
- });
|