Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have conditional dependencies in gulp?

Tags:

gulp

I have two requirements for my build script:

  1. When I run gulp clean build, clean must complete before build starts.
  2. If I run gulp build, then clean shouldn't run.

So, if clean is specified, then build should wait for it, else start.

The first part is possible if I do

gulp.task('clean');
gulp.task('build', ['clean']);

However, that violates point 2

If I do

gulp.task('clean');
gulp.task('build');

That violates point 1

Is this possible with gulp?

like image 682
pgreen2 Avatar asked Dec 12 '14 20:12

pgreen2


1 Answers

You cannot run two gulp tasks with the same command like you did with dependency management you want.

Anyway you can pass an argument to your build task that will allow, using a little ternary, to wait for the clean one to complete before running.

So something like this:

gulp.task('build', (process.argv[3] === '--clean') ? ['clean'] : null, function () {
  ...
});

This way, you can launch your build normally with

gulp build

And when you want to call it with the clean:

gulp build --clean

There is a lot of ways to get better argument handling, like yargs or the env of gulp-util. But I found my method nice in the fact that it doesn't need any extra dependency.

like image 54
Preview Avatar answered Jan 05 '23 03:01

Preview