« 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: ef8d0c672cc6b02d454fcc20a9f8d942a255f3ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'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 (or rejects with a warning, if the exit code is
  // non-zero).

  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)
      }
    })
  })
}