fetch-cli.mjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env node
  2. /**
  3. * Download the prebuilt luatos-cli binary for the current platform.
  4. *
  5. * Usage:
  6. * node scripts/fetch-cli.mjs
  7. * node scripts/fetch-cli.mjs --version v1.11.0
  8. * node scripts/fetch-cli.mjs --platform darwin --arch arm64
  9. *
  10. * The binary is placed in resources/bin/ and marked executable on Unix.
  11. */
  12. import { createWriteStream } from 'node:fs';
  13. import fs from 'node:fs/promises';
  14. import os from 'node:os';
  15. import path from 'node:path';
  16. import { pipeline } from 'node:stream/promises';
  17. import { execFile } from 'node:child_process';
  18. import { promisify } from 'node:util';
  19. import { fileURLToPath } from 'node:url';
  20. const execFileAsync = promisify(execFile);
  21. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  22. const projectRoot = path.resolve(__dirname, '..');
  23. function parseArgs(argv) {
  24. const args = { version: 'v1.11.0', platform: process.platform, arch: process.arch };
  25. for (let i = 0; i < argv.length; i += 1) {
  26. const arg = argv[i];
  27. if (arg === '--version') args.version = argv[++i];
  28. else if (arg === '--platform') args.platform = argv[++i];
  29. else if (arg === '--arch') args.arch = argv[++i];
  30. else if (arg === '--help' || arg === '-h') {
  31. console.log('Usage: node scripts/fetch-cli.mjs [--version v1.11.0] [--platform darwin|win32|linux] [--arch arm64|x64]');
  32. process.exit(0);
  33. }
  34. }
  35. return args;
  36. }
  37. function assetFor(platform, arch) {
  38. if (platform === 'darwin') {
  39. // Universal binary works on both Intel and Apple Silicon.
  40. return 'luatos-cli-universal-apple-darwin.tar.gz';
  41. }
  42. if (platform === 'win32') {
  43. const a = arch === 'arm64' ? 'aarch64' : 'x86_64';
  44. return `luatos-cli-${a}-pc-windows-msvc.zip`;
  45. }
  46. if (platform === 'linux') {
  47. const a = arch === 'arm64' ? 'aarch64' : 'x86_64';
  48. return `luatos-cli-${a}-unknown-linux-gnu.tar.gz`;
  49. }
  50. throw new Error(`不支持的平台:${platform}(支持 darwin / win32 / linux)`);
  51. }
  52. async function download(url, destination) {
  53. console.log(`下载 ${url}`);
  54. try {
  55. const response = await fetch(url, { redirect: 'follow' });
  56. if (!response.ok || !response.body) {
  57. throw new Error(`HTTP ${response.status} ${response.statusText}`);
  58. }
  59. await pipeline(response.body, createWriteStream(destination));
  60. return;
  61. } catch (error) {
  62. // Some corporate proxies only work through the system curl. Fall back to
  63. // curl (macOS/Linux built-in, Windows 10+ ships curl.exe).
  64. console.log(`Node fetch 失败(${error.message}),尝试 curl…`);
  65. const curl = process.platform === 'win32' ? 'curl.exe' : 'curl';
  66. await fs.rm(destination, { force: true }).catch(() => {});
  67. await execFileAsync(
  68. curl,
  69. ['-L', '--fail', '--retry', '2', '--connect-timeout', '20', '-o', destination, url],
  70. { windowsHide: true, maxBuffer: 10 * 1024 * 1024 },
  71. );
  72. }
  73. }
  74. async function findBinary(root, fileName) {
  75. const entries = await fs.readdir(root, { withFileTypes: true });
  76. for (const entry of entries) {
  77. const full = path.join(root, entry.name);
  78. if (entry.isFile() && entry.name === fileName) return full;
  79. if (entry.isDirectory()) {
  80. const found = await findBinary(full, fileName).catch(() => null);
  81. if (found) return found;
  82. }
  83. }
  84. return null;
  85. }
  86. async function main() {
  87. const args = parseArgs(process.argv.slice(2));
  88. const asset = assetFor(args.platform, args.arch);
  89. const url = `https://github.com/wendal/luatos-cli/releases/download/${args.version}/${asset}`;
  90. const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'luatos-cli-'));
  91. const archive = path.join(tempDir, asset);
  92. try {
  93. await download(url, archive);
  94. console.log('解压…');
  95. await execFileAsync('tar', ['-xf', archive, '-C', tempDir], { windowsHide: true });
  96. const binaryName = args.platform === 'win32' ? 'luatos-cli.exe' : 'luatos-cli';
  97. const found = await findBinary(tempDir, binaryName);
  98. if (!found) throw new Error(`压缩包内没有找到 ${binaryName}`);
  99. const binDir = path.join(projectRoot, 'resources', args.platform === 'win32' ? 'win' : 'bin');
  100. await fs.mkdir(binDir, { recursive: true });
  101. const destination = path.join(binDir, binaryName);
  102. await fs.copyFile(found, destination);
  103. if (args.platform !== 'win32') await fs.chmod(destination, 0o755);
  104. console.log(`已安装:${destination}`);
  105. try {
  106. const { stdout } = await execFileAsync(destination, ['--version'], { windowsHide: true });
  107. console.log(stdout.trim());
  108. } catch (error) {
  109. console.warn(`已安装,但 --version 执行失败:${error.message}`);
  110. console.warn('macOS 上如果提示“无法验证开发者”,请在 App 内首次运行时允许,或执行:');
  111. console.warn(` xattr -cr "${destination}" && codesign --force --sign - "${destination}"`);
  112. }
  113. } finally {
  114. await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {});
  115. }
  116. }
  117. main().catch((error) => {
  118. console.error(`\n获取 luatos-cli 失败:${error.message}`);
  119. console.error('\n你可以手动下载:');
  120. console.error(' https://github.com/wendal/luatos-cli/releases');
  121. console.error('然后把 luatos-cli(macOS)或 luatos-cli.exe(Windows)放到 resources/bin/。');
  122. process.exit(1);
  123. });