0

I have a gulp task configured like so:

gulp.task('concatenate_client_js', function(){
  return merge2(
      gulp.src('./gulp/buildConfig.json')
        .pipe(plugins.ngConfig('app.envConfig', {
          environment: 'development'
        })
      ),
      gulp.src(paths.clientJS)
    )
    .pipe(plugins.concat('angular_app.js'))
    .pipe(gulp.dest(paths.distScripts));
});

This task is then called in the runSequence of other tasks (split across different gulp files) I have like so:

gulp.task('prepare-dev-files', function(callback){
  runSequence('clean_dist_dir', 'copy_assets', 'copy_serverFiles', 'copy_bower_css',
  ['compile_bower_sass', 'concatonate_bower_js'],
  'compile_sass', 'concatenate_client_js', 'compile_main_js', callback);
});

gulp.task('prepare-staging-files',  ['prepare-dev-files'], function(callback) {
  runSequence('clean_test_src', 'copy_unitTestFiles', 'copy_files_for_coverage_report', 'karma_tests_ci', callback)
});

What I would like to be able to do is to set the environmentconfig option to the ngConfig plugin in the concatenate_client_js task based on args or params supplied to the task.

I have seen tools like yargs but am unsure how to use in my case. I would like to be able to supply an arg e.g --mode development to the call to my task in the runSequence's, e.g.

gulp.task('prepare-staging-files',  ['prepare-dev-files --mode staging'], function(callback) {
      runSequence('clean_test_src', 'copy_unitTestFiles', 'copy_files_for_coverage_report', 'karma_tests_ci', callback)
    });

And then add some switch code based on the mode argument passed to the task.

Is this achievable? Thanks

gnerkus
  • 11,357
  • 6
  • 47
  • 71
mindparse
  • 6,115
  • 27
  • 90
  • 191

1 Answers1

4

It is achievable. You can utilize the yargs module to do this. The argv property of the yargs module is available for all the gulp tasks defined in the gulpfile.js. Here is an example:

var gulp   = require('gulp');

var args   = require('yargs').argv;

gulp.task('a', function() {
  console.log('Arguments for A: ', args.env);
});

gulp.task('b', function() {
  console.log('Arguments for B: ', args.env);
});

gulp.task('c', ['a', 'b'], function() {
  console.log('Arguments for C: ', args.env);
});

The command $ gulp c --env development gives the following output:

14:42:01] Starting 'a'...

Arguments for A: development

[14:42:01] Finished 'a' after 175 μs

[14:42:01] Starting 'b'...

Arguments for B: development

[14:42:01] Finished 'b' after 67 μs

[14:42:01] Starting 'c'...

Arguments for C: development

[14:42:01] Finished 'c' after 51 μs

Community
  • 1
  • 1
gnerkus
  • 11,357
  • 6
  • 47
  • 71