Skip to content

Commit

Permalink
feat!(conventional-changelog-writer): support async `options.transfor…
Browse files Browse the repository at this point in the history
…m` and `options.finalizeContext` #471 (#1065)

BREAKING CHANGE: `conventionalChangelogWriter.parseArray` now returns Promise

Co-authored-by: dangreen <danon0404@gmail.com>
  • Loading branch information
1aron and dangreen committed Aug 26, 2023
1 parent bf28e5f commit ced3077
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createConventionalRecommendedBumpOpts (parserOpts) {
})

return {
level: level,
level,
reason: breakings === 1
? `There is ${breakings} BREAKING CHANGE and ${features} features`
: `There are ${breakings} BREAKING CHANGES and ${features} features`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createConventionalRecommendedBumpOpts (parserOpts) {
})

return {
level: level,
level,
reason: `There are ${breakings} BREAKING CHANGES and ${features} features`
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createConventionalRecommendedBumpOpts (parserOpts) {
})

return {
level: level,
level,
reason: `There are ${breakings} BREAKING CHANGES and ${features} features`
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createConventionalRecommendedBumpOpts (parserOpts) {
})

return {
level: level,
level,
reason: `There are ${breakings} BREAKING CHANGES and ${features} features`
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function createConventionalRecommendedBumpOpts (parserOpts) {
})

return {
level: level,
level,
reason: `There are ${breakings} breaking changes and ${features} features`
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createConventionalRecommendedBumpOpts (parserOpts) {
})

return {
level: level,
level,
reason: `There are ${breakings} BREAKING CHANGES and ${features} features`
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createConventionalRecommendedBumpOpts (parserOpts) {
})

return {
level: level,
level,
reason: `There are ${breakings} BREAKING CHANGES and ${features} features`
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createConventionalRecommendedBumpOpts (parserOpts) {
})

return {
level: level,
level,
reason: `There are ${breakings} BREAKING CHANGES and ${features} features`
}
}
Expand Down
50 changes: 25 additions & 25 deletions packages/conventional-changelog-writer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ const { Transform } = require('stream')
const { join } = require('path')
const { readFileSync } = require('fs')
const { valid: semverValid } = require('semver')
const util = require('./lib/util')
const {
functionify,
processCommit,
generate
} = require('./lib/util')

// sv-SEis used for yyyy-mm-dd format
// sv-SE is used for yyyy-mm-dd format
const dateFormatter = Intl.DateTimeFormat('sv-SE', {
timeZone: 'UTC'
})
Expand All @@ -28,13 +32,9 @@ function conventionalChangelogWriterInit (context, options) {
commitsSort: 'header',
noteGroupsSort: 'title',
notesSort: 'text',
generateOn: function (commit) {
return semverValid(commit.version)
},
finalizeContext: function (context) {
return context
},
debug: function () {},
generateOn: commit => semverValid(commit.version),
finalizeContext: context => context,
debug: () => {},
reverse: false,
includeDetails: false,
ignoreReverted: true,
Expand Down Expand Up @@ -78,10 +78,10 @@ function conventionalChangelogWriterInit (context, options) {
}
}

options.commitGroupsSort = util.functionify(options.commitGroupsSort)
options.commitsSort = util.functionify(options.commitsSort)
options.noteGroupsSort = util.functionify(options.noteGroupsSort)
options.notesSort = util.functionify(options.notesSort)
options.commitGroupsSort = functionify(options.commitGroupsSort)
options.commitsSort = functionify(options.commitsSort)
options.noteGroupsSort = functionify(options.noteGroupsSort)
options.notesSort = functionify(options.notesSort)

return { context, options, generateOn }
}
Expand All @@ -97,10 +97,10 @@ function conventionalChangelogWriterParseStream (context, options) {
return new Transform({
objectMode: true,
highWaterMark: 16,
transform (chunk, _enc, cb) {
async transform (chunk, _enc, cb) {
try {
let result
const commit = util.processCommit(chunk, options.transform, context)
const commit = await processCommit(chunk, options.transform, context)
const keyCommit = commit || chunk

// previous blocks of logs
Expand All @@ -111,11 +111,11 @@ function conventionalChangelogWriterParseStream (context, options) {

if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false
result = util.generate(options, commits, context, keyCommit)
result = await generate(options, commits, context, keyCommit)
if (options.includeDetails) {
this.push({
log: result,
keyCommit: keyCommit
keyCommit
})
} else {
this.push(result)
Expand All @@ -126,7 +126,7 @@ function conventionalChangelogWriterParseStream (context, options) {
} else {
if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false
result = util.generate(options, commits, context, savedKeyCommit)
result = await generate(options, commits, context, savedKeyCommit)

if (!firstRelease || options.doFlush) {
if (options.includeDetails) {
Expand Down Expand Up @@ -154,14 +154,14 @@ function conventionalChangelogWriterParseStream (context, options) {
cb(err)
}
},
flush (cb) {
async flush (cb) {
if (!options.doFlush && (options.reverse || neverGenerated)) {
cb(null)
return
}

try {
const result = util.generate(options, commits, context, savedKeyCommit)
const result = await generate(options, commits, context, savedKeyCommit)

if (options.includeDetails) {
this.push({
Expand All @@ -183,7 +183,7 @@ function conventionalChangelogWriterParseStream (context, options) {
/*
* Given an array of commits, returns a string representing a CHANGELOG entry.
*/
conventionalChangelogWriterParseStream.parseArray = (rawCommits, context, options) => {
conventionalChangelogWriterParseStream.parseArray = async (rawCommits, context, options) => {
let generateOn
rawCommits = [...rawCommits];
({ context, options, generateOn } = conventionalChangelogWriterInit(context, options))
Expand All @@ -194,10 +194,10 @@ conventionalChangelogWriterParseStream.parseArray = (rawCommits, context, option
}
const entries = []
for (const rawCommit of rawCommits) {
const commit = util.processCommit(rawCommit, options.transform, context)
const commit = await processCommit(rawCommit, options.transform, context)
const keyCommit = commit || rawCommit
if (generateOn(keyCommit, commits, context, options)) {
entries.push(util.generate(options, commits, context, savedKeyCommit))
entries.push(await generate(options, commits, context, savedKeyCommit))
savedKeyCommit = keyCommit
commits = []
}
Expand All @@ -207,9 +207,9 @@ conventionalChangelogWriterParseStream.parseArray = (rawCommits, context, option
}
if (options.reverse) {
entries.reverse()
return util.generate(options, commits, context, savedKeyCommit) + entries.join('')
return await generate(options, commits, context, savedKeyCommit) + entries.join('')
} else {
return entries.join('') + util.generate(options, commits, context, savedKeyCommit)
return entries.join('') + await generate(options, commits, context, savedKeyCommit)
}
}

Expand Down
28 changes: 14 additions & 14 deletions packages/conventional-changelog-writer/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function getCommitGroups (groupBy, commits, groupsSort, commitsSort) {
}

commitGroups.push({
title: title,
commits: commits
title,
commits
})
})

Expand Down Expand Up @@ -110,7 +110,7 @@ function getNoteGroups (notes, noteGroupsSort, notesSort) {

if (!titleExists) {
retGroups.push({
title: title,
title,
notes: [note]
})
}
Expand Down Expand Up @@ -175,7 +175,7 @@ function cloneCommit (commit) {
return commitClone
}

function processCommit (chunk, transform, context) {
async function processCommit (chunk, transform, context) {
let commit

try {
Expand All @@ -185,7 +185,7 @@ function processCommit (chunk, transform, context) {
commit = cloneCommit(chunk)

if (typeof transform === 'function') {
commit = transform(commit, context)
commit = await transform(commit, context)

if (commit) {
commit.raw = chunk
Expand Down Expand Up @@ -225,7 +225,7 @@ function getExtraContext (commits, notes, options) {
return context
}

function generate (options, commits, context, keyCommit) {
async function generate (options, commits, context, keyCommit) {
const notes = []
let filteredCommits
const compiled = compileTemplates(options)
Expand Down Expand Up @@ -264,18 +264,18 @@ function generate (options, commits, context, keyCommit) {
context.isPatch = context.isPatch || semver.patch(context.version) !== 0
}

context = options.finalizeContext(context, options, filteredCommits, keyCommit, commits)
context = await options.finalizeContext(context, options, filteredCommits, keyCommit, commits)
options.debug('Your final context is:\n' + stringify(context, null, 2))

return compiled(context)
}

module.exports = {
compileTemplates: compileTemplates,
functionify: functionify,
getCommitGroups: getCommitGroups,
getNoteGroups: getNoteGroups,
processCommit: processCommit,
getExtraContext: getExtraContext,
generate: generate
compileTemplates,
functionify,
getCommitGroups,
getNoteGroups,
processCommit,
getExtraContext,
generate
}

0 comments on commit ced3077

Please sign in to comment.