Estus Flask
Estus Flask

Reputation: 223318

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.

Upvotes: 12

Views: 8639

Answers (3)

tuck182
tuck182

Reputation: 121

Adding to the accepted answer:

Yes you can retrieve a task by calling gulp.task, but rather than calling it directly you can wrap it in a call to gulp.series (presumably gulp.parallel would also work) and call that. This has the benefit of producing the expected "Starting/Finished" output. An example would be:

gulp.series(gulp.task('some-task'))();

with the output:

[09:18:32] Starting 'some-task'...
[09:18:32] Finished 'some-task' after 7.96 ms

This was tested just now on gulp v4.0.2.

Upvotes: 12

Pascal Filion
Pascal Filion

Reputation: 9

I can run a task by doing this:

gulp.task('sync-task').unwrap()();

Upvotes: 0

Estus Flask
Estus Flask

Reputation: 223318

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

Upvotes: 14

Related Questions