39 lines
1.0 KiB
JavaScript
39 lines
1.0 KiB
JavaScript
const { spawn } = require('child_process');
|
|
|
|
function spawnAsync(command, args = [], options = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const child = spawn(command, args, options);
|
|
let stdout = '';
|
|
let stderr = '';
|
|
|
|
if (child.stdout) {
|
|
child.stdout.on('data', (data) => {
|
|
stdout += data.toString();
|
|
});
|
|
}
|
|
|
|
if (child.stderr) {
|
|
child.stderr.on('data', (data) => {
|
|
stderr += data.toString();
|
|
});
|
|
}
|
|
|
|
child.on('error', (error) => {
|
|
reject(error);
|
|
});
|
|
|
|
child.on('close', (code) => {
|
|
if (code === 0) {
|
|
resolve({ stdout, stderr });
|
|
} else {
|
|
const error = new Error(`Command failed with code ${code}: ${stderr || stdout}`);
|
|
error.code = code;
|
|
error.stdout = stdout;
|
|
error.stderr = stderr;
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = { spawnAsync }; |