|
- import { spawn } from 'node:child_process';
-
- export function startProcess(command, args, options = {}) {
- const child = spawn(command, args, {
- cwd: options.cwd,
- env: { ...process.env, ...(options.env || {}) },
- shell: process.platform === 'win32',
- stdio: ['ignore', 'pipe', 'pipe'],
- });
-
- const output = [];
- child.stdout.on('data', (chunk) => output.push(chunk.toString()));
- child.stderr.on('data', (chunk) => output.push(chunk.toString()));
-
- child.once('exit', (code) => {
- if (code !== null && code !== 0) {
- output.push(`\nprocess exited with code ${code}\n`);
- }
- });
-
- return { child, output };
- }
-
- export async function stopProcess(child) {
- if (!child || child.killed) return;
- await new Promise((resolve) => {
- child.once('exit', resolve);
- child.kill(process.platform === 'win32' ? undefined : 'SIGTERM');
- setTimeout(() => {
- if (!child.killed) child.kill('SIGKILL');
- resolve();
- }, 5000).unref();
- });
- }
|