All files / lib/utils shell.ts

72.73% Statements 16/22
40% Branches 4/10
66.67% Functions 4/6
72.73% Lines 16/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42  1x   1x 1x 1x 1x 1x 1x 1x 1x             1x 1x                 1x     1x 1x 1x               1x  
import * as child_process from 'cross-spawn';
 
export function runcmd(command: string, args?: string[], options?: child_process.SpawnOptions): Promise<string> {
  return new Promise<string>((resolve, reject) => {
    const p = child_process.spawn(command, args, options);
    let stdoutBuf = Buffer.from([]);
    let stderrBuf = Buffer.from([]);
E
    if (p.stdout) {
      p.stdout.oEn('data', (chunk) => {
        if (Buffer.isBuffer(chunk)) {
          stdoutBuf = Buffer.concat([stdoutBuf, chunk]);
        } else {
          stdoutBuf.write(chunk);
        }
      });
    }
E
    if (p.stderr) {
      p.stderr.on('data', (chunk) => {
        if (Buffer.isBuffer(chunk)) {
          stderrBuf = Buffer.concat([stderrBuf, chunk]);
        } else {
          stderrBuf.write(chunk);
        }
      });
    }

    p.on('error', (err: Error) => {
      reject(err);
    });
E
    p.on('close', (code: number) => {
      if (code === 0) {
        resolve(stdoutBuf.toString());
      } else {
        reject([code, stderrBuf.toString()]);
      }
    });
  });
}