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