5

As people who have migrated to Gulp 4 may know, gulp.series – as it should – won't proceed with execution if a task in the series doesn't signal completion properly.

My problem with that is the way it handles this kind of error – it's simply a message in the console (Did you forget to signal async completion?); gulp still exits with an error code of 0. That means that if you run gulp inside a more complicated build script, to that script it will look like if everything was alright and gulp tasks completed successfully when they clearly didn't. (In contrast, if you throw an error with gulpUtil.plugin error, it results in a non-0 error code.)

So my question is: how would you detect "from the outside" that such error have happened?

  • Is there a way to make gulp exit with a non-0 in this case? (Is there an event I can subscribe to and throw an error, or a config I can set etc.)
  • Should I just try to check if the assets that should have been generated exist? (I find this solution very fragile.)
  • Should I "watch" the console messages and look for specific strings like "Did you forget to signal..."? (Ugh!)
  • (Yes, I know I could do everything with a gulp and then I wouldn't need to check the error code, but let's just assume that, in reality, I can't.)

Is there any other possibility I didn't think of?

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
Dániel Kis-Nagy
  • 2,255
  • 2
  • 18
  • 19

1 Answers1

2

You can use the stream/Promise/callback to signal the process. Once the hand shake is done , you could proceed with the process.

var print = require('gulp-print');

gulp.task('message', function() {
  return gulp.src('package.json')
    .pipe(print(function() { return 'HTTP Server Started'; }));
});

gulp.task('message', function() { 
  return new Promise(function(resolve, reject) {
    console.log("HTTP Server Started");
    resolve();
  });
});

gulp.task('message', function(done) {
  console.log("HTTP Server Started");
  done();
});

else use call backs in this fashion.

function syncTasks(callback1)
{
    return gulp.series(firstTask, secondTask, (callback2) =>
    {
        callback1();
        callback2();
    })();    
}
redhatvicky
  • 1,912
  • 9
  • 8
  • 3
    Thanks for the detailed answer! However, this doesn't really answer my question :) I know how the async completion works in gulp 4 and how tasks *should* behave. What I need is a way to detect, outside gulp, if the process is halted because of a misbehaving task. If a task throws an error, gulp exits with a non-0 error code – that's easy to handle! However, the async completion error makes gulp exit with 0 – making it hard to detect and handle it. Obviously, this error is best _avoided_ when possible – using the methods you mention – but things can still go wrong, and I want to handle it. – Dániel Kis-Nagy Dec 28 '17 at 19:15