Skip to content

Commit

Permalink
When on of the sequential tasks fails, exit the process. Closes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
okonet committed Jul 8, 2016
1 parent 8b4f96d commit 9f0a8d1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/runScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@ var batch = require('batchflow')

module.exports = function runScript (linters, paths, config, cb) {
var lintersArray = Array.isArray(linters) ? linters : [linters]
var exitCode = 0
batch(lintersArray)
.sequential()
.series()
.each(function (i, linter, next) {
// If previous process finished with non-zero code
// we'll stop executing the sequence
if (exitCode > 0) {
return next(exitCode)
}

findBin(linter, paths, config, function (err, binPath, args) {
if (err) {
throw new Error(err)
throw err
}
var npmStream = spawn(binPath, args, {
stdio: 'inherit' // <== IMPORTANT: use this option to inherit the parent's environment
})
npmStream.on('error', function (error) {
throw new Error(error)
})
npmStream.on('exit', function (code) {
process.exitCode = code
})
npmStream.on('close', function (code) {
next(code)
exitCode = code
})
npmStream.on('close', next)
})
})
.error(function (err) {
console.error(err)
cb.call(this, err, null)
})
.end(function (codes) {
var exitCode = codes.length ? Math.max.apply(this, codes) : 0
.end(function () {
process.exitCode = exitCode
if (typeof cb === 'function') {
cb.call(this, null, exitCode)
}
Expand Down
54 changes: 54 additions & 0 deletions test/runScript.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,59 @@ describe('runScript', () => {
done()
}, 10)
})

it('should stop the sequence execution if prev process finishes with non-zero', done => {
const spy = expect.createSpy()
const mySpawn = mockSpawn()
mySpawn.sequence.add(mySpawn.simple(1))
mySpawn.sequence.add(mySpawn.simple(0))
runScript.__set__('spawn', mySpawn)

runScript(['test', 'test2'], 'test.js', packageJSON, spy)
setTimeout(() => {
expect(mySpawn.calls.length).toEqual(1)
expect(mySpawn.calls[0].exitCode).toEqual(1)

expect(spy.calls.length).toEqual(1)
expect(spy).toHaveBeenCalledWith(null, 1)
done()
}, 10)
})

it('should handle errors for single commands', done => {
const err = new Error('linting error')
const spy = expect.createSpy()
const mySpawn = mockSpawn()
mySpawn.sequence.add({throws: err})
mySpawn.sequence.add(mySpawn.simple(0))
runScript.__set__('spawn', mySpawn)

runScript('test', 'test.js', packageJSON, spy)
setTimeout(() => {
expect(mySpawn.calls.length).toEqual(1)
expect(spy.calls.length).toEqual(1)
expect(spy.calls[0].arguments[0]).toBe(err)
expect(spy.calls[0].arguments[1]).toNotEqual(0)
done()
}, 10)
})

it('should handle errors for sequences', done => {
const err = new Error('linting error')
const spy = expect.createSpy()
const mySpawn = mockSpawn()
mySpawn.sequence.add({throws: err})
mySpawn.sequence.add(mySpawn.simple(0))
runScript.__set__('spawn', mySpawn)

runScript(['test', 'test2'], 'test.js', packageJSON, spy)
setTimeout(() => {
expect(mySpawn.calls.length).toEqual(1)
expect(spy.calls.length).toEqual(1)
expect(spy.calls[0].arguments[0]).toBe(err)
expect(spy.calls[0].arguments[1]).toNotEqual(0)
done()
}, 10)
})
})

0 comments on commit 9f0a8d1

Please sign in to comment.