@busy-web/deploy
Version:
busy-web ember-cli-deploy addon.
142 lines (122 loc) • 3.22 kB
JavaScript
/**
* @module Helpers
*
*/
var fs = require('fs');
/**
* `version helper methods`
*
*/
module.exports = {
_package: null,
package: null,
vf: null,
getVersion: function(update) {
update = update === true ? true : false;
if ((update === true) || (!this.vf && !this.package)) {
var vf = (fs.readFileSync('./public/version.json')).toString();
this.vf = JSON.parse(vf);
this._package = (fs.readFileSync('./package.json')).toString();
this.package = JSON.parse(this._package);
if (!this.vf || !this.vf.version) {
this.vf = this.package;
}
}
return this.vf.version;
},
setVersion: function(version, updatePackage) {
updatePackage = updatePackage === true ? true : false;
this.getVersion();
if (version !== this.vf.version && this.isValid(version)) {
saveVF(this, version);
if (updatePackage === true) {
savePKG(this, version);
}
}
},
syncVersion: function() {
this.getVersion();
saveVF(this, this.package.version);
},
getBuild: function() {
this.getVersion();
var parts = this.vf.version.split('.')[2];
if (parts) {
parts = parts.split('-')[1];
if (parts) {
return parts;
} else {
return 'production';
}
}
return false;
},
setBuild: function(type) {
this.getVersion();
var parts = this.vf.version.split('.');
if (type === 'production' || type === 'prod') {
parts = [parts[0], parts[1], 0];
} else {
var build = [0, type];
parts[2] = build.join('-');
parts[3] = 0;
}
return parts.join('.');
},
patch: function() {
return this.incrementVersion();
},
minor: function() {
return this.incrementVersion(1);
},
major: function() {
return this.incrementVersion(0);
},
incrementVersion(index) {
this.getVersion();
if (this.isValid(this.vf.version)) {
var parts = this.vf.version.split('.');
index = index !== undefined ? index : parts.length-1;
for (var i=index; i<parts.length; i++) {
var nums = parts[i].split('-');
nums[0] = (i === index ? Number(nums[0]) + 1 : 0);
parts[i] = nums.join('-');
}
return parts.join('.');
}
return false;
},
isValid(version) {
if (version !== null || version !== undefined) {
if (/\d+\.\d+/.test(version)) {
return true;
}
}
return false;
},
isMatch(version) {
this.getVersion();
var parts = this.vf.version.split('.');
var testParts = version.split('.');
return testParts[0] === parts[0] && testParts[1] === parts[1];
},
isProduction(version) {
this.getVersion();
var parts = this.vf.version.split('.');
var testParts = version.split('.');
return testParts[0] === 'v' + parts[0] && testParts[1] === parts[1];
},
};
function saveVF(target, version) {
var buildDate = (new Date()).toUTCString();
var buildTime = Date.now()/1000;
buildTime = buildTime - buildTime%1;
target.vf = { version, buildDate, buildTime };
fs.writeFileSync('./public/version.json', JSON.stringify(target.vf));
}
function savePKG(target, version) {
var oldVersion = target.package.version;
target.package.version = version;
target._package = target._package.replace('"version": "' + oldVersion + '"', '"version": "' + version + '"');
fs.writeFileSync('./package.json', target._package);
}