Last active
October 27, 2017 16:58
-
-
Save kvn1234/ab60070c4f881a14ee6bc898941f3d74 to your computer and use it in GitHub Desktop.
Ionic & Cordova hook for incrementing build & version numbers
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
#!/usr/bin/env node | |
// Save hook under `project-root/hooks/before_prepare/` | |
// | |
// Don't forget to install xml2js using npm | |
// `$ npm install xml2js` | |
// tested with Cordova 7.0.1 & Ionic 3.7.0 | |
var fs = require('fs'); | |
var xml2js = require('xml2js'); | |
// Read config.xml | |
fs.readFile('config.xml', 'utf8', function(err, data) { | |
if(err) { | |
return console.log(err); | |
} | |
// Get XML | |
var xml = data; | |
// Parse XML to JS Obj | |
xml2js.parseString(xml, function (err, result) { | |
if(err) { | |
return console.log(err); | |
} | |
// Get JS Obj | |
var obj = result; | |
// ios-CFBundleVersion doen't exist in config.xml | |
if(typeof obj['widget']['$']['ios-CFBundleVersion'] === 'undefined') { | |
obj['widget']['$']['ios-CFBundleVersion'] = 0; | |
} | |
// android-versionCode doen't exist in config.xml | |
if(typeof obj['widget']['$']['android-versionCode'] === 'undefined') { | |
obj['widget']['$']['android-versionCode'] = 0; | |
} | |
if(typeof obj['widget']['$']['version'] === 'undefined') { | |
obj['widget']['$']['version'] = '0.0.1'; | |
console.log('Version Undefined; Setting to 0.0.1'); | |
} else { | |
let v = obj['widget']['$']['version'].split('.'); | |
obj['widget']['$']['version'] = v[0] + '.' + v[1] + '.' + String(Number(v[2]) + 1); | |
} | |
// Increment build numbers (separately for iOS and Android) | |
obj['widget']['$']['ios-CFBundleVersion']++; | |
obj['widget']['$']['android-versionCode']++; | |
// Build XML from JS Obj | |
var builder = new xml2js.Builder(); | |
var xml = builder.buildObject(obj); | |
// Write config.xml | |
fs.writeFile('config.xml', xml, function(err) { | |
if(err) { | |
return console.log(err); | |
} | |
console.log('Build & version numbers successfully incremented'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment