Created
January 9, 2019 03:26
-
-
Save tinacious/b40f0ab8050e29f7a092d75af94904bf to your computer and use it in GitHub Desktop.
Version bump Node.js projects
This file contains 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
/** | |
* 1. Download file and put somewhere | |
* | |
* 2. In your ~/.bash_profile: | |
* | |
* alias version="node /path/to/version_bump.js" | |
* | |
* 3. From a JS project: | |
* | |
* version <type> major (M) | minor (m) | patch (p) | |
*/ | |
const Fs = require('fs'); | |
const Assert = require('assert'); | |
const versions = ['major', 'minor', 'patch']; // M.m.p | |
/** | |
* Parses package.json of the current directory | |
*/ | |
const parsePackageJson = () => { | |
const packageFileString = Fs.readFileSync('package.json').toString(); | |
return JSON.parse(packageFileString); | |
}; | |
/** | |
* Looks at first argument to determine type of upgrade: patch, minor, major | |
* defaults to patch if undefined | |
*/ | |
const getVersionType = (type = 'patch') => { | |
const versions = { | |
M: 'major', | |
m: 'minor', | |
p: 'patch', | |
major: 'major', | |
minor: 'minor', | |
patch: 'patch', | |
}; | |
return versions[type]; | |
}; | |
// Tests | |
Assert.equal(getVersionType('p'), 'patch'); | |
Assert.equal(getVersionType('patch'), 'patch'); | |
Assert.equal(getVersionType('minor'), 'minor'); | |
Assert.equal(getVersionType('m'), 'minor'); | |
Assert.equal(getVersionType('major'), 'major'); | |
Assert.equal(getVersionType('M'), 'major'); | |
const incrementVersion = (version, type) => { | |
const index = versions.indexOf(type); | |
const versionArr = version.split('.'); | |
const currentValue = Number(versionArr[index]); | |
const updatedValue = currentValue + 1; | |
const updatedVersionArr = [].concat(versionArr); | |
switch (type) { | |
case 'major': | |
return `${updatedValue}.0.0`; | |
case 'minor': | |
updatedVersionArr[1] = updatedValue; | |
updatedVersionArr[2] = 0; | |
return updatedVersionArr.join('.'); | |
case 'patch': | |
updatedVersionArr[2] = updatedValue; | |
return updatedVersionArr.join('.'); | |
default: | |
throw `Provide one of: ${versions.join(', ')}`; | |
} | |
}; | |
// Tests | |
Assert.equal(incrementVersion('1.1.1', 'patch'), '1.1.2', 'Failed to update patch version'); | |
Assert.equal(incrementVersion('2.1.1', 'patch'), '2.1.2', 'Failed to update patch version'); | |
Assert.equal(incrementVersion('1.1.1', 'minor'), '1.2.0', 'Failed to update minor version'); | |
Assert.equal(incrementVersion('2.1.1', 'minor'), '2.2.0', 'Failed to update minor version'); | |
Assert.equal(incrementVersion('1.1.1', 'major'), '2.0.0', 'Failed to update major version'); | |
Assert.equal(incrementVersion('2.1.1', 'major'), '3.0.0', 'Failed to update major version'); | |
const writePackageJson = (updated) => { | |
const fileContents = JSON.stringify(updated, null, 2) + '\n'; | |
Fs.writeFileSync('package.json', fileContents); | |
}; | |
function bumpVersion() { | |
const packageJson = parsePackageJson(); | |
const currentVersion = packageJson.version; | |
const versionType = getVersionType(process.argv[2]); | |
const updatedVersion = incrementVersion(currentVersion, versionType); | |
packageJson.version = updatedVersion; | |
writePackageJson(packageJson); | |
} | |
// do it | |
bumpVersion(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment