| 17 | * `teen_process` basically does not respect `{stdio: 'inherit'}`. |
| 18 | */ |
| 19 | export async function spawnBackgroundProcess(command: string, args: string[], opts: SpawnBackgroundProcessOpts = {}) { |
| 20 | return new Promise<void>((resolve, reject) => { |
| 21 | spawn(command, args, {...opts, stdio: 'inherit'}) |
| 22 | .on('error', reject) |
| 23 | .on('close', (code, signal) => { |
| 24 | if (code === 0) { |
| 25 | return resolve(); |
| 26 | } |
| 27 | const reason = code === null ? `signal ${signal ?? 'unknown'}` : `code ${code}`; |
| 28 | reject(new Error(`${command} exited with ${reason}`)); |
| 29 | }); |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Wraps {@linkcode exec} with error handling that appends stderr to the thrown error message. |