How to run some conditional gulp task

Suppose I have this in my gulpfile:

gulp.task('foo', ...);

gulp.task('bar', function () {
  if (something) {
    // how do I run task 'foo' here?
  }
});
+4
source share
2 answers

You can make the bar depend on foo and place the condition inside foo:

gulp.task('foo', function(){
  if(something){...}
}, 'bar');

gulp.task('bar', function(){});

This bar will always work until foo, and foo can choose whether to run its own logic.

+1
source

Gulp v3

Use obsolete but still working gulp.run

gulp.task('foo', ...)    

gulp.task('bar', function () {
  if (something) {
    gulp.run('foo')
  }
})

, , run-sequence ( , , ). (Gulp v3):

gulp.task('bar', (callback) => {
  if (something) {
    runSequence('foo', callback)
  } else {
    runSequence('foo', 'anotherTask', callback)
  }
})

Gulp v4

gulpfile, gulpfile.babel.js , Gulp , :

export function foo () {
  ...
}

export function bar () {
  if (something) {
    foo()
  }
}
+1

Source: https://habr.com/ru/post/1610218/


All Articles