Skip to content

Instantly share code, notes, and snippets.

@gpickin
Last active May 31, 2018 06:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpickin/9b6edbfa49d2879b4359 to your computer and use it in GitHub Desktop.
Save gpickin/9b6edbfa49d2879b4359 to your computer and use it in GitHub Desktop.
This Cordova Hook determines which Environment your app is building for, and replaces an Environment Javascript Object to allow the App to behave accordingly.
#!/usr/bin/env node
var environmentList = ['debug','staging','prod'];
var fs = require("fs");
var path = require("path");
console.log('-----------------------------');
console.log("Running hook: "+path.basename(process.env.CORDOVA_HOOK));
var buildEnvironment = environmentList[0];
if (process.env.target && environmentList.indexOf( process.env.target ) > 0 ) {
buildEnvironment = process.env.target;
console.log( 'Setting environment');
}
else {
console.log( 'Using Default Environment' );
}
console.log('Using environment: ' + buildEnvironment);
var theSourceFile = path.join( path.resolve(),'www','js') + '/env.' + buildEnvironment + '.js';
var theDestinationFile = path.join( path.resolve(),'www','js') + '/env.js';
console.log( theSourceFile );
if (fs.existsSync(theSourceFile)) {
console.log('it exists');
fs.readFile( theSourceFile, function(err, buf) {
if (typeof buf !== 'undefined') {
console.log('Read from file ' + theSourceFile);
fs.writeFile(theDestinationFile, buf.toString() , function(err) {
console.log('Wrote to file ' + theDestinationFile);
console.log('-----------------------------');
if (err) throw err;
});
}
else {
console.log('Config File ' + theSourceFile + ' NOT FOUND');
console.log(theDestinationFile + ' NOT CHANGED');
console.log('-----------------------------');
}
});
}
else {
console.log('Config File ' + theSourceFile + ' NOT FOUND');
console.log(theDestinationFile + ' NOT CHANGED');
console.log('-----------------------------');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment