Files
LoveLiveMusicPlayer-Next/scripts/run-electron-vite.mjs
2026-08-08 23:41:19 +08:00

64 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 跨平台启动 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)
})