|
3 | 3 | const { spawn } = require('child_process')
|
4 | 4 | const { EOL } = require('os')
|
5 | 5 |
|
6 |
| -const EE_PROPS = Object.getOwnPropertyNames( |
7 |
| - require('events').EventEmitter.prototype |
8 |
| -) |
| 6 | +const EE_PROPS = Object.getOwnPropertyNames(require('events').EventEmitter.prototype) |
9 | 7 | .filter(name => !name.startsWith('_'))
|
10 | 8 | .concat(['kill', 'ref', 'unref'])
|
11 | 9 |
|
12 | 10 | const eos = (stream, listener, buffer = []) =>
|
13 |
| - stream[listener] |
14 |
| - ? stream[listener].on('data', data => buffer.push(data)) && buffer |
15 |
| - : buffer |
| 11 | + stream[listener] ? stream[listener].on('data', data => buffer.push(data)) && buffer : buffer |
| 12 | + |
| 13 | +const createChildProcessError = ({ cmd, cmdArgs, exitCode, stderr, childProcess }) => { |
| 14 | + const command = `${cmd} ${cmdArgs.join(' ')}` |
| 15 | + let message = `The command spawned as:${EOL}${EOL}` |
| 16 | + message += ` ${command}${EOL}${EOL}` |
| 17 | + message += `exited with \`{ code: ${exitCode} }\` and the following trace:${EOL}${EOL}` |
| 18 | + message += String(stderr) |
| 19 | + .split(EOL) |
| 20 | + .map(line => ` ${line}`) |
| 21 | + .join(EOL) |
| 22 | + const error = new Error(message) |
| 23 | + error.command = command |
| 24 | + error.name = 'ChildProcessError' |
| 25 | + Object.keys(childProcess).forEach(key => { |
| 26 | + error[key] = childProcess[key] |
| 27 | + }) |
| 28 | + return error |
| 29 | +} |
16 | 30 |
|
17 | 31 | const clean = str => str.trim().replace(/\n$/, '')
|
18 | 32 |
|
@@ -42,30 +56,15 @@ const extend = defaults => (input, args, options) => {
|
42 | 56 | get: parse(stdout, opts)
|
43 | 57 | })
|
44 | 58 | Object.defineProperty(childProcess, 'stderr', { get: parse(stderr) })
|
45 |
| - if (exitCode === 0) return resolve(childProcess) |
46 |
| - const command = `${cmd} ${cmdArgs.join(' ')}` |
47 |
| - let message = `The command spawned as:${EOL}${EOL}` |
48 |
| - message += ` ${command}${EOL}${EOL}` |
49 |
| - message += `exited with \`{ code: ${exitCode} }\` and the following trace:${EOL}${EOL}` |
50 |
| - message += String(stderr) |
51 |
| - .split(EOL) |
52 |
| - .map(line => ` ${line}`) |
53 |
| - .join(EOL) |
54 |
| - const error = new Error(message) |
55 |
| - error.command = command |
56 |
| - error.name = 'ChildProcessError' |
57 |
| - Object.keys(childProcess).forEach(key => { |
58 |
| - error[key] = childProcess[key] |
59 |
| - }) |
60 |
| - reject(error) |
| 59 | + return exitCode === 0 |
| 60 | + ? resolve(childProcess) |
| 61 | + : reject(createChildProcessError({ cmd, cmdArgs, exitCode, stderr, childProcess })) |
61 | 62 | })
|
62 | 63 | })
|
63 | 64 |
|
64 | 65 | const subprocess = Object.assign(promise, childProcess)
|
65 | 66 | if (childProcess) {
|
66 |
| - EE_PROPS.forEach( |
67 |
| - name => (subprocess[name] = childProcess[name].bind(childProcess)) |
68 |
| - ) |
| 67 | + EE_PROPS.forEach(name => (subprocess[name] = childProcess[name].bind(childProcess))) |
69 | 68 | }
|
70 | 69 | return subprocess
|
71 | 70 | }
|
|
0 commit comments