Last active
August 29, 2015 14:21
-
-
Save chris-ramon/fd797b752f720eea19e3 to your computer and use it in GitHub Desktop.
gulp replacer
This file contains 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
angular.module('coolapp-constants',[]) | |
.constant('apiUrl', '@@apiUrl'); | |
// gulp-replace-task | |
//- config/ | |
// ` localdev.json | |
// ` test.json | |
// ` production.json | |
// localdev.json | |
{ | |
"apiUrl":"https://dev.coolapp.com/api" | |
} | |
// production.json | |
{ | |
"apiUrl":"https://www.coolapp.com/api" | |
} | |
var gulp = require('gulp'); | |
var replace = require('gulp-replace-task'); | |
var args = require('yargs').argv; | |
var fs = require('fs'); | |
gulp.task('replace', function () { | |
// Get the environment from the command line | |
var env = args.env || 'localdev'; | |
// Read the settings from the right file | |
var filename = env + '.json'; | |
var settings = JSON.parse(fs.readFileSync('./config/' + filename, 'utf8')); | |
// Replace each placeholder with the correct value for the variable. | |
gulp.src('js/coolapp-constants.js') | |
.pipe(replace({ | |
patterns: [ | |
{ | |
match: 'apiUrl', | |
replacement: settings.apiUrl | |
} | |
] | |
})) | |
.pipe(gulp.dest('build/js')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment