fix: 修复打包编译自动化异常问题

This commit is contained in:
DEV_DSW
2026-03-19 16:55:31 +08:00
parent 37be5e8ba7
commit 0dc7f39db2
8 changed files with 90 additions and 56 deletions

View File

@@ -11,7 +11,7 @@ const openedTabIndexByChannelName = new Map<string, number>()
function getScriptsDir() {
return app.isPackaged
? path.join(process.resourcesPath, 'scripts')
? path.join(__dirname, 'scripts')
: path.join(process.cwd(), 'src/main/scripts')
}

View File

@@ -1,5 +1,5 @@
import { EventEmitter } from 'events';
import { spawn } from 'child_process';
import { utilityProcess } from 'electron';
import log from 'electron-log';
@@ -26,7 +26,7 @@ export class executeScriptService extends EventEmitter {
const channels = options?.channels ?? '';
const startTabIndex = options?.startTabIndex ?? '';
const child = spawn(process.execPath, [scriptPath], {
const child = utilityProcess.fork(scriptPath, [], {
env: {
...process.env,
ROOM_TYPE: String(roomType),
@@ -37,35 +37,31 @@ export class executeScriptService extends EventEmitter {
CHANNELS: typeof channels === 'string' ? channels : JSON.stringify(channels),
START_TAB_INDEX: String(startTabIndex),
},
stdio: 'inherit'
stdio: 'pipe'
});
let stdoutTail = '';
let stderrTail = '';
child.stdout?.on('data', (data: Buffer) => {
const text = data.toString();
stdoutTail = appendTail(stdoutTail, text);
log.info(`stdout: ${text}`);
});
child.stderr?.on('data', (data: Buffer) => {
const text = data.toString();
stderrTail = appendTail(stderrTail, text);
log.info(`stderr: ${text}`);
});
child.on('error', (err: any) => {
resolve({
success: false,
exitCode: null,
stdoutTail,
stderrTail,
error: err?.message || 'Failed to start script process',
if (child.stdout) {
child.stdout.on('data', (data: Buffer) => {
const text = data.toString();
stdoutTail = appendTail(stdoutTail, text);
log.info(`stdout: ${text}`);
});
});
}
child.on('close', (code: number | null) => {
if (child.stderr) {
child.stderr.on('data', (data: Buffer) => {
const text = data.toString();
stderrTail = appendTail(stderrTail, text);
log.info(`stderr: ${text}`);
});
}
// utilityProcess doesn't throw 'error' directly like child_process, but we can catch spawn errors or just resolve on exit
// Electron's utilityProcess emits 'exit' instead of 'close' for completion
child.on('exit', (code: number) => {
log.info(`子进程退出,退出码 ${code}`);
resolve({
success: code === 0,

View File

@@ -43,7 +43,9 @@ export async function launchLocalChrome() {
chromeProcess.on('error', reject);
// 等浏览器起来
resolve(0);
// 延迟几秒等浏览器起来
setTimeout(() => {
resolve(0);
}, 3000); // 延迟3秒
});
}