feat: 浏览器自动化操作开发
This commit is contained in:
38
src/main/service/execute-script-service/index.ts
Normal file
38
src/main/service/execute-script-service/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { EventEmitter } from 'events';
|
||||
import { spawn } from 'child_process';
|
||||
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 脚本时出错' };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user