All files / src/util shell.js

55.17% Statements 16/29
21.43% Branches 3/14
50% Functions 4/8
55.56% Lines 15/27
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59    1x     1x 1x 1x   1x   1x   1x   1x                                                                     1x             1x  
import { spawn } from 'child_process'
import { stop } from 'simple-spinner'
import { exec as cmd, cd } from 'shelljs'
 
const defaultOptions = {
  stdio: 'inherit',
  env: Object.create(process.env),
  cwd: process.env.NORTHBROOK_EXEC_DIR || process.cwd(),
  detached: true
}
 
/**
 * executes a command asynchronously that shares the stdin and stdout
 */
export function exec (command, args, _options = defaultOptions) {
  const options = Object.assign({}, defaultOptions, _options)
  return new Promise((resolve) => {
    if (options && options.start) {
      process.stdout.write(options.start)
    }
 
    process.nextTick(() => {
      const child = spawn(command, args, options)
 
      child.on('close', (code) => {
        stop()
        if (options && options.stop) {
          process.stdout.write(options.stop)
        }
        resolve({
          code, out: '', err: ''
        })
      })
    })
  })
}
 
const defaultPOptions = Object.assign({}, defaultOptions, { stdio: 'pipe', silent: true })

/**
 * execute a command asynchronously without sharing stdin and stdiout
 * @type {Object}
 */
export function execp (command, _options = defaultPOptions) {
  const options = Object.assign({}, defaultPOptions, _options)
  return new Promise((resolve, reject) => {
    cmd(command, options, function (code, out, err) {
      resolve({ code, err, out })
    })
  })
}
 
/**
 *  change the current directory
 */
export function chdir (path) {
  cd(path)
}E