|
1 | | -import spawn from 'spawn-command' |
2 | | -import async from 'async' |
| 1 | +import spawn from 'spawn-command-with-kill' |
| 2 | +import Promise from 'bluebird' |
3 | 3 | import colors from 'colors/safe' |
4 | | -import {isString, find, clone} from 'lodash' |
| 4 | +import {isString, clone} from 'lodash' |
5 | 5 | import {sync as findUpSync} from 'find-up' |
6 | 6 | import managePath from 'manage-path' |
7 | 7 | import arrify from 'arrify' |
8 | 8 | import getScriptToRun from './get-script-to-run' |
9 | 9 | import getScriptsFromConfig from './get-scripts-from-config' |
10 | 10 | import getLogger from './get-logger' |
11 | 11 |
|
12 | | -const noop = () => {} // eslint-disable-line func-style, no-empty-function |
| 12 | +const NON_ERROR = 0 |
13 | 13 |
|
14 | 14 | export default runPackageScripts |
15 | 15 |
|
16 | | -function runPackageScripts({scriptConfig, scripts, args, options = {}}, callback = noop) { |
| 16 | +function runPackageScripts({scriptConfig, scripts, args, options = {}}) { |
17 | 17 | if (scripts.length === 0) { |
18 | 18 | scripts = ['default'] |
19 | 19 | } |
20 | 20 | const scriptNames = arrify(scripts) |
21 | | - const method = options.parallel ? 'map' : 'mapSeries' |
22 | | - async[method](scriptNames, (scriptName, cb) => { |
23 | | - const child = runPackageScript({scriptConfig, options, scriptName, args}) |
24 | | - if (child.on) { |
25 | | - child.on('exit', exitCode => cb(null, exitCode)) |
26 | | - } else { |
27 | | - cb(child) |
28 | | - } |
29 | | - }, (err, results) => { |
30 | | - if (err) { |
31 | | - callback({error: err}) |
32 | | - } else { |
33 | | - const NON_ERROR = 0 |
34 | | - const result = find(results, r => r !== NON_ERROR) |
35 | | - callback({code: result}) |
| 21 | + if (options.parallel) { |
| 22 | + return runParallel() |
| 23 | + } else { |
| 24 | + return runSeries() |
| 25 | + } |
| 26 | + |
| 27 | + function runSeries() { |
| 28 | + return scriptNames.reduce((res, scriptName) => { |
| 29 | + return res.then(() => ( |
| 30 | + runPackageScript({scriptConfig, options, scriptName, args}) |
| 31 | + )) |
| 32 | + }, Promise.resolve()) |
| 33 | + } |
| 34 | + |
| 35 | + function runParallel() { |
| 36 | + const results = scriptNames.map(script => ({script, code: undefined})) |
| 37 | + let aborted = false |
| 38 | + |
| 39 | + const promises = scriptNames.map(scriptName => { |
| 40 | + return runPackageScript({scriptConfig, options, scriptName, args}) |
| 41 | + }) |
| 42 | + |
| 43 | + const allPromise = Promise.all(promises.map((promise, index) => { |
| 44 | + return promise.then(code => { |
| 45 | + if (!aborted) { |
| 46 | + results[index].code = code |
| 47 | + } |
| 48 | + }) |
| 49 | + })).then(() => results) |
| 50 | + |
| 51 | + allPromise.catch(() => { |
| 52 | + /* istanbul ignore if */ |
| 53 | + if (aborted) { |
| 54 | + // this is very unlikely to happen |
| 55 | + } else { |
| 56 | + abortAll() |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + return allPromise |
| 61 | + |
| 62 | + function abortAll() { |
| 63 | + aborted = true |
| 64 | + promises.forEach(p => p.abort()) |
36 | 65 | } |
37 | | - }) |
| 66 | + } |
38 | 67 | } |
39 | 68 |
|
| 69 | + |
40 | 70 | function runPackageScript({scriptConfig, options, scriptName, args}) { |
41 | 71 | const scripts = getScriptsFromConfig(scriptConfig, scriptName) |
42 | 72 | const script = getScriptToRun(scripts, scriptName) |
43 | 73 | if (!isString(script)) { |
44 | | - return { |
| 74 | + return Promise.reject({ |
45 | 75 | message: colors.red( |
46 | 76 | `Scripts must resolve to strings. There is no script that can be resolved from "${scriptName}"` |
47 | 77 | ), |
48 | 78 | ref: 'missing-script', |
49 | | - } |
| 79 | + }) |
50 | 80 | } |
51 | 81 | const command = [script, args].join(' ').trim() |
52 | 82 | const log = getLogger(getLogLevel(options)) |
53 | 83 | log.info(colors.gray('p-s executing: ') + colors.green(command)) |
54 | | - return spawn(command, {stdio: 'inherit', env: getEnv()}) |
| 84 | + let child |
| 85 | + const promise = new Promise((resolve, reject) => { |
| 86 | + child = spawn(command, {stdio: 'inherit', env: getEnv()}) |
| 87 | + |
| 88 | + child.on('error', error => { |
| 89 | + child = null |
| 90 | + reject({ |
| 91 | + message: colors.red( |
| 92 | + `The script called "${scriptName}" which runs "${command}" emitted an error` |
| 93 | + ), |
| 94 | + ref: 'emitted-an-error', |
| 95 | + error, |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + child.on('close', code => { |
| 100 | + child = null |
| 101 | + if (code === NON_ERROR) { |
| 102 | + resolve(code) |
| 103 | + } else { |
| 104 | + reject({ |
| 105 | + message: colors.red( |
| 106 | + `The script called "${scriptName}" which runs "${command}" failed with exit code ${code}` |
| 107 | + ), |
| 108 | + ref: 'failed-with-exit-code', |
| 109 | + code, |
| 110 | + }) |
| 111 | + } |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + promise.abort = function abort() { |
| 116 | + if (child !== null) { |
| 117 | + child.kill() |
| 118 | + child = null |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return promise |
55 | 123 | } |
56 | 124 |
|
57 | 125 | function getLogLevel({silent, logLevel}) { |
|
0 commit comments