« get me outta code hell

promisify-process.js « src - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/promisify-process.js
blob: ca49b31ffc258c6e4b25b851f8d91a0ca1d6ccec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'use strict'

module.exports = function promisifyProcess(proc, showLogging = true) {
  // Takes a process (from child_process) and returns a promise that resolves
  // when the process exits.

  return new Promise((resolve, reject) => {
    if (showLogging) {
      proc.stdout.pipe(process.stdout)
      proc.stderr.pipe(process.stderr)
    }

    proc.on('exit', code => {
      if (code === 0) {
        resolve()
      } else {
        console.error('Process failed!', proc.spawnargs)
        reject(code)
      }
    })
  })
}