Skip to content

Instantly share code, notes, and snippets.

@richthegeek
Created June 12, 2019 08:43
Show Gist options
  • Save richthegeek/a7810c770e78364ec11ab64e2f0f95f2 to your computer and use it in GitHub Desktop.
Save richthegeek/a7810c770e78364ec11ab64e2f0f95f2 to your computer and use it in GitHub Desktop.
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);
@richthegeek
Copy link
Author

richthegeek commented Jun 12, 2019

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), or patch (1.2.3 -> 1.2.4)

Adding it to your .bash_profile with an alias can make it easier: `alias semver='node ~/.semver.js'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment