1
Fork 0
userstyles/bump.js

54 lines
1.6 KiB
JavaScript

// Bumps a specified style's version
const fs = require('fs');
const path = require('path');
/**
* @function bump
* @param {string} jsonPath - Path to the .json file
* @param {string} incrementType - Which semver type to increment: "major", "minor" or "patch"
*/
function bump(jsonPath, incrementType) {
if (!fs.existsSync(jsonPath))
throw Error(`File ${jsonPath} does not exist.`);
if (path.extname(jsonPath) !== '.json')
throw Error(`File ${jsonPath} does not end in "json".`);
if (typeof incrementType === 'undefined')
throw Error('No valid increment type was included. Valid: "major", "minor" or "patch".');
if (incrementType !== 'major' && incrementType !== 'minor' && incrementType !== 'patch')
throw Error(`Increment type ${incrementType} does not equal "major", "minor" or "patch".`);
const jsonFile = require(jsonPath);
if (typeof jsonFile.options.version === 'undefined')
throw Error(`${jsonFile} does not include a "version" property.`);
const incrementTypes = {
'major': 0,
'minor': 1,
'patch': 2,
};
const oldVersion = jsonFile.options.version.split('.');
let bumped = parseInt(oldVersion[incrementTypes[incrementType]]) + 1;
let newVersion;
switch (incrementType) {
case 'major':
newVersion = `${bumped}.0.0`;
break;
case 'minor':
newVersion = `${oldVersion[0]}.${bumped}.0`;
break;
case 'patch':
newVersion = `${oldVersion[0]}.${oldVersion[1]}.${bumped}`;
break;
}
jsonFile.options.version = newVersion;
fs.writeFileSync(jsonPath, JSON.stringify(jsonFile, null, 2));
}
exports.bump = bump;