Last active
May 31, 2018 06:28
-
-
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.
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
#!/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