Skip to content

Commit

Permalink
Add changelog.js for CHANGELOG.md [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed Apr 25, 2016
1 parent 0303039 commit 29b883a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict'

// TODO: consider migrate to the "conventional-changelog-angular"

const url = require('./package.json').repository.url
const exec = require('child_process').exec

exec('git log --pretty=format:"%b"', (error, stdout, stderr) => {
if (error) {
console.error(error)
process.exit(1)
}

// "reverts commit 6537cab0bf940cf7b780a87c8c754d380b4cd5ba"
// -> "6537cab0bf940cf7b780a87c8c754d380b4cd5ba"
const pattern = 'reverts commit ([\\w\\d]{40})'
const strs = stdout.match(new RegExp(pattern, 'g')) || []
const reverts = strs.map((str) => (str.match(new RegExp(pattern)))[1])

const script = 'git log --pretty=format:"[%ad] %H %an : %s"'
exec(script, (error, stdout, stderr) => {
if (error) {
console.error(error)
process.exit(1)
}

const logs = []
stdout.split('\n').forEach((line) => {
const matches = line.match(/^\[(.+?)\] (\w+) (.+?) : (.+?)$/)
const date = new Date(matches[1]).toLocaleString()
const hash = matches[2]
const commiter = matches[3]
const subject = matches[4] || ''

if (reverts.indexOf(hash) > -1) {
return
}

const semver = subject.match(/^([\d.]+)/)
if (semver) {
const version = semver[0]
logs.push('')
logs.push(version)
logs.push('---')

const isTag = version === subject
if (isTag) {
return
}
}

const commitUrl = url.replace(/(.git|\/)$/, '') + '/commit/' + hash
const normalizeCommiter = commiter.replace('horse_n_deer', '59naga')
const issueUrlBase = url.replace(/(.git|\/)$/, '') + '/issues/'
const linkedDescription = subject.split('`').map((chunk, i) => {
if (i % 2 === 1) {
return chunk // ignore if code-block
}
return chunk.replace(/#([\d]+)/, (str, issueNumber) => {
return `[${str}](${issueUrlBase}${issueNumber})`
})
}).join('`')

let log = ` - [${date}](${commitUrl}) ${linkedDescription} by ${normalizeCommiter}`
logs.push(log)
})
process.stdout.write(logs.join('\n') + '\n')
process.exit(0)
})
})
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"cover:test": "nyc --reporter=lcov --reporter=text npm test",
"cover:report": "npm-if TRAVIS \"codeclimate-test-reporter < coverage/lcov.info\"",
"lint": "eslint src test",
"version": "node changelog.js > CHANGELOG.md && git add CHANGELOG.md",
"postversion": "git push --follow-tags",
"build": "abby compile --log --env"
},
"nyc": {
Expand Down

0 comments on commit 29b883a

Please sign in to comment.