Skip to content

Commit

Permalink
(refactor): support promises when printing the changelog
Browse files Browse the repository at this point in the history
- create streamToPromList to convert Streams into promisifed Lists
- wrap the changelog exec in a function
  - and wrap that in streamToPromList so it returns a Promise
- change onCommitList to return a Promise and accept only a list as arg
  - as it's call is wrapped in streamToPromList, no need to catch err
  - this style / no change in indentation is just to reduce the diff
  • Loading branch information
agilgur5 committed Jul 22, 2019
1 parent 867cf90 commit 199b57c
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions changelog-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,12 @@ function printCommits (list) {
process.stdout.write(out)
}

function onCommitList (err, list) {
if (err) {
throw err
}

function onCommitList (list) {
list = organiseCommits(list)

collectCommitLabels(list, function (err) {
if (err) {
throw err
}
// eslint-disable-next-line brace-style
return new Promise((resolve, reject) => { collectCommitLabels(list, function (err) {
if (err) { reject(err) }

if (argv.group) {
list = groupCommits(list)
Expand All @@ -122,7 +117,9 @@ function onCommitList (err, list) {
if (!quiet) {
printCommits(list)
}
})

resolve()
})}) // eslint-disable-line brace-style, block-spacing
}

let _startrefcmd = replace(refcmd, { ref: argv['start-ref'] || defaultRef })
Expand All @@ -137,7 +134,22 @@ debug('%s', _sincecmd)
debug('%s', _untilcmd)
debug('%s', _gitcmd)

gitexec.exec(process.cwd(), _gitcmd)
.pipe(split2())
.pipe(commitStream(ghId.user, ghId.repo))
.pipe(list.obj(onCommitList))
// convert a Stream into a ListStream, then List, then Promisify that List
function streamToPromList (stream) {
return new Promise((resolve, reject) => {
stream.pipe(list.obj((err, list) => {
if (err) { reject(err) }
resolve(list)
}))
})
}

// print the changelog
function printChangelog () {
return streamToPromList(gitexec.exec(process.cwd(), _gitcmd)
.pipe(split2())
.pipe(commitStream(ghId.user, ghId.repo)))
.then(onCommitList)
}

printChangelog()

0 comments on commit 199b57c

Please sign in to comment.