Skip to content

Commit

Permalink
handle orchestration aborted events (#97)
Browse files Browse the repository at this point in the history
Handle orchestration aborted events
  • Loading branch information
memoryhole authored and OverZealous committed Sep 19, 2017
1 parent 066b8c6 commit 2155560
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function runSequence(gulp) {
var callBack = typeof taskSets[taskSets.length - 1] === 'function' ? taskSets.pop() : false;
var currentTaskSet;

var finished;

if(options().ignoreUndefinedTasks) {
// ignore missing tasks
taskSets = filterArray(taskSets)
Expand All @@ -67,8 +69,12 @@ function runSequence(gulp) {
}

function finish(e) {
if(finished) return;
finished = true;

gulp.removeListener('task_stop', onTaskEnd);
gulp.removeListener('task_err', onError);
gulp.removeListener('err', onGulpError);

var error;
if(e && e.err) {
Expand Down Expand Up @@ -96,6 +102,17 @@ function runSequence(gulp) {
}
}

function onGulpError(e) {
// In the case that you call gulp.stop after a successful run,
// we will not recieve a task_err or task_stop event. This callback
// will finish the run sequence execution in case of an 'orchestration aborted'
// even coming from gulp's global error handler. That event is fired in when
// gulp.stop is called.
if(e.message === 'orchestration aborted') {
finish(e);
}
};

function runNextSet() {
if(taskSets.length) {
var command = taskSets.shift();
Expand All @@ -113,6 +130,7 @@ function runSequence(gulp) {

gulp.on('task_stop', onTaskEnd);
gulp.on('task_err', onError);
gulp.on('err', onGulpError);

runNextSet();
}
Expand Down
36 changes: 35 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,40 @@ describe('runSequence', function() {

called.should.eql(true);
})

it('should pass error if gulp execution halted in second execution', function(done) {
var stopTask = gulp.task('stopTask', function() {
if(stopTask.shouldStop) {
gulp.stop();
}
});

stopTask.shouldStop = false;

var outerTask = gulp.task('outerTask', function(cb) {
runSequence('task2', ['stopTask', 'task3'], function(err) {
if(stopTask.shouldStop) {
try {
should(err).be.ok;
err.message.should.equal('orchestration aborted');
} catch(e) {
cb();
return done(e);
}
cb();
done();
} else {
cb();
}
});
});

gulp.start('outerTask', function() {
stopTask.shouldStop = true;
task3.shouldPause = true;
gulp.start('outerTask');
});
})
});

describe('Options', function() {
Expand All @@ -275,4 +309,4 @@ describe('runSequence', function() {
});
});

});
});

0 comments on commit 2155560

Please sign in to comment.