Reputation: 135463
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
Reputation: 2420
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)
}
})
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
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