|
1 | 1 | // @ts-check
|
2 | 2 | const { spawn } = require("child_process");
|
3 | 3 |
|
4 |
| -const spawnProcess = (command, args = [], options = {}) => |
5 |
| - new Promise((resolve, reject) => { |
6 |
| - try { |
7 |
| - const ls = spawn(command, args, options); |
8 |
| - ls.stdout.on("data", (data) => { |
9 |
| - console.log(data.toString()); |
10 |
| - }); |
11 |
| - ls.stderr.on("data", (data) => { |
12 |
| - console.error(`stderr: ${data.toString()}`); |
13 |
| - }); |
| 4 | +const spawnProcess = async (command, args = [], options = {}) => { |
| 5 | + const childProcess = spawn(command, args, options); |
14 | 6 |
|
15 |
| - ls.on("close", (code) => { |
16 |
| - console.log(`child process exited with code ${code}`); |
17 |
| - resolve(); |
18 |
| - }); |
19 |
| - } catch (e) { |
20 |
| - reject(e); |
21 |
| - } |
| 7 | + childProcess.stdout.pipe(process.stdout); |
| 8 | + childProcess.stderr.pipe(process.stderr); |
| 9 | + |
| 10 | + return new Promise((resolve, reject) => { |
| 11 | + childProcess.on("error", reject); |
| 12 | + childProcess.on("exit", (code, signal) => |
| 13 | + code === 0 ? resolve() : reject(`${command} failed with { code: ${code}, signal: ${signal} }`) |
| 14 | + ); |
22 | 15 | });
|
| 16 | +}; |
23 | 17 |
|
24 | 18 | module.exports = { spawnProcess };
|
0 commit comments