Skip to content

Instantly share code, notes, and snippets.

@andrewhathaway
Last active August 10, 2017 02:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewhathaway/38b9778e7a8e0adfd351 to your computer and use it in GitHub Desktop.
Save andrewhathaway/38b9778e7a8e0adfd351 to your computer and use it in GitHub Desktop.
Environment-based configuration for JavaScript applications - Gulpfile.js
var Gulp = require('gulp');
var Del = require('del');
var Rename = require('gulp-rename');
var RunSequence = require('run-sequence');
var Yargs = require('yargs');
var argv = Yargs.argv;
/**
* Build Settings
*/
var settings = {
/*
* Environment to build our application for
*
* If we have passed an environment via a
* CLI option, then use that. If not attempt
* to use the NODE_ENV. If not set, use production.
*/
environment : (!!argv.env
? argv.env
: process.env.NODE_ENV || 'production',
/*
* Where is our config folder?
*/
configFolder : '/js/config',
/*
* Where is our code?
*/
srcFolder : '/js/src',
/*
* Where are we building to?
*/
buildFolder : '/js/build',
/*
* Where should the final file be?
*/
destFolder : '/public/js'
};
/**
* Clean Task
*
* Clears the build folder from our
* previous builds files.
*/
Gulp.task('clean', function(cb) {
Del([
settings.buildFolder + '/**/*'
], cb);
});
/**
* Config Task
*
* Get the configuration file, 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.desk(settings.buildFolder));
});
/**
* Src Task
*
* Grabs all the files from the src folder and
* places them in our build folder.
*/
Gulp.task('src', function() {
return Gulp.src(settings.srcFolder + '/**/*')
.pipe(Gulp.dest(settings.buildFolder));
});
/**
* Build Task
*
* Build our transpiled/compiled and config
* files in to one awesome file.
*/
Gulp.task('build', function() {
return Gulp.src(settings.srcFolder + '/app.js')
.pipe(// Employ your build system)
.pipe(settings.destFolder);
});
/**
* Default Task
*
* Run the above tasks in the correct order
*/
Gulp.task('default', function(cb) {
RunSequence([
'clean',
'config',
'src',
'build'
], cb);
});
/**
* Watch Task
*
* Run 'gulp watch' to build when a file has been saved.
*/
Gulp.task('watch', function() {
return Gulp.watch([
settings.srcFolder + '/**/*',
settings.configFolder + '/**/*'
], {}, function() {
Gulp.start('default');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment