Gulp 4: how to detect the "Did you forget to signal async completion?" error in a complex build script

3.4k views Asked by At

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?

1

There are 1 answers

1
redhatvicky On

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();
    })();    
}