|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const denodeify = require('denodeify'); |
| 4 | +const chalk = require('chalk'); |
| 5 | +const childProcess = require('child_process'); |
| 6 | +const spawn = childProcess.spawn; |
| 7 | +const defaults = require('lodash/defaults'); |
| 8 | +const killCliProcess = require('./kill-cli-process'); |
| 9 | +const debug = require('./debug'); |
| 10 | +const logOnFailure = require('./log-on-failure'); |
| 11 | +const exec = denodeify(childProcess.exec); |
| 12 | + |
| 13 | +const isWindows = process.platform === 'win32'; |
| 14 | + |
| 15 | +module.exports = function run(/* command, args, options */) { |
| 16 | + let command = arguments[0]; |
| 17 | + let args = Array.prototype.slice.call(arguments, 1); |
| 18 | + let options = {}; |
| 19 | + |
| 20 | + if (typeof args[args.length - 1] === 'object') { |
| 21 | + options = args.pop(); |
| 22 | + } |
| 23 | + |
| 24 | + debug('running command=' + command + '; args=' + args + '; cwd=' + process.cwd()); |
| 25 | + |
| 26 | + if (isWindows && (command === 'npm' || command === 'bower')) { |
| 27 | + return exec(command + ' ' + args.join(' ')); |
| 28 | + } |
| 29 | + |
| 30 | + options = defaults(options, { |
| 31 | + |
| 32 | + onOutput(string) { |
| 33 | + options.log(string); |
| 34 | + }, |
| 35 | + |
| 36 | + onError(string) { |
| 37 | + options.log(chalk.red(string)); |
| 38 | + }, |
| 39 | + |
| 40 | + log(string) { |
| 41 | + debug(string); |
| 42 | + |
| 43 | + logOnFailure(string); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + return new Promise((resolve, reject) => { |
| 48 | + let opts = {}; |
| 49 | + if (isWindows) { |
| 50 | + args = ['"' + command + '"'].concat(args); |
| 51 | + command = 'node'; |
| 52 | + opts.windowsVerbatimArguments = true; |
| 53 | + opts.stdio = [null, null, null, 'ipc']; |
| 54 | + } |
| 55 | + let child = spawn(command, args, opts); |
| 56 | + let result = { |
| 57 | + output: [], |
| 58 | + errors: [], |
| 59 | + code: null |
| 60 | + }; |
| 61 | + |
| 62 | + if (options.onChildSpawned) { |
| 63 | + let onChildSpawnedPromise = new Promise((childSpawnedResolve, childSpawnedReject) => { |
| 64 | + try { |
| 65 | + options.onChildSpawned(child).then(childSpawnedResolve, childSpawnedReject); |
| 66 | + } catch (err) { |
| 67 | + childSpawnedReject(err); |
| 68 | + } |
| 69 | + }); |
| 70 | + onChildSpawnedPromise |
| 71 | + .then(() => { |
| 72 | + if (options.killAfterChildSpawnedPromiseResolution) { |
| 73 | + killCliProcess(child); |
| 74 | + } |
| 75 | + }, err => { |
| 76 | + result.testingError = err; |
| 77 | + if (options.killAfterChildSpawnedPromiseResolution) { |
| 78 | + killCliProcess(child); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + child.stdout.on('data', data => { |
| 84 | + let string = data.toString(); |
| 85 | + |
| 86 | + options.onOutput(string, child); |
| 87 | + |
| 88 | + result.output.push(string); |
| 89 | + }); |
| 90 | + |
| 91 | + child.stderr.on('data', data => { |
| 92 | + let string = data.toString(); |
| 93 | + |
| 94 | + options.onError(string, child); |
| 95 | + |
| 96 | + result.errors.push(string); |
| 97 | + }); |
| 98 | + |
| 99 | + child.on('close', (code, signal) => { |
| 100 | + result.code = code; |
| 101 | + result.signal = signal; |
| 102 | + |
| 103 | + if (code === 0) { |
| 104 | + resolve(result); |
| 105 | + } else { |
| 106 | + reject(result); |
| 107 | + } |
| 108 | + }); |
| 109 | + }); |
| 110 | +}; |
0 commit comments