Skip to content

Commit

Permalink
fix(conventional-changelog-writer): fix transform async handlers (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen committed Sep 10, 2023
1 parent e170b47 commit be3901b
Showing 1 changed file with 104 additions and 72 deletions.
176 changes: 104 additions & 72 deletions packages/conventional-changelog-writer/index.js
Expand Up @@ -2,7 +2,7 @@

const { Transform } = require('stream')
const { join } = require('path')
const { readFileSync } = require('fs')
const { readFile } = require('fs/promises')
const { valid: semverValid } = require('semver')
const {
functionify,
Expand All @@ -15,7 +15,11 @@ const dateFormatter = Intl.DateTimeFormat('sv-SE', {
timeZone: 'UTC'
})

function conventionalChangelogWriterInit (context, options) {
function immediate () {
return new Promise(resolve => setImmediate(resolve))
}

async function conventionalChangelogWriterInit (context, options) {
context = {
commit: 'commits',
issue: 'issues',
Expand All @@ -27,6 +31,18 @@ function conventionalChangelogWriterInit (context, options) {
context.linkReferences = true
}

const [
mainTemplate,
headerPartial,
commitPartial,
footerPartial
] = await Promise.all([
readFile(join(__dirname, 'templates/template.hbs'), 'utf-8'),
readFile(join(__dirname, 'templates/header.hbs'), 'utf-8'),
readFile(join(__dirname, 'templates/commit.hbs'), 'utf-8'),
readFile(join(__dirname, 'templates/footer.hbs'), 'utf-8')
])

options = {
groupBy: 'type',
commitsSort: 'header',
Expand All @@ -39,10 +55,10 @@ function conventionalChangelogWriterInit (context, options) {
includeDetails: false,
ignoreReverted: true,
doFlush: true,
mainTemplate: readFileSync(join(__dirname, 'templates/template.hbs'), 'utf-8'),
headerPartial: readFileSync(join(__dirname, 'templates/header.hbs'), 'utf-8'),
commitPartial: readFileSync(join(__dirname, 'templates/commit.hbs'), 'utf-8'),
footerPartial: readFileSync(join(__dirname, 'templates/footer.hbs'), 'utf-8'),
mainTemplate,
headerPartial,
commitPartial,
footerPartial,
...options
}

Expand Down Expand Up @@ -86,9 +102,8 @@ function conventionalChangelogWriterInit (context, options) {
return { context, options, generateOn }
}

function conventionalChangelogWriterParseStream (context, options) {
let generateOn
({ context, options, generateOn } = conventionalChangelogWriterInit(context, options))
function conventionalChangelogWriterParseStream (inputContext, inputOptions) {
const initPromise = conventionalChangelogWriterInit(inputContext, inputOptions)
let commits = []
let neverGenerated = true
let savedKeyCommit
Expand All @@ -97,85 +112,102 @@ function conventionalChangelogWriterParseStream (context, options) {
return new Transform({
objectMode: true,
highWaterMark: 16,
async transform (chunk, _enc, cb) {
try {
let result
const commit = await processCommit(chunk, options.transform, context)
const keyCommit = commit || chunk

// previous blocks of logs
if (options.reverse) {
if (commit) {
commits.push(commit)
}

if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false
result = await generate(options, commits, context, keyCommit)
if (options.includeDetails) {
this.push({
log: result,
keyCommit
})
} else {
this.push(result)
// `transform` option should not return Promises.
// It cause a bug in Node.js 16, because it interprets the Promise resolve as a callback call.
// In Node 20 it handle only callback call, Promises is not handled.
transform (chunk, _enc, cb) {
(async () => {
try {
const { context, options, generateOn } = await initPromise
let result
const commit = await processCommit(chunk, options.transform, context)
const keyCommit = commit || chunk

// previous blocks of logs
if (options.reverse) {
if (commit) {
commits.push(commit)
}

commits = []
}
} else {
if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false
result = await generate(options, commits, context, savedKeyCommit)
if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false
result = await generate(options, commits, context, keyCommit)

await immediate()

if (!firstRelease || options.doFlush) {
if (options.includeDetails) {
this.push({
log: result,
keyCommit: savedKeyCommit
keyCommit
})
} else {
this.push(result)
}

commits = []
}
} else {
if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false
result = await generate(options, commits, context, savedKeyCommit)

if (!firstRelease || options.doFlush) {
await immediate()

if (options.includeDetails) {
this.push({
log: result,
keyCommit: savedKeyCommit
})
} else {
this.push(result)
}
}

firstRelease = false
commits = []
savedKeyCommit = keyCommit
}
firstRelease = false
commits = []
savedKeyCommit = keyCommit
}

if (commit) {
commits.push(commit)
if (commit) {
commits.push(commit)
}
}
}

cb()
} catch (err) {
cb(err)
}
},
async flush (cb) {
if (!options.doFlush && (options.reverse || neverGenerated)) {
cb(null)
return
}

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

if (options.includeDetails) {
this.push({
log: result,
keyCommit: savedKeyCommit
})
} else {
this.push(result)
cb()
} catch (err) {
cb(err)
}
})()
},
flush (cb) {
(async () => {
try {
const { context, options } = await initPromise

if (!options.doFlush && (options.reverse || neverGenerated)) {
cb(null)
return
}

cb()
} catch (err) {
cb(err)
}
const result = await generate(options, commits, context, savedKeyCommit)

await immediate()

if (options.includeDetails) {
this.push({
log: result,
keyCommit: savedKeyCommit
})
} else {
this.push(result)
}

cb()
} catch (err) {
cb(err)
}
})()
}
})
}
Expand All @@ -186,7 +218,7 @@ function conventionalChangelogWriterParseStream (context, options) {
conventionalChangelogWriterParseStream.parseArray = async (rawCommits, context, options) => {
let generateOn
rawCommits = [...rawCommits];
({ context, options, generateOn } = conventionalChangelogWriterInit(context, options))
({ context, options, generateOn } = await conventionalChangelogWriterInit(context, options))
let commits = []
let savedKeyCommit
if (options.reverse) {
Expand Down

0 comments on commit be3901b

Please sign in to comment.