Skip to content

Instantly share code, notes, and snippets.

@ronapelbaum
Created November 19, 2018 08:24
Show Gist options
  • Save ronapelbaum/300b3002e151a51975e7735ab812c621 to your computer and use it in GitHub Desktop.
Save ronapelbaum/300b3002e151a51975e7735ab812c621 to your computer and use it in GitHub Desktop.
git-increment
const proc = require('child_process');
const gitGetTag = cb => proc.execFile('git', ['describe', '--abbrev=0', '--tags'], (err, res) => cb(res.trim()));
const gitPutTag = (tag, cb) => proc.execFile('git', ['tag', tag], (err, res) => cb(res.trim()));
const parseTag = (tag) => {
try {
return /v(\d+)\.(\d+)\.(\d+)(-(\d+))?/.exec(tag).slice(1)
} catch (e) {
// do nothing
}
return '';
};
const patch = (tag) => {
const arr = parseTag(tag);
return `v${arr[0]}.${arr[1]}.${+arr[2] + 1}`;
};
const minor = (tag) => {
const arr = parseTag(tag);
return `v${arr[0]}.${+arr[1] + 1}.${arr[2]}`;
};
const major = (tag) => {
const arr = parseTag(tag);
return `v${+arr[0] + 1}.${arr[1]}.${arr[2]}`;
};
const prerelease = (tag) => {
const arr = parseTag(tag);
return `v${arr[0]}.${arr[1]}.${arr[2]}-${+(arr[4] || 0) + 1}`;
};
function increment(tag, type) {
switch (type) {
case '--major':
return major(tag);
case '--minor':
return minor(tag);
case '--patch':
return patch(tag);
default:
return prerelease(tag);
}
}
gitGetTag((tag) => {
try {
const newTag = increment(tag, process.argv[2]);
gitPutTag(newTag, () => {
console.log(`git-increment ${tag} -> ${newTag} successful`);
});
} catch (e) {
console.error(e);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment