Skip to content

Instantly share code, notes, and snippets.

@ganobrega
Created March 24, 2019 22:15
Show Gist options
  • Save ganobrega/514ca3f05ffab0c5844319b1ed1a9d3c to your computer and use it in GitHub Desktop.
Save ganobrega/514ca3f05ffab0c5844319b1ed1a9d3c to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const git = require('simple-git');
const changelog = require('generate-changelog');
const fs = require('fs');
const idx = require('idx');
const argv = require('minimist')(process.argv.slice(1));
git().tags((err, tags) => {
const currentChangelog = fs.readFileSync('./CHANGELOG.md');
const matched = tags.latest != null ? tags.latest.match(/v\d+.\d+.\d+-(\d+)/) : 'v0.0.1-0';
const build = (idx(matched, _ => Number(_[1])) || 0) + 1;
changelog
.generate({
major: argv.major,
minor: argv.minor,
patch: argv.patch,
})
.then(function(changelog) {
const rxVersion = /\d+\.\d+\.\d+/;
const newVersion = argv.version || idx(changelog.match(rxVersion), _ => _[0]) + `-${build}`;
changelog = changelog.replace(rxVersion, newVersion) + currentChangelog;
fs.writeFileSync('./CHANGELOG.md', changelog);
const addFile = c => git().add('CHANGELOG.md', c);
const commit = c => git().commit(`build(change-log): v${newVersion}`, c);
const addTag = c => git().addAnnotatedTag(`v${newVersion}`, `build(tag): v${newVersion}`, c);
addFile(() => commit(() => addTag()));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment