Created
February 1, 2019 10:48
-
-
Save refo/053077df0cb3cee845cee28ebc6080ee to your computer and use it in GitHub Desktop.
CalVer versioning helper
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
const {exec} = require('child_process'); | |
const moment = require('moment'); | |
run('git describe') | |
.then(parseDescribe) | |
.catch(showError); | |
function parseDescribe(desc) { | |
const [tag, numOfCommits, lastCommit] = desc.trim().split('-'); | |
const majorCurr = tag.split('.').splice(0, 2).map(n => n.length < 2 ? `0${n}` : n); | |
const majorNew = moment() | |
// .add(1, 'd') | |
.format('YYYYMM.DD').split('.'); | |
const majorMax = verMax(majorCurr, majorNew); | |
const build = majorCurr.join('') === majorMax.join('') | |
? numOfCommits | |
: '0'; | |
const verSugg = [].concat(majorMax).concat([build]); | |
const tagSugg = verSugg.map(str => str * 1).join('.'); | |
console.log({desc, majorCurr, majorNew, majorMax, verSugg, tag, tagSugg}); | |
// const calverMajorCurrentInt = tag.split('.').splice(0, 2).map(n => n.length < 2 ? `0${n}` : n).join('') * 1; | |
// const calverMajorNew = moment().format('YYYYMM.DD'); | |
// const calverMajorNewInt = calverMajorNew.split('.').join(''); | |
// console.log({calverMajorCurrent, calverMajorNew}); | |
} | |
function verMax(ver1, ver2) { | |
const int1 = verArrToInt(ver1); | |
const int2 = verArrToInt(ver2); | |
const obj = { | |
[int1]: ver1, | |
[int2]: ver2, | |
}; | |
const max = Math.max(int1, int2); | |
return obj[max]; | |
} | |
function verArrToInt(arr) { | |
return arr.join('') * 1; | |
} | |
function showError(err) { | |
const line = '-'.repeat(20); | |
console.error(line); | |
console.error('ERROR:'); | |
console.error(err.message); | |
console.error(line); | |
} | |
function run(cmd) { | |
return new Promise((resolve, reject) => { | |
exec(cmd, {}, (error, stdout, stderr) => { | |
if (error) { | |
return reject(error.message); | |
} | |
if (stderr) { | |
return reject(stderr); | |
} | |
return resolve(stdout); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment