feat: 飞猪自动化调试完成
This commit is contained in:
@@ -5,34 +5,73 @@ import log from 'electron-log';
|
||||
|
||||
export class executeScriptService extends EventEmitter {
|
||||
// 执行脚本
|
||||
async executeScript(scriptPath: string, options: object): Promise<{ success: boolean; message?: string; error?: string }> {
|
||||
try {
|
||||
const child = spawn('node', [scriptPath], {
|
||||
env: {
|
||||
...process.env,
|
||||
...options,
|
||||
}
|
||||
});
|
||||
|
||||
child.stdout.on('data', (data: Buffer) => {
|
||||
log.info(`stdout: ${data.toString()}`);
|
||||
});
|
||||
|
||||
child.stderr.on('data', (data: Buffer) => {
|
||||
log.info(`stderr: ${data.toString()}`);
|
||||
});
|
||||
|
||||
child.on('close', (code: number) => {
|
||||
log.info(`子进程退出,退出码 ${code}`);
|
||||
});
|
||||
|
||||
return { success: true, message: 'Node 脚本执行中' };
|
||||
} catch (error) {
|
||||
return { success: false, message: '运行 Node 脚本时出错' };
|
||||
}
|
||||
async executeScript(
|
||||
scriptPath: string,
|
||||
options: Record<string, any>,
|
||||
): Promise<{ success: boolean; exitCode: number | null; stdoutTail: string; stderrTail: string; error?: string }> {
|
||||
const MAX_TAIL = 32 * 1024;
|
||||
|
||||
const appendTail = (current: string, chunk: string) => {
|
||||
const next = current + chunk;
|
||||
return next.length > MAX_TAIL ? next.slice(next.length - MAX_TAIL) : next;
|
||||
};
|
||||
|
||||
return await new Promise((resolve) => {
|
||||
try {
|
||||
const child = spawn('node', [scriptPath], {
|
||||
env: {
|
||||
...process.env,
|
||||
...options,
|
||||
}
|
||||
});
|
||||
|
||||
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',
|
||||
});
|
||||
});
|
||||
|
||||
child.on('close', (code: number | null) => {
|
||||
log.info(`子进程退出,退出码 ${code}`);
|
||||
resolve({
|
||||
success: code === 0,
|
||||
exitCode: code,
|
||||
stdoutTail,
|
||||
stderrTail,
|
||||
...(code === 0 ? {} : { error: `Script exited with code ${code}` }),
|
||||
});
|
||||
});
|
||||
} catch (error: any) {
|
||||
resolve({
|
||||
success: false,
|
||||
exitCode: null,
|
||||
stdoutTail: '',
|
||||
stderrTail: '',
|
||||
error: error?.message || '运行 Node 脚本时出错',
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user