1

There is a gulp task:

gulp.task('copy-bower-components-to-dev', function () {
  gulp.src('./app/bower_components/**')
    .pipe(gulp.dest('dev/bower_components'));
});

I also need the same task for another folder:

gulp.task('copy-bower-components-to-prod', function () {
  gulp.src('./app/bower_components/**')
    .pipe(gulp.dest('prod/bower_components'));
});

But it is not a clean code; how can I make a gulp task with params(in this case param with a folder name)? Thanks in advance!

malcoauri
  • 10,195
  • 24
  • 72
  • 124
  • You can send it an argument -dev or -prod http://stackoverflow.com/questions/23023650/is-it-possible-to-pass-a-flag-to-gulp-to-have-it-run-tasks-in-different-ways – Ed Knowles May 03 '15 at 09:30

2 Answers2

2

The gulp-utils are actually perfect for this:

var gutil = require('gulp-util');

function destination() {
    return (!!gutil.env.type && gutil.env.type == 'prod' ? 'prod' : 'dev';
}

gulp.task('copy-bower-components', function () {
  return gulp.src('./app/bower_components/**')
    .pipe(gulp.dest(destination() + '/bower_components'));
});

Run it with gulp --type=prod to set the new folder!

ddprrt
  • 6,939
  • 3
  • 23
  • 23
  • Thanks! It works! Also, may I set some flags for task by default? For example, for this task: gulp.task('build-dev', ['translate-sass', 'translate-coffee', 'translate-slim', 'copy-bower-components', 'connect']) – malcoauri May 03 '15 at 09:55
  • I updated the answer to make it more failsafe: The default value for destination is `'dev'` in that example -- that should work for your other flags as well ;-) – ddprrt May 03 '15 at 09:57
0

You can try this plugin gulp-foal to run a foal-task with param.

param NOT from cmd.

'use strict';

var gulp = require('gulp');
var clean = require('gulp-clean');
var foal = require('gulp-foal');

//use "foal.task(...)" to define a foal-task.
foal.task('clean_some', function(cleanPath) {
  //"return" shall not be missed for running foal-task orderly.
  return gulp.src(cleanPath)
    .pipe(clean());
});

gulp.task('default', function(cb) {
  //use "foal.run(...)" to run your foal-task with param.
  foal.run(clean_some('test_path'), cb);
});
CJY0208
  • 115
  • 1
  • 5