Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass a flag to Gulp to have it run tasks in different ways?

Normally in Gulp tasks look like this:

gulp.task('my-task', function() {     return gulp.src(options.SCSS_SOURCE)         .pipe(sass({style:'nested'}))         .pipe(autoprefixer('last 10 version'))         .pipe(concat('style.css'))         .pipe(gulp.dest(options.SCSS_DEST)); }); 

Is it possible to pass a command line flag to gulp (that's not a task) and have it run tasks conditionally based on that? For instance

$ gulp my-task -a 1 

And then in my gulpfile.js:

gulp.task('my-task', function() {         if (a == 1) {             var source = options.SCSS_SOURCE;         } else {             var source = options.OTHER_SOURCE;         }         return gulp.src(source)             .pipe(sass({style:'nested'}))             .pipe(autoprefixer('last 10 version'))             .pipe(concat('style.css'))             .pipe(gulp.dest(options.SCSS_DEST)); }); 
like image 916
asolberg Avatar asked Apr 11 '14 22:04

asolberg


People also ask

How do you define a task in Gulp?

To have your tasks execute in order, use the series() method. For tasks to run at maximum concurrency, combine them with the parallel() method. Tasks are composed immediately when either series() or parallel() is called. This allows variation in the composition instead of conditional behavior inside individual tasks.


1 Answers

Gulp doesn't offer any kind of util for that, but you can use one of the many command args parsers. I like yargs. Should be:

var argv = require('yargs').argv;  gulp.task('my-task', function() {     return gulp.src(argv.a == 1 ? options.SCSS_SOURCE : options.OTHER_SOURCE)         .pipe(sass({style:'nested'}))         .pipe(autoprefixer('last 10 version'))         .pipe(concat('style.css'))         .pipe(gulp.dest(options.SCSS_DEST)); }); 

You can also combine it with gulp-if to conditionally pipe the stream, very useful for dev vs. prod building:

var argv = require('yargs').argv,     gulpif = require('gulp-if'),     rename = require('gulp-rename'),     uglify = require('gulp-uglify');  gulp.task('my-js-task', function() {   gulp.src('src/**/*.js')     .pipe(concat('out.js'))     .pipe(gulpif(argv.production, uglify()))     .pipe(gulpif(argv.production, rename({suffix: '.min'})))     .pipe(gulp.dest('dist/')); }); 

And call with gulp my-js-task or gulp my-js-task --production.

like image 129
Caio Cunha Avatar answered Sep 22 '22 03:09

Caio Cunha