Created
June 12, 2019 08:43
-
-
Save richthegeek/a7810c770e78364ec11ab64e2f0f95f2 to your computer and use it in GitHub Desktop.
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
let type = process.argv[2]; | |
if (type !== 'minor' && type !== 'major' && type !== 'patch') { | |
console.error('Specify major/minor/patch as the first argument'); | |
process.exit(1) | |
} | |
let file = process.cwd() + '/package.json'; | |
let package = require(file); | |
let version = package.version.match(/^([0-9]+)\.([0-9]+).([0-9]+)(.*)$/); | |
if (!version) { | |
console.error('Version string was not in valid format: ' + package.version); | |
} | |
if (type === 'major') { | |
version[1] = Number(version[1]) + 1; | |
version[2] = '0'; | |
version[3] = '0'; | |
} else if (type === 'minor') { | |
version[2] = Number(version[2]) + 1; | |
version[3] = '0'; | |
} else { | |
version[3] = Number(version[3]) + 1; | |
} | |
version = version.slice(1).filter(Boolean).join('.'); | |
console.log(`Updating package ${package.name} ${package.version} -> ${version} (${type})`); | |
package.version = version; | |
let message = process.argv.slice(3).join(' '); | |
if (message.trim().length === 0) { | |
console.error('Specify a commit message as the second argument'); | |
} | |
message = version + ': ' + message; | |
const fs = require('fs'); | |
fs.writeFileSync(file, JSON.stringify(package, null, 2)); | |
const exec = require('child_process').exec; | |
command = `git commit -am "${message}" && \n git tag v${version} && \n git push && \n git push --tags && \n npm publish`; | |
console.log(command); | |
exec('sleep 2 ; ' + command); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with
semver.js patch "some commit log"
Level can be
major
(1.2.3 -> 2.0.0),minor
(1.2.3 -> 1.3.0), orpatch
(1.2.3 -> 1.2.4)Adding it to your .bash_profile with an alias can make it easier: `alias semver='node ~/.semver.js'