Skip to content

Commit 97d7f47

Browse files
author
Kent C. Dodds
committed
WIP: I think it is done!
1 parent de105c8 commit 97d7f47

File tree

4 files changed

+169
-87
lines changed

4 files changed

+169
-87
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "p-s",
3-
"version": "2.5.0-beta.2",
3+
"version": "0.0.0-semantically-released",
44
"description": "All the benefits of npm scripts without the cost of a bloated package.json and limits of json",
55
"main": "dist/index",
66
"scripts": {
@@ -21,7 +21,6 @@
2121
"license": "MIT",
2222
"dependencies": {
2323
"arrify": "1.0.1",
24-
"async": "2.0.1",
2524
"bluebird": "3.4.6",
2625
"colors": "1.1.2",
2726
"commander": "2.9.0",

src/bin/p-s.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,9 @@ function loadAndRun(scriptsAndArgs, psConfig) {
6060
parallel: scriptsAndArgs.parallel,
6161
logLevel: program.logLevel,
6262
}),
63-
}, result => {
64-
if (result.error) {
65-
log.error(result.error)
66-
process.exit(FAIL_CODE)
67-
}
68-
process.exit(result.code)
63+
}).catch(error => {
64+
log.error(error)
65+
process.exitCode = error.code || FAIL_CODE
6966
})
7067
}
7168

src/index.js

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,69 @@
11
import spawn from 'spawn-command-with-kill'
22
import Promise from 'bluebird'
3-
import async from 'async'
43
import colors from 'colors/safe'
5-
import {isString, find, clone} from 'lodash'
4+
import {isString, clone} from 'lodash'
65
import {sync as findUpSync} from 'find-up'
76
import managePath from 'manage-path'
87
import arrify from 'arrify'
98
import getScriptToRun from './get-script-to-run'
109
import getScriptsFromConfig from './get-scripts-from-config'
1110
import getLogger from './get-logger'
1211

13-
const noop = () => {} // eslint-disable-line func-style, no-empty-function
1412
const NON_ERROR = 0
1513

1614
export default runPackageScripts
1715

18-
function runPackageScripts({scriptConfig, scripts, args, options = {}}, callback = noop) {
16+
function runPackageScripts({scriptConfig, scripts, args, options = {}}) {
1917
if (scripts.length === 0) {
2018
scripts = ['default']
2119
}
2220
const scriptNames = arrify(scripts)
2321
if (options.parallel) {
24-
runParallel()
22+
return runParallel()
2523
} else {
26-
runSeries()
24+
return runSeries()
2725
}
2826

2927
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())
3433
}
3534

3635
function runParallel() {
3736
const results = scriptNames.map(script => ({script, code: undefined}))
3837
let aborted = false
39-
let errorCode = null
4038

4139
const promises = scriptNames.map(scriptName => {
4240
return runPackageScript({scriptConfig, options, scriptName, args})
4341
})
4442

45-
const allPromise = Promise.all((promise, index) => {
43+
const allPromise = Promise.all(promises.map((promise, index) => {
4644
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
5647
}
5748
})
58-
})
49+
})).then(() => results)
5950

6051
allPromise.catch(() => {
61-
if (!aborted) {
52+
/* istanbul ignore if */
53+
if (aborted) {
54+
// this is very unlikely to happen
55+
} else {
6256
abortAll()
6357
}
6458
})
6559

66-
allPromise.then(() => onFinished(null, results), onFinished)
60+
return allPromise
6761

6862
function abortAll() {
6963
aborted = true
7064
promises.forEach(p => p.abort())
7165
}
7266
}
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-
}
8267
}
8368

8469

@@ -121,6 +106,7 @@ function runPackageScript({scriptConfig, options, scriptName, args}) {
121106
`The script called "${scriptName}" which runs "${command}" failed with exit code ${code}`
122107
),
123108
ref: 'failed-with-exit-code',
109+
code,
124110
})
125111
}
126112
})

0 commit comments

Comments
 (0)