0

I have folder with files for different build environment like production,stage, and test. example:

src/config.prod.js 
src/config.stage.js 
src/config.test.js 

what I want is to copy config file based on environment I got, for getting environment name from command i using following code:

var nopt = require('nopt')
        , knownOpts = {
              "env" : [String, null]
        }
        , shortHands = {
              "test" : ["--env", "test"]
            , "dev" : ["--dev", "dev"]
            , "stage" : ["--env", "stage"]
            , "prod" : ["--env", "prod"]
        };
var flags = nopt(knownOpts, shortHands, process.argv, 2);

and when I hit command

gulp build --env dev 

I am getting the environment name, now what I want is to copy config file to dist folder(build) based on environment. I have this task for copy file but it copy all files as I don't know how to filter it out.

 gulp.task('copyConfig', function(){
    gulp.src(['src/*.js'])
    .pipe(gulp.dest('dist/'))
})

I am new to gulp, if some one has any suggestion. please help.

Toretto
  • 4,721
  • 5
  • 27
  • 46
  • Look at this very helpful question : https://stackoverflow.com/questions/23023650/is-it-possible-to-pass-a-flag-to-gulp-to-have-it-run-tasks-in-different-ways – Mark Aug 30 '17 at 14:38

1 Answers1

1

new configs directory where you mv your 3, existing configs (prod, stage, test) ... mkdir configs

Then , 2 related changes to gulp to incorporate build.ENV and a copy.config task that knows about your 3 diff config files...

var settings = {
  /*
   * Environment development | production
   * match './configs' files of same name
   */
  environment  :  process.env.NODE_ENV || 'development',
  /*
   * Where is our config folder?
   */
  configFolder : 'configs',
  /*
   * Where is our code?
   */
  srcFolder    : 'app/scripts',
  /*
   * Where are we building to?
   */
  buildFolder  : 'dist',
};

/**
 * Config Task
 *
 * Get the configuration file (dev or prod), rename it
 * and move it to be built.
 */
gulp.task('config', function() {
  return gulp.src(settings.configFolder + '/' + settings.environment + '.js')
             .pipe(rename('config.js'))
             .pipe(gulp.dest(settings.srcFolder));
});
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43