build.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/usr/bin/env node
  2. /**
  3. * Tauri 构建脚本
  4. * Build script for Claude AI Installer (Tauri)
  5. *
  6. * 用法 / Usage:
  7. * node scripts/build.js [options]
  8. *
  9. * 选项 / Options:
  10. * --debug, -d 调试模式构建
  11. * --skip-frontend 跳过前端构建
  12. * --help, -h 显示帮助信息
  13. */
  14. import { execSync } from 'child_process';
  15. import path from 'path';
  16. import fs from 'fs';
  17. import { fileURLToPath } from 'url';
  18. const __filename = fileURLToPath(import.meta.url);
  19. const __dirname = path.dirname(__filename);
  20. // 颜色输出
  21. const colors = {
  22. reset: '\x1b[0m',
  23. bright: '\x1b[1m',
  24. red: '\x1b[31m',
  25. green: '\x1b[32m',
  26. yellow: '\x1b[33m',
  27. blue: '\x1b[34m',
  28. cyan: '\x1b[36m',
  29. };
  30. const log = {
  31. info: (msg) => console.log(`${colors.blue}ℹ${colors.reset} ${msg}`),
  32. success: (msg) => console.log(`${colors.green}✔${colors.reset} ${msg}`),
  33. warn: (msg) => console.log(`${colors.yellow}⚠${colors.reset} ${msg}`),
  34. error: (msg) => console.log(`${colors.red}✖${colors.reset} ${msg}`),
  35. step: (msg) => console.log(`\n${colors.cyan}${colors.bright}▶ ${msg}${colors.reset}`),
  36. };
  37. // 项目路径
  38. const PROJECT_ROOT = path.resolve(__dirname, '..');
  39. const TAURI_DIR = path.join(PROJECT_ROOT, 'src-tauri');
  40. // 解析命令行参数
  41. function parseArgs() {
  42. const args = process.argv.slice(2);
  43. const options = {
  44. debug: false,
  45. skipFrontend: false,
  46. help: false,
  47. };
  48. for (let i = 0; i < args.length; i++) {
  49. const arg = args[i];
  50. switch (arg) {
  51. case '--debug':
  52. case '-d':
  53. options.debug = true;
  54. break;
  55. case '--skip-frontend':
  56. case '-s':
  57. options.skipFrontend = true;
  58. break;
  59. case '--help':
  60. case '-h':
  61. options.help = true;
  62. break;
  63. }
  64. }
  65. return options;
  66. }
  67. // 显示帮助信息
  68. function showHelp() {
  69. console.log(`
  70. ${colors.bright}Claude AI Installer 构建脚本 (Tauri)${colors.reset}
  71. ${colors.cyan}用法:${colors.reset}
  72. node scripts/build.js [options]
  73. ${colors.cyan}选项:${colors.reset}
  74. --debug, -d 以调试模式构建
  75. --skip-frontend, -s 跳过前端构建
  76. --help, -h 显示此帮助信息
  77. ${colors.cyan}示例:${colors.reset}
  78. node scripts/build.js # 完整发布构建
  79. node scripts/build.js --debug # 调试构建
  80. node scripts/build.js -s # 跳过前端,仅构建 Tauri
  81. ${colors.cyan}输出 (发布模式):${colors.reset}
  82. src-tauri/target/release/bundle/
  83. ├── nsis/*.exe # NSIS 安装程序
  84. └── msi/*.msi # MSI 安装程序
  85. src-tauri/target/release/
  86. └── claude-ai-installer.exe # 可执行文件
  87. ${colors.cyan}输出 (调试模式):${colors.reset}
  88. src-tauri/target/debug/
  89. └── claude-ai-installer.exe # 调试版可执行文件
  90. `);
  91. }
  92. // 执行命令
  93. function exec(command, options = {}) {
  94. log.info(`执行: ${command}`);
  95. try {
  96. execSync(command, {
  97. stdio: 'inherit',
  98. cwd: PROJECT_ROOT,
  99. ...options,
  100. });
  101. return true;
  102. } catch (error) {
  103. log.error(`命令执行失败: ${command}`);
  104. return false;
  105. }
  106. }
  107. // 获取版本号
  108. function getVersion() {
  109. const tauriConfPath = path.join(TAURI_DIR, 'tauri.conf.json');
  110. const tauriConf = JSON.parse(fs.readFileSync(tauriConfPath, 'utf8'));
  111. return tauriConf.version;
  112. }
  113. // 获取产品名称
  114. function getProductName() {
  115. const tauriConfPath = path.join(TAURI_DIR, 'tauri.conf.json');
  116. const tauriConf = JSON.parse(fs.readFileSync(tauriConfPath, 'utf8'));
  117. return tauriConf.productName || 'Claude AI Installer';
  118. }
  119. // 构建 Tauri 应用
  120. function buildTauri(options) {
  121. log.step(`构建 Tauri 应用 (${options.debug ? '调试' : '发布'}模式)...`);
  122. let command = 'npm run tauri build';
  123. if (options.debug) {
  124. command += ' -- --debug';
  125. }
  126. if (!exec(command)) {
  127. throw new Error('Tauri 构建失败');
  128. }
  129. log.success('Tauri 构建完成');
  130. }
  131. // 显示构建结果
  132. function showResults(options) {
  133. log.step('构建结果:');
  134. const mode = options.debug ? 'debug' : 'release';
  135. const targetDir = path.join(TAURI_DIR, 'target', mode);
  136. const bundleDir = path.join(targetDir, 'bundle');
  137. console.log('');
  138. // 显示可执行文件
  139. const exeName = 'claude-ai-installer.exe';
  140. const exePath = path.join(targetDir, exeName);
  141. if (fs.existsSync(exePath)) {
  142. const stats = fs.statSync(exePath);
  143. const sizeMB = (stats.size / (1024 * 1024)).toFixed(2);
  144. console.log(` ${colors.green}•${colors.reset} ${exeName} (${sizeMB} MB)`);
  145. log.info(` 路径: ${exePath}`);
  146. }
  147. // 显示 NSIS 安装程序
  148. const nsisDir = path.join(bundleDir, 'nsis');
  149. if (fs.existsSync(nsisDir)) {
  150. const files = fs.readdirSync(nsisDir).filter(f => f.endsWith('.exe'));
  151. for (const file of files) {
  152. const filePath = path.join(nsisDir, file);
  153. const stats = fs.statSync(filePath);
  154. const sizeMB = (stats.size / (1024 * 1024)).toFixed(2);
  155. console.log(` ${colors.green}•${colors.reset} nsis/${file} (${sizeMB} MB)`);
  156. }
  157. }
  158. // 显示 MSI 安装程序
  159. const msiDir = path.join(bundleDir, 'msi');
  160. if (fs.existsSync(msiDir)) {
  161. const files = fs.readdirSync(msiDir).filter(f => f.endsWith('.msi'));
  162. for (const file of files) {
  163. const filePath = path.join(msiDir, file);
  164. const stats = fs.statSync(filePath);
  165. const sizeMB = (stats.size / (1024 * 1024)).toFixed(2);
  166. console.log(` ${colors.green}•${colors.reset} msi/${file} (${sizeMB} MB)`);
  167. }
  168. }
  169. console.log('');
  170. log.info(`输出目录: ${targetDir}`);
  171. if (fs.existsSync(bundleDir)) {
  172. log.info(`安装包目录: ${bundleDir}`);
  173. }
  174. }
  175. // 主函数
  176. async function main() {
  177. const options = parseArgs();
  178. if (options.help) {
  179. showHelp();
  180. process.exit(0);
  181. }
  182. console.log(`
  183. ${colors.bright}╔════════════════════════════════════════════╗
  184. ║ Claude AI Installer 构建脚本 ║
  185. ║ (Tauri 2.0) ║
  186. ╚════════════════════════════════════════════╝${colors.reset}
  187. `);
  188. log.info(`构建模式: ${options.debug ? '调试' : '发布'}`);
  189. log.info(`版本: ${getVersion()}`);
  190. log.info(`产品名称: ${getProductName()}`);
  191. const startTime = Date.now();
  192. try {
  193. // 构建 Tauri 应用
  194. buildTauri(options);
  195. // 显示结果
  196. showResults(options);
  197. const duration = ((Date.now() - startTime) / 1000).toFixed(1);
  198. log.success(`构建完成! 耗时: ${duration}s`);
  199. } catch (error) {
  200. log.error(error.message);
  201. process.exit(1);
  202. }
  203. }
  204. main();