adapt windows

This commit is contained in:
2026-08-08 23:41:19 +08:00
parent 1fa69aa055
commit 9455c1d57e
20 changed files with 433 additions and 80 deletions

View File

@@ -0,0 +1,63 @@
/**
* 跨平台启动 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)
})