build.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // 重命名构建产物,使用与 Electron 版本一致的命名格式
  132. function renameArtifacts(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. const version = getVersion();
  138. // 重命名 NSIS 安装程序
  139. const nsisDir = path.join(bundleDir, 'nsis');
  140. if (fs.existsSync(nsisDir)) {
  141. const files = fs.readdirSync(nsisDir).filter(f => f.endsWith('.exe'));
  142. for (const file of files) {
  143. const oldPath = path.join(nsisDir, file);
  144. // 新文件名格式: Claude-AI-Installer-{version}-win-x64-setup.exe
  145. const newName = `Claude-AI-Installer-${version}-win-x64-setup.exe`;
  146. const newPath = path.join(nsisDir, newName);
  147. if (file !== newName) {
  148. // 如果目标文件已存在,先删除
  149. if (fs.existsSync(newPath)) {
  150. fs.unlinkSync(newPath);
  151. }
  152. fs.renameSync(oldPath, newPath);
  153. log.success(`重命名: ${file} -> ${newName}`);
  154. }
  155. }
  156. }
  157. // 重命名 MSI 安装程序
  158. const msiDir = path.join(bundleDir, 'msi');
  159. if (fs.existsSync(msiDir)) {
  160. const files = fs.readdirSync(msiDir).filter(f => f.endsWith('.msi'));
  161. for (const file of files) {
  162. const oldPath = path.join(msiDir, file);
  163. // 新文件名格式: Claude-AI-Installer-{version}-win-x64.msi
  164. const newName = `Claude-AI-Installer-${version}-win-x64.msi`;
  165. const newPath = path.join(msiDir, newName);
  166. if (file !== newName) {
  167. // 如果目标文件已存在,先删除
  168. if (fs.existsSync(newPath)) {
  169. fs.unlinkSync(newPath);
  170. }
  171. fs.renameSync(oldPath, newPath);
  172. log.success(`重命名: ${file} -> ${newName}`);
  173. }
  174. }
  175. }
  176. }
  177. // 显示构建结果
  178. function showResults(options) {
  179. log.step('构建结果:');
  180. const mode = options.debug ? 'debug' : 'release';
  181. const targetDir = path.join(TAURI_DIR, 'target', mode);
  182. const bundleDir = path.join(targetDir, 'bundle');
  183. console.log('');
  184. // 显示可执行文件
  185. const exeName = 'claude-ai-installer.exe';
  186. const exePath = path.join(targetDir, exeName);
  187. if (fs.existsSync(exePath)) {
  188. const stats = fs.statSync(exePath);
  189. const sizeMB = (stats.size / (1024 * 1024)).toFixed(2);
  190. console.log(` ${colors.green}•${colors.reset} ${exeName} (${sizeMB} MB)`);
  191. log.info(` 路径: ${exePath}`);
  192. }
  193. // 显示 NSIS 安装程序
  194. const nsisDir = path.join(bundleDir, 'nsis');
  195. if (fs.existsSync(nsisDir)) {
  196. const files = fs.readdirSync(nsisDir).filter(f => f.endsWith('.exe'));
  197. for (const file of files) {
  198. const filePath = path.join(nsisDir, file);
  199. const stats = fs.statSync(filePath);
  200. const sizeMB = (stats.size / (1024 * 1024)).toFixed(2);
  201. console.log(` ${colors.green}•${colors.reset} nsis/${file} (${sizeMB} MB)`);
  202. }
  203. }
  204. // 显示 MSI 安装程序
  205. const msiDir = path.join(bundleDir, 'msi');
  206. if (fs.existsSync(msiDir)) {
  207. const files = fs.readdirSync(msiDir).filter(f => f.endsWith('.msi'));
  208. for (const file of files) {
  209. const filePath = path.join(msiDir, file);
  210. const stats = fs.statSync(filePath);
  211. const sizeMB = (stats.size / (1024 * 1024)).toFixed(2);
  212. console.log(` ${colors.green}•${colors.reset} msi/${file} (${sizeMB} MB)`);
  213. }
  214. }
  215. console.log('');
  216. log.info(`输出目录: ${targetDir}`);
  217. if (fs.existsSync(bundleDir)) {
  218. log.info(`安装包目录: ${bundleDir}`);
  219. }
  220. }
  221. // 主函数
  222. async function main() {
  223. const options = parseArgs();
  224. if (options.help) {
  225. showHelp();
  226. process.exit(0);
  227. }
  228. console.log(`
  229. ${colors.bright}╔════════════════════════════════════════════╗
  230. ║ Claude AI Installer 构建脚本 ║
  231. ║ (Tauri 2.0) ║
  232. ╚════════════════════════════════════════════╝${colors.reset}
  233. `);
  234. log.info(`构建模式: ${options.debug ? '调试' : '发布'}`);
  235. log.info(`版本: ${getVersion()}`);
  236. log.info(`产品名称: ${getProductName()}`);
  237. const startTime = Date.now();
  238. try {
  239. // 构建 Tauri 应用
  240. buildTauri(options);
  241. // 重命名构建产物(仅发布模式)
  242. if (!options.debug) {
  243. renameArtifacts(options);
  244. }
  245. // 显示结果
  246. showResults(options);
  247. const duration = ((Date.now() - startTime) / 1000).toFixed(1);
  248. log.success(`构建完成! 耗时: ${duration}s`);
  249. } catch (error) {
  250. log.error(error.message);
  251. process.exit(1);
  252. }
  253. }
  254. main();