@modea/modea-increment-version
Version:
Increments version and build numbers in specified files
84 lines (75 loc) • 2.24 kB
JavaScript
function increment(placesToChange, incrementType) {
let changesToMake = []
/*
format to return a list of these things
const changeToMake = {
type: 'build',
startIndex: 0,
endIndex: 0,
value: '',
}
*/
for (const place of placesToChange) {
const change = createChangeToMake(place);
if (place.type === 'build') {
change.value = bumpBuild(place.value)
changesToMake.push(change)
}
else if (place.type === 'version' && incrementType !== 'build') {
change.value = bumpVersion(place.value, incrementType)
changesToMake.push(change)
}
else {
console.error('\x1b[31m', `build type '${place.type}' is not supported. Please use 'version' or 'build'.`);
}
}
return changesToMake
}
function createChangeToMake(placeToChange) {
return {
type: placeToChange.type,
startIndex: placeToChange.startIndex,
endIndex: placeToChange.endIndex,
value: placeToChange.value
}
}
function bumpBuild(buildNumber) {
return String(Number(buildNumber) + 1)
}
function bumpVersion(versionCode, incrementType) {
const incrementIndex = getIncrementIndex(incrementType)
return incrementVersionByIndex(versionCode, incrementIndex)
}
function getIncrementIndex(incrementType) {
switch (incrementType) {
case 'major':
return 0;
case 'minor':
return 1;
case 'patch':
return 2;
default:
console.error(
'\x1b[31m%s\x1b[0m',
`No command line argument for type of increment found, please pass one in \nnode increment.js [major | minor | patch | build]`
);
process.exit(-1);
}
}
function incrementVersionByIndex(versionCode, index) {
const versionCodeDelimiter = '.';
let versionArray = versionCode.split(versionCodeDelimiter);
versionArray = incrementArrayIndexByOne(versionArray, index)
return versionArray.join(versionCodeDelimiter);
}
function incrementArrayIndexByOne(array, index) {
array[index] = String(Number(array[index]) + 1);
// now we have to downgrade the lesser types so we get 1.6.0 instead of 1.6.1 when incrementing minor version from 1.5.1
for (let i = index + 1; i < array.length; i++) {
array[i] = '0';
}
return array;
}
module.exports = {
increment,
};