1800 INFORMATION
1800 INFORMATION

Reputation: 135463

How to run some gulp task based on a conditional

Suppose I have this in my gulpfile:

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

gulp.task('bar', function () {
  if (something) {
    // how do I run task 'foo' here?
  }
});

Upvotes: 6

Views: 5718

Answers (2)

revelt
revelt

Reputation: 2420

Gulp v3

Use deprecated but still working gulp.run

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

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

Alternatively, use use any plugins that consume task names as arguments, like run-sequence for example (which you will probably need anyway for running tasks in a strict sequence). I call my tasks conditionally this way (Gulp v3):

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

Gulp v4

Your gulpfile, that is, gulpfile.babel.js for now, would set Gulp tasks as exported functions so you would call them directly:

export function foo () {
  ...
}

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

Upvotes: 2

Meir
Meir

Reputation: 14395

You could make 'bar' a dependency of 'foo' and put the condition inside 'foo':

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

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

This way bar will always run before foo, and foo can choose if it is necessary to run its own logic.

Upvotes: 0

Related Questions