How to run a gulp task from another task?

Solution 1:

I did it using gulp.start(); like this:

gulp.task('test1', function(){
  gulp.start('test2');
})

gulp.task('test2', function(){
  // do something
})

I was using gulp 3.9.1 if it matters. Looks like gulp.start() may be removed or deprecated; but it hasn't happened yet.


Update If I were to break things out into separate functions, as Novellizator suggested, it would be something like this:

function doSomething(){
  // do something
}

gulp.task('test1', function(){
  doSomething();
})

gulp.task('test2', function(){
  doSomething();
})

It is simple and avoids the use of gulp.start().

Solution 2:

From Gulp 4+ I found this to work well as a replacement for gulp.start:

const task1 = () => { /*do stuff*/ };
const task2 = () => { /*do stuff*/ };

// Register the tasks with gulp. They will be named the same as their function
gulp.task(task1);
gulp.task(task2);

...

// Elsewhere in your gulfile you can run these tasks immediately by calling them like this
(gulp.series("task1", "task2")());
// OR
(gulp.parallel("task1", "task2")());