|
1 | 1 | import spawn from 'spawn-command-with-kill' |
2 | 2 | import Promise from 'bluebird' |
3 | | -import async from 'async' |
4 | 3 | import colors from 'colors/safe' |
5 | | -import {isString, find, clone} from 'lodash' |
| 4 | +import {isString, clone} from 'lodash' |
6 | 5 | import {sync as findUpSync} from 'find-up' |
7 | 6 | import managePath from 'manage-path' |
8 | 7 | import arrify from 'arrify' |
9 | 8 | import getScriptToRun from './get-script-to-run' |
10 | 9 | import getScriptsFromConfig from './get-scripts-from-config' |
11 | 10 | import getLogger from './get-logger' |
12 | 11 |
|
13 | | -const noop = () => {} // eslint-disable-line func-style, no-empty-function |
14 | 12 | const NON_ERROR = 0 |
15 | 13 |
|
16 | 14 | export default runPackageScripts |
17 | 15 |
|
18 | | -function runPackageScripts({scriptConfig, scripts, args, options = {}}, callback = noop) { |
| 16 | +function runPackageScripts({scriptConfig, scripts, args, options = {}}) { |
19 | 17 | if (scripts.length === 0) { |
20 | 18 | scripts = ['default'] |
21 | 19 | } |
22 | 20 | const scriptNames = arrify(scripts) |
23 | 21 | if (options.parallel) { |
24 | | - runParallel() |
| 22 | + return runParallel() |
25 | 23 | } else { |
26 | | - runSeries() |
| 24 | + return runSeries() |
27 | 25 | } |
28 | 26 |
|
29 | 27 | function runSeries() { |
30 | | - async.mapSeries(scriptNames, (scriptName, cb) => { |
31 | | - runPackageScript({scriptConfig, options, scriptName, args}) |
32 | | - .then(code => cb(null, code), cb) |
33 | | - }, onFinished) |
| 28 | + return scriptNames.reduce((res, scriptName) => { |
| 29 | + return res.then(() => ( |
| 30 | + runPackageScript({scriptConfig, options, scriptName, args}) |
| 31 | + )) |
| 32 | + }, Promise.resolve()) |
34 | 33 | } |
35 | 34 |
|
36 | 35 | function runParallel() { |
37 | 36 | const results = scriptNames.map(script => ({script, code: undefined})) |
38 | 37 | let aborted = false |
39 | | - let errorCode = null |
40 | 38 |
|
41 | 39 | const promises = scriptNames.map(scriptName => { |
42 | 40 | return runPackageScript({scriptConfig, options, scriptName, args}) |
43 | 41 | }) |
44 | 42 |
|
45 | | - const allPromise = Promise.all((promise, index) => { |
| 43 | + const allPromise = Promise.all(promises.map((promise, index) => { |
46 | 44 | return promise.then(code => { |
47 | | - if (aborted) { |
48 | | - return |
49 | | - } |
50 | | - |
51 | | - results[index].code = code |
52 | | - |
53 | | - if (errorCode === null && code !== 0) { |
54 | | - errorCode = errorCode || code |
55 | | - abortAll() |
| 45 | + if (!aborted) { |
| 46 | + results[index].code = code |
56 | 47 | } |
57 | 48 | }) |
58 | | - }) |
| 49 | + })).then(() => results) |
59 | 50 |
|
60 | 51 | allPromise.catch(() => { |
61 | | - if (!aborted) { |
| 52 | + /* istanbul ignore if */ |
| 53 | + if (aborted) { |
| 54 | + // this is very unlikely to happen |
| 55 | + } else { |
62 | 56 | abortAll() |
63 | 57 | } |
64 | 58 | }) |
65 | 59 |
|
66 | | - allPromise.then(() => onFinished(null, results), onFinished) |
| 60 | + return allPromise |
67 | 61 |
|
68 | 62 | function abortAll() { |
69 | 63 | aborted = true |
70 | 64 | promises.forEach(p => p.abort()) |
71 | 65 | } |
72 | 66 | } |
73 | | - |
74 | | - function onFinished(err, results) { |
75 | | - if (err) { |
76 | | - callback({error: err}) |
77 | | - } else { |
78 | | - const result = find(results, r => r !== NON_ERROR) |
79 | | - callback({code: result}) |
80 | | - } |
81 | | - } |
82 | 67 | } |
83 | 68 |
|
84 | 69 |
|
@@ -121,6 +106,7 @@ function runPackageScript({scriptConfig, options, scriptName, args}) { |
121 | 106 | `The script called "${scriptName}" which runs "${command}" failed with exit code ${code}` |
122 | 107 | ), |
123 | 108 | ref: 'failed-with-exit-code', |
| 109 | + code, |
124 | 110 | }) |
125 | 111 | } |
126 | 112 | }) |
|
0 commit comments