/** * 跨平台启动 electron-vite,并清除 Cursor/VS Code 注入的 * ELECTRON_RUN_AS_NODE / ATOM_SHELL_INTERNAL_RUN_AS_NODE(否则 Electron 会当 Node 跑)。 * * Windows 上用 spawn + taskkill /T,避免终端关掉后留下无 Vite 的孤儿 Electron(热更新假死)。 */ import { spawn } from 'child_process' import { dirname, join } from 'path' import { fileURLToPath } from 'url' const root = join(dirname(fileURLToPath(import.meta.url)), '..') const cli = join(root, 'node_modules/electron-vite/bin/electron-vite.js') const env = { ...process.env } delete env.ELECTRON_RUN_AS_NODE delete env.ATOM_SHELL_INTERNAL_RUN_AS_NODE if (process.platform === 'win32') { env.CHOKIDAR_USEPOLLING = '1' } const child = spawn(process.execPath, [cli, ...process.argv.slice(2)], { stdio: 'inherit', env, cwd: root, shell: false, windowsHide: false }) let shuttingDown = false function killTree() { if (shuttingDown || !child.pid) return shuttingDown = true if (process.platform === 'win32') { spawn('taskkill', ['/PID', String(child.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true }) } else { try { child.kill('SIGTERM') } catch { /* ignore */ } } } for (const sig of ['SIGINT', 'SIGTERM', 'SIGHUP']) { process.on(sig, () => { killTree() process.exit(0) }) } process.on('exit', killTree) child.on('error', (err) => { console.error('[run-electron-vite]', err) process.exit(1) }) child.on('exit', (code, signal) => { shuttingDown = true if (signal) process.exit(1) process.exit(code ?? 1) })