node.jsgulpgulp-4

Run Gulp 4 task programmatically


There are several tasks defined with gulp.task in gulpfile.js:

gulp.task('sync-task', () => { ... });
gulp.task('async-task', cb => { ... });

I would like to start one of them programmatically. Preferably in the same process (no exec, etc.), because one of the reasons behind this is the ability to run the script in debugger.

How can this be done?

It looks like there were things like gulp.run and gulp.start, but they are deprecated in Gulp 4.


Solution

  • It appears that a task can be retrieved with gulp.task getter function and called directly:

    gulp.task('sync-task')();
    gulp.task('async-or-random-task')(function callbackForAsyncTasks(err) {
      if (err)
        console.error('error', err);
    });