2019-03-02 15:19:08 +00:00
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
2019-03-03 22:51:52 +00:00
|
|
|
interface Options {
|
|
|
|
cwd?: string;
|
|
|
|
env?: {[key: string]: string};
|
|
|
|
debug?: boolean;
|
|
|
|
}
|
2019-03-02 15:19:08 +00:00
|
|
|
|
2019-03-03 22:51:52 +00:00
|
|
|
export function exec(command: string, options?: Options): Promise<void> {
|
2019-03-02 15:19:08 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-03-03 22:51:52 +00:00
|
|
|
const spawnOptions = {
|
2019-03-02 15:19:08 +00:00
|
|
|
shell: true,
|
2019-03-03 22:51:52 +00:00
|
|
|
} as any;
|
|
|
|
|
|
|
|
if (options && options.cwd) {
|
|
|
|
spawnOptions['cwd'] = options.cwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options && options.env) {
|
|
|
|
spawnOptions['env'] = {
|
|
|
|
...options.env,
|
|
|
|
...process.env,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options && options.debug) {
|
|
|
|
console.log('>>> ' + command);
|
|
|
|
}
|
|
|
|
const cmd = spawn(command, spawnOptions);
|
2019-03-02 15:19:08 +00:00
|
|
|
|
|
|
|
cmd.stdout.pipe(process.stdout);
|
|
|
|
cmd.stderr.pipe(process.stderr);
|
|
|
|
cmd.on('exit', (statusCode: number) => {
|
|
|
|
if (statusCode == 0) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-03 22:51:52 +00:00
|
|
|
reject(new Error('\'' + command + '\' exited with status code ' + statusCode));
|
2019-03-02 15:19:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|