const { spawn } = require('child_process'); const path = require('path'); const fs = require('fs'); // 先编译 TypeScript 文件 const tsc = spawn('npx', ['tsc', '-p', 'tsconfig.electron.json'], { stdio: 'inherit', shell: true }); tsc.on('close', (code) => { if (code === 0) { // 编译成功后复制 preload.js 文件 const sourcePreload = path.join(__dirname, 'preload.js'); const destPreload = path.join(__dirname, 'dist', 'preload.js'); try { fs.copyFileSync(sourcePreload, destPreload); console.log('preload.js copied successfully'); } catch (err) { console.error('Failed to copy preload.js:', err); } // 编译成功后运行 Electron 应用 const electron = require('electron'); const appPath = path.join(__dirname, 'dist', 'main.js'); // 启动 Vite 开发服务器 const vite = spawn('npm', ['run', 'dev'], { stdio: 'inherit', shell: true, env: { ...process.env, VITE_DEV_SERVER_URL: 'http://localhost:5173', }, }); // 等待一段时间让 Vite 服务器启动,然后启动 Electron setTimeout(() => { const child = spawn(electron, [appPath], { stdio: 'inherit', env: { ...process.env, VITE_DEV_SERVER_URL: 'http://localhost:5173', }, }); child.on('close', (code) => { process.exit(code || 0); }); }, 5000); // 等待 5 秒钟让 Vite 服务器启动 } else { console.error('TypeScript compilation failed'); process.exit(code || 1); } });