keep-a-changelog
keep-a-changelog copied to clipboard
NPM script to bump/scaffold CHANGELOG on `npm version ...`
Hello! Not really sure where/how to put this out there... maybe others will find it a useful idea and can make it cleaner. Anyway, "updating the change log" is kind of one of those things that sometimes gets "forgotten" when some folks on my team are cutting major/minor/patch releases 🙄
So, I thought it should be a trivial thing to write a little script that could scaffold out the CHANGELOG and set it up for the next release, using your prescribed format. Obviously, this doesn't help get people to actually track the changes in the log... but, I've found the less people have to remember, the better.
It's pretty "hacked" together, I'm sure there's some REGEX wizards out there who could come up with less fragile code, and I'm sure my JavaScript is far from "well written". Also, my project is in GitLab, so the "compare" url would have to be adjusted for GitHub, Bitbucket, etc.
We just use npm scripts for our build system, so it's as easy as tying into the default npm version
script in the package.json
:
"scripts": {
"changelog": "node scripts/changelog.js",
"version": "npm run changelog && git add .",
...
},
And then the actual changelog.js
script:
(function () {
'use strict';
var replace = require('../node_modules/replace-in-file');
var version = require('../package.json').version;
var url = require('../package.json').repository.url.slice(0, -4) + '/compare/v';
var now = new Date();
var mm = now.getMonth() + 1;
mm = (mm < 10) ? '0' + mm : mm;
var options = {
files: 'CHANGELOG.md',
from: [
/Unreleased/,
/develop\)/,
/##/
],
to: [
version,
'v' + version + ') - ' + now.getFullYear() + '-' + mm + '-' + now.getDate(),
'## [Unreleased](' + url + version + '...develop)' + '\r\n\r\n' + '##'
],
encoding: 'utf8'
};
replace(options).then(function (changedFiles) {
console.log('Modified files:', changedFiles.join(', '));
}, function (error) {
console.error('Error occurred:', error);
});
})();
Then, running npm version minor
would turn your CHANGELOG.md from this:
## [Unreleased](https://github.com/olivierlacan/keep-a-changelog/compare/v0.1.0...develop)
### Added
- Add a thing
- Add another thing
### Fixed
- Fixed a thing
- Fixed a really ugly thing
### Removed
- Removed a really bad thing
into this:
## [Unreleased](https://github.com/olivierlacan/keep-a-changelog/compare/v0.2.0...develop)
## [0.2.0](https://github.com/olivierlacan/keep-a-changelog/compare/v0.1.0...v0.2.0) - 2017-05-22
### Added
- Add a thing
- Add another thing
### Fixed
- Fixed a thing
- Fixed a really ugly thing
### Removed
- Removed a really bad thing
I don't know, just an idea...
Cheers! And, thanks for helping to create consistency!
Yesterday I've got an exactly same idea since I've constantly was forgetting to update changelog version after npm version
command.
Here is our own flavor.
If someone finds it useful — let me know, we'll move it into standalone NPM module.
I just recently released https://github.com/brightcove/kacl for handling this as we're starting to move more of our projects towards the Keep a Changelog format. I've included a section on configuring package.json scripts ( https://github.com/brightcove/kacl#usage-in-packagejson ), and have also had a lot of success pairing it with gh-release for fully automating releases. Feel free to check it out, feedback is definitely welcome!
Same here, I wrote https://github.com/ianfixes/keepachangelog_manager_gem
It seems similar to what @tjaneczko did, but written in ruby.