Created
September 13, 2016 14:43
-
-
Save simonrenoult/3b413bc1fe7f801f6b76527775c351b9 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
#!/usr/bin/env node | |
const exec = require('child_process').exec | |
const path = require('path') | |
const moment = require('moment') | |
const prependFile = require('prepend-file') | |
const pkg = require('./package') | |
const version = pkg.version | |
const SEP = '##_' | |
exec(`git log --pretty=format:"%ad${SEP}%s${SEP}%h" --date=short version-${version}..HEAD`, (err, stdout, stderr) => { | |
if (err) throw err | |
if (stderr) throw new Error(stderr) | |
const args = process.argv.slice(2) | |
const version = args && args[0] | |
if (!version) throw new Error('A version number must be provided') | |
const location = _getChangelogLocation() | |
const today = _getToday() | |
const commits = _parseCommits(stdout) | |
const message = _createMessage(version, today, commits) | |
prependFile(location, message, err => { | |
if (err) throw err | |
}) | |
function _getChangelogLocation () { | |
return path.resolve(__dirname, '..', 'changelog.md') | |
} | |
function _parseCommits (stdout) { | |
return stdout | |
.split('\n') | |
.map(commit => { | |
const chunks = commit.split(SEP) | |
return { | |
hash: chunks[2], | |
date: chunks[0], | |
message: { | |
type: /(\w+)(\((\w+)\))?: (.*)/.exec(chunks[1])[1], | |
package: /(\w+)(\((\w+)\))?: (.*)/.exec(chunks[1])[3], | |
title: /(\w+)(\((\w+)\))?: (.*)/.exec(chunks[1])[4] | |
} | |
} | |
}) | |
} | |
function _getToday () { | |
return moment().format('YYYY-MM-DD') | |
} | |
function _formatMessage (commit) { | |
const commitPackage = commit.message.package | |
? ' ' + commit.message.package + ':' | |
: '' | |
return `*${commitPackage} ${commit.message.title} (${commit.hash})` | |
} | |
function _createMessage (version, date, commits) { | |
const bugfixes = commits.filter(commit => ['fix', 'bugfix'].indexOf(commit.message.type) !== -1) | |
const features = commits.filter(commit => ['feat'].indexOf(commit.message.type) !== -1) | |
return `## ${version} (${date}) | |
### Features | |
${features.map(_formatMessage).join('\n')} | |
### Bugs fixed | |
${bugfixes.map(_formatMessage).join('\n')} | |
` | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment