Skip to content

Commit

Permalink
Refactor code.
Browse files Browse the repository at this point in the history
* custom parser
* clear logic for format, parse
* rename bin scripts. `ezchangelogStream`.
  • Loading branch information
zoubin committed Nov 7, 2015
1 parent e37e0c4 commit bab9bcd
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 262 deletions.
4 changes: 4 additions & 0 deletions bin/cmd
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

git log "$@" --no-merges | ezchangelogStream

4 changes: 0 additions & 4 deletions bin/ezchangelog

This file was deleted.

67 changes: 45 additions & 22 deletions bin/stream.js
Expand Up @@ -2,44 +2,49 @@

var fs = require('fs')
var minimist = require('minimist')
var parser = require('../lib/parser')
var format = require('../lib/format')
var changeLog = require('..')
var path = require('path')

var argv = minimist(
process.argv.slice(2),
{
string: ['file'],
boolean: ['print'],
string: ['out'],
boolean: ['print', 'incremental'],
'default': {
incremental: true,
},
alias: {
p: 'print',
f: 'file',
o: 'out',
inc: 'incremental',
},
}
)

Promise.resolve(argv.file)
.then(function (file) {
if (file) {
return file
}
return require('../lib/config')().changelog
})
.catch(function () {
})
.then(function (file) {
file = file || 'changelog.md'
return readFile(file)
getConfig(argv.out)
.then(function (conf) {
return readFile(conf.out)
.then(function (src) {
return { file: file, source: src }
conf.source = src
return conf
})
})
.then(function (row) {
.then(function (conf) {
var dest = argv.print
? process.stdout
: fs.createWriteStream(row.file)
: fs.createWriteStream(conf.out)
var formatter = changeLog.format({ history: conf.source })
if (!argv.incremental) {
formatter.get('filter').pop()
formatter.get('wrap').pop()
} else if (argv.print) {
formatter.get('wrap').pop()
}
process.stdin
.pipe(parser())
.pipe(format(row.source))
.pipe(changeLog.parse({
baseUrl: conf.baseUrl,
}))
.pipe(formatter)
.pipe(dest)
})

Expand All @@ -50,3 +55,21 @@ function readFile(file) {
})
})
}

function getConfig(out) {
var noop = function () {}
return new Promise(function (resolve) {
resolve(
require(path.resolve('package.json')).changelog
)
})
.catch(noop)
.then(function (conf) {
conf = conf || {}
if (out) {
conf.out = out
}
conf.out = conf.out || 'changelog.md'
return conf
})
}
14 changes: 5 additions & 9 deletions changelog.md
Expand Up @@ -40,15 +40,11 @@

* [ 2015-11-06 [bcb97ae](https://github.com/zoubin/ezchangelog/commit/bcb97ae) ] Refactor code: support most `git log` options

>`ezchangelog-stream`: used in a unix pipeline
>`ezchangelog`: directly executed
>`git-log-parser` is no longer used in this package, cause it takes the
>responsibility of parsing `git log` arguments, which makes things a
>little messy.
* `ezchangelog-stream`: used in a unix pipeline
* `ezchangelog`: directly executed
* `git-log-parser` is no longer used in this package, cause it takes the
responsibility of parsing `git log` arguments, which makes things a
little messy.

* [ 2015-11-06 [3b1762e](https://github.com/zoubin/ezchangelog/commit/3b1762e) ] CAHNGELOG

Expand Down
3 changes: 3 additions & 0 deletions index.js
@@ -0,0 +1,3 @@

exports.parse = require('./lib/parse')
exports.format = require('./lib/format')
5 changes: 0 additions & 5 deletions lib/config.js

This file was deleted.

54 changes: 33 additions & 21 deletions lib/format.js
@@ -1,21 +1,40 @@
var thr = require('through2')
var combine = require('stream-combiner2')
var stringify = require('./stringify')
var splicer = require('labeled-stream-splicer')
var formatter = require('./formatter')

var HEADER_PREFIX = '<!-- LATEST '
var HEADER_SUFFIX = ' -->\n\n'
var HASH_OFFSET = HEADER_PREFIX.length

module.exports = function (history) {
module.exports = function (opts) {
var history = opts && opts.history
var header
var hasNewHeader = false
var lastCommit = history && getLastCommit(history)
var lastCommit = getLastCommit(history)

function prepend(s, _, next) {
function filter(ci, _, next) {
if (lastCommit && lastCommit === ci.commit.short) {
return this.push(null)
}
next(null, ci)
}

function collectHeader(ci, _, next) {
if (!header) {
header = getHeader(ci.commit.short)
}
next(null, ci)
}

function pushHeader(buf, _, next) {
if (!hasNewHeader && header) {
hasNewHeader = true
this.push(header)
}
next(null, buf)
}

function prepend(s, _, next) {
next(null, s)
}

Expand All @@ -30,21 +49,12 @@ module.exports = function (history) {
done()
}

function filter(ci, _, next) {
var hash = ci.commit.short
if (lastCommit && lastCommit === hash) {
return this.push(null)
}
if (!header) {
header = getHeader(hash)
}
next(null, ci)
}

return combine.obj([
thr.obj(filter),
stringify(),
thr(prepend, append),
return splicer.obj([
'filter', [ thr.obj(filter) ],
'header', [ thr.obj(collectHeader) ],
'format', [ formatter() ],
'pushHeader', [ thr(pushHeader) ],
'wrap', [ thr(prepend, append) ],
])
}

Expand All @@ -57,6 +67,8 @@ function hasHeader(s) {
}

function getLastCommit(history) {
return hasHeader(history) && history.substring(HASH_OFFSET, HASH_OFFSET + 7)
return history &&
hasHeader(history) &&
history.substring(HASH_OFFSET, HASH_OFFSET + 7)
}

32 changes: 32 additions & 0 deletions lib/formatter.js
@@ -0,0 +1,32 @@
var thr = require('through2')

module.exports = function () {
return thr.obj(function (ci, _, next) {
if (ci.tag) {
this.push(formatTag(ci))
} else {
this.push(formatMsg(ci))
}
next(null, '\n\n')
})
}

function formatTag(ci) {
return '## ' + linkify(ci.tag, ci.url) +
' (' + formatDate(ci.committer.date) + ')'
}

function formatMsg(ci) {
return '* [ ' + formatDate(ci.committer.date) +
' ' + linkify(ci.commit.short, ci.url) + ' ] ' +
ci.subject.trim() + (ci.body ? '\n' + ci.body : '')
}

function formatDate(d) {
return d.toISOString().slice(0, 10)
}

function linkify(text, href) {
return href && '[' + text + '](' + href + ')' || text
}

34 changes: 34 additions & 0 deletions lib/parse.js
@@ -0,0 +1,34 @@
var split = require('split2')
var combine = require('stream-combiner2')
var thr = require('through2')

module.exports = function (opts) {
opts = opts || {}
var parser = typeof opts.parser === 'function'
? opts.parser
: require('./parser')

return combine.obj(split(), group(), parser(opts))
}

function group() {
var ci
var COMMIT_LINE = /^commit \w{40}$/
return thr.obj(function (buf, _, next) {
var line = buf.toString('utf8')
if (COMMIT_LINE.test(line)) {
if (ci) {
this.push(ci)
}
ci = { raws: [ line ] }
} else if (ci) {
ci.raws.push(line)
}
next()
}, function (done) {
if (ci) {
this.push(ci)
}
done()
})
}

0 comments on commit bab9bcd

Please sign in to comment.