Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Increment build number for Cordova project
#!/usr/bin/env node
"use strict";
// Save hook under `project-root/hooks/before_compile/`
//
// Don't forget to install xml2js using npm
// `$ npm install xml2js`
// Modules
var fs = require('fs');
var xml2js = require('xml2js');
// Process
var cliCommand = process.env.CORDOVA_CMDLINE;
var isRelease = (cliCommand.indexOf('--release') > -1);
// Exit
if (!isRelease) {
return;
}
// Run increment build number
run();
function run() {
patchConfigXml();
patchPackageJson();
}
// Patch config.xml
function patchConfigXml() {
fs.readFile('config.xml', 'utf8', function (err, data) {
console.log('-----------------------------------------');
console.log('Increment config.xml reversion number');
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.warn(err);
}
// Get JS Obj
var obj = result;
// Patch version
if (typeof obj.widget.$.version === 'undefined') {
obj.widget.$.version = '0.0.1';
} else {
var currentVersion = obj.widget.$.version;
var curVer = String(obj.widget.$.version).split('.');
var versionObj = {
major: Number(curVer[0]),
minor: Number(curVer[1]),
reversion: Number(curVer[2])
};
console.log('Current Version: ', versionObj);
// Increment reversion numbers
versionObj.reversion += 1;
console.log('Target Version: ', versionObj);
var targetVersion = versionObj.major + '.' + versionObj.minor + '.' + versionObj.reversion;
obj.widget.$.version = targetVersion;
console.log(currentVersion + ' -> ' + targetVersion);
}
// Patch ios-CFBundleVersion
if (typeof obj.widget.$['ios-CFBundleVersion'] === 'undefined') {
obj.widget.$['ios-CFBundleVersion'] = '0.0.1.1';
} else {
var currentIosVersion = obj.widget.$['ios-CFBundleVersion'];
var curIosVer = String(obj.widget.$['ios-CFBundleVersion']).split('.');
var versionIosObj = {
major: Number(curIosVer[0]),
minor: Number(curIosVer[1]),
reversion: Number(curIosVer[2]),
build: Number(curIosVer[3])
};
console.log('Current ios-CFBundleVersion: ', versionIosObj);
// Increment reversion numbers
versionIosObj.reversion += 1;
console.log('Target ios-CFBundleVersion: ', versionIosObj);
var targetIosVersion = versionIosObj.major + '.' + versionIosObj.minor + '.' + versionIosObj.reversion + '.' + versionIosObj.build;
obj.widget.$['ios-CFBundleVersion'] = targetIosVersion;
console.log(currentIosVersion + ' -> ' + targetIosVersion);
}
// 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.warn(err); }
console.log('config.xml reversion number successfully incremented');
});
});
});
}
// Patch package.json
function patchPackageJson() {
fs.readFile('package.json', 'utf8', function (err, data) {
console.log('-----------------------------------------');
console.log('Increment package.json reversion number');
if (err) {
return console.warn(err);
}
// Get JSON
var json = JSON.parse(data);
if (typeof json.version === 'undefined') {
json.version = '0.0.1';
} else {
var currentVersion = json.version;
var curVer = String(json.version).split('.');
var versionObj = {
major: Number(curVer[0]),
minor: Number(curVer[1]),
reversion: Number(curVer[2])
};
console.log('Current Version: ', versionObj);
// Increment reversion numbers
versionObj.reversion += 1;
console.log('Target Version: ', versionObj);
var targetVersion = versionObj.major + '.' + versionObj.minor + '.' + versionObj.reversion;
json.version = targetVersion;
console.log(currentVersion + ' -> ' + targetVersion);
}
// Write package.json
fs.writeFile('package.json', JSON.stringify(json, null, 4), function (err) {
if (err) { return console.warn(err); }
console.log('package.json reversion number successfully incremented');
});
});
}
@poychang

This comment has been minimized.

Copy link
Owner Author

poychang commented Jun 22, 2016

Patch version to config.xml and package.json. In config.xml, also increase ios-CFBundleVersion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.