Last active
August 10, 2017 02:00
-
-
Save andrewhathaway/38b9778e7a8e0adfd351 to your computer and use it in GitHub Desktop.
Environment-based configuration for JavaScript applications - Gulpfile.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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