Skip to content

Instantly share code, notes, and snippets.

@simonrenoult
Created September 13, 2016 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonrenoult/3b413bc1fe7f801f6b76527775c351b9 to your computer and use it in GitHub Desktop.
Save simonrenoult/3b413bc1fe7f801f6b76527775c351b9 to your computer and use it in GitHub Desktop.
#!/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