Skip to content

Commit

Permalink
feat(conventional-changelog-writer)!: rewrite to TypeScript (#1150)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen committed Dec 2, 2023
1 parent 600a8ae commit 8af364f
Show file tree
Hide file tree
Showing 53 changed files with 3,531 additions and 2,553 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"version": "1.0.0",
"devDependencies": {
"@trigen/eslint-config": "8.0.0-alpha.28",
"@trigen/eslint-config": "8.0.0-alpha.29",
"@types/node": "^20.5.9",
"@vitest/coverage-v8": "^0.34.0",
"better-than-before": "^1.0.0",
Expand Down
67 changes: 36 additions & 31 deletions packages/conventional-changelog-angular/writerOpts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,60 +26,64 @@ function getWriterOpts () {
return {
transform: (commit, context) => {
let discard = true
const issues = []

commit.notes.forEach(note => {
note.title = 'BREAKING CHANGES'
const notes = commit.notes.map(note => {
discard = false

return {
...note,
title: 'BREAKING CHANGES'
}
})

let type = commit.type

if (commit.type === 'feat') {
commit.type = 'Features'
type = 'Features'
} else if (commit.type === 'fix') {
commit.type = 'Bug Fixes'
type = 'Bug Fixes'
} else if (commit.type === 'perf') {
commit.type = 'Performance Improvements'
type = 'Performance Improvements'
} else if (commit.type === 'revert' || commit.revert) {
commit.type = 'Reverts'
type = 'Reverts'
} else if (discard) {
return
} else if (commit.type === 'docs') {
commit.type = 'Documentation'
type = 'Documentation'
} else if (commit.type === 'style') {
commit.type = 'Styles'
type = 'Styles'
} else if (commit.type === 'refactor') {
commit.type = 'Code Refactoring'
type = 'Code Refactoring'
} else if (commit.type === 'test') {
commit.type = 'Tests'
type = 'Tests'
} else if (commit.type === 'build') {
commit.type = 'Build System'
type = 'Build System'
} else if (commit.type === 'ci') {
commit.type = 'Continuous Integration'
type = 'Continuous Integration'
}

if (commit.scope === '*') {
commit.scope = ''
}
const scope = commit.scope === '*' ? '' : commit.scope
const shortHash = typeof commit.hash === 'string'
? commit.hash.substring(0, 7)
: commit.shortHash

if (typeof commit.hash === 'string') {
commit.shortHash = commit.hash.substring(0, 7)
}
const issues = []
let subject = commit.subject

if (typeof commit.subject === 'string') {
if (typeof subject === 'string') {
let url = context.repository
? `${context.host}/${context.owner}/${context.repository}`
: context.repoUrl
if (url) {
url = `${url}/issues/`
// Issue URLs.
commit.subject = commit.subject.replace(/#([0-9]+)/g, (_, issue) => {
subject = subject.replace(/#([0-9]+)/g, (_, issue) => {
issues.push(issue)
return `[#${issue}](${url}${issue})`
})
}
if (context.host) {
// User URLs.
commit.subject = commit.subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, username) => {
subject = subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, username) => {
if (username.includes('/')) {
return `@${username}`
}
Expand All @@ -90,15 +94,16 @@ function getWriterOpts () {
}

// remove references that already appear in the subject
commit.references = commit.references.filter(reference => {
if (issues.indexOf(reference.issue) === -1) {
return true
}
const references = commit.references.filter(reference => !issues.includes(reference.issue))

return false
})

return commit
return {
notes,
type,
scope,
shortHash,
subject,
references
}
},
groupBy: 'type',
commitGroupsSort: 'title',
Expand Down
24 changes: 13 additions & 11 deletions packages/conventional-changelog-atom/writerOpts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ function getWriterOpts () {
return
}

commit.emoji = commit.emoji.substring(0, 72)
const emojiLength = commit.emoji.length

if (typeof commit.hash === 'string') {
commit.shortHash = commit.hash.substring(0, 7)
}

if (typeof commit.shortDesc === 'string') {
commit.shortDesc = commit.shortDesc.substring(0, 72 - emojiLength)
const emoji = commit.emoji.substring(0, 72)
const emojiLength = emoji.length
const shortHash = typeof commit.hash === 'string'
? commit.hash.substring(0, 7)
: commit.shortHash
const shortDesc = typeof commit.shortDesc === 'string'
? commit.shortDesc.substring(0, 72 - emojiLength)
: undefined

return {
emoji,
shortHash,
shortDesc
}

return commit
},
groupBy: 'emoji',
commitGroupsSort: 'title',
Expand Down
10 changes: 6 additions & 4 deletions packages/conventional-changelog-codemirror/writerOpts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ function getWriterOpts () {
return
}

if (typeof commit.hash === 'string') {
commit.shortHash = commit.hash.substring(0, 7)
}
const shortHash = typeof commit.hash === 'string'
? commit.hash.substring(0, 7)
: commit.shortHash

return commit
return {
shortHash
}
},
groupBy: 'language',
commitGroupsSort: 'title',
Expand Down
11 changes: 8 additions & 3 deletions packages/conventional-changelog-conventionalcommits/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ export function addBangNotes (commit) {
const match = commit.header.match(breakingHeaderPattern)
if (match && commit.notes.length === 0) {
const noteText = match[3] // the description of the change.
commit.notes.push({
text: noteText
})

return [
{
text: noteText
}
]
}

return commit.notes
}
57 changes: 31 additions & 26 deletions packages/conventional-changelog-conventionalcommits/writerOpts.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getWriterOpts (config) {
// adds additional breaking change notes
// for the special case, test(system)!: hello world, where there is
// a '!' but no 'BREAKING CHANGE' in body:
addBangNotes(commit)
let notes = addBangNotes(commit)

// Add an entry in the CHANGELOG if special Release-As footer
// is used:
Expand All @@ -87,32 +87,36 @@ function getWriterOpts (config) {
discard = false
}

commit.notes.forEach(note => {
note.title = 'BREAKING CHANGES'
notes = notes.map(note => {
discard = false

return {
...note,
title: 'BREAKING CHANGES'
}
})

// breaking changes attached to any type are still displayed.
if (discard && (entry === undefined ||
entry.hidden)) return

if (entry) commit.type = entry.section

if (commit.scope === '*') {
commit.scope = ''
}

if (typeof commit.hash === 'string') {
commit.shortHash = commit.hash.substring(0, 7)
}

if (typeof commit.subject === 'string') {
const type = entry
? entry.section
: commit.type
const scope = commit.scope === '*'
? ''
: commit.scope
const shortHash = typeof commit.hash === 'string'
? commit.hash.substring(0, 7)
: commit.shortHash
let subject = commit.subject

if (typeof subject === 'string') {
// Issue URLs.
config.issuePrefixes.join('|')
const issueRegEx = '(' + config.issuePrefixes.join('|') + ')' + '([a-z0-9]+)'
const re = new RegExp(issueRegEx, 'g')

commit.subject = commit.subject.replace(re, (_, prefix, issue) => {
subject = subject.replace(re, (_, prefix, issue) => {
issues.push(prefix + issue)
const url = expandTemplate(config.issueUrlFormat, {
host: context.host,
Expand All @@ -124,7 +128,7 @@ function getWriterOpts (config) {
return `[${prefix}${issue}](${url})`
})
// User URLs.
commit.subject = commit.subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, user) => {
subject = subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, user) => {
// TODO: investigate why this code exists.
if (user.includes('/')) {
return `@${user}`
Expand All @@ -142,15 +146,16 @@ function getWriterOpts (config) {
}

// remove references that already appear in the subject
commit.references = commit.references.filter(reference => {
if (issues.indexOf(reference.prefix + reference.issue) === -1) {
return true
}

return false
})

return commit
const references = commit.references.filter(reference => !issues.includes(reference.prefix + reference.issue))

return {
notes,
type,
scope,
shortHash,
subject,
references
}
},
groupBy: 'type',
// the groupings of commit messages, e.g., Features vs., Bug Fixes, are
Expand Down
4 changes: 2 additions & 2 deletions packages/conventional-changelog-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { execFileSync } from 'child_process'
import addStream from 'add-stream'
import gitRawCommits from 'git-raw-commits'
import { parseCommitsStream } from 'conventional-commits-parser'
import conventionalChangelogWriter from 'conventional-changelog-writer'
import { createChangelogWriterStream } from 'conventional-changelog-writer'
import mergeConfig from './lib/merge-config.js'

export default function conventionalChangelog (options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) {
Expand Down Expand Up @@ -117,7 +117,7 @@ export default function conventionalChangelog (options, context, gitRawCommitsOp
err.message = 'Error in options.transform: ' + err.message
setImmediate(readable.emit.bind(readable), 'error', err)
})
.pipe(conventionalChangelogWriter(context, writerOpts))
.pipe(createChangelogWriterStream(context, writerOpts, writerOpts.includeDetails))
.on('error', function (err) {
err.message = 'Error in conventional-changelog-writer: ' + err.message
setImmediate(readable.emit.bind(readable), 'error', err)
Expand Down
23 changes: 14 additions & 9 deletions packages/conventional-changelog-ember/writerOpts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,30 @@ function getWriterOpts () {
return
}

let tag = commit.tag

if (commit.tag === 'BUGFIX') {
commit.tag = 'Bug Fixes'
tag = 'Bug Fixes'
} else if (commit.tag === 'CLEANUP') {
commit.tag = 'Cleanup'
tag = 'Cleanup'
} else if (commit.tag === 'FEATURE') {
commit.tag = 'Features'
tag = 'Features'
} else if (commit.tag === 'DOC') {
commit.tag = 'Documentation'
tag = 'Documentation'
} else if (commit.tag === 'SECURITY') {
commit.tag = 'Security'
tag = 'Security'
} else {
return
}

if (typeof commit.hash === 'string') {
commit.shortHash = commit.hash.substring(0, 7)
}
const shortHash = typeof commit.hash === 'string'
? commit.hash.substring(0, 7)
: commit.shortHash

return commit
return {
tag,
shortHash
}
},
groupBy: 'tag',
commitGroupsSort: 'title',
Expand Down
6 changes: 4 additions & 2 deletions packages/conventional-changelog-eslint/writerOpts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ function getWriterOpts () {
return
}

commit.shortHash = commit.hash.substring(0, 7)
const shortHash = commit.hash.substring(0, 7)

return commit
return {
shortHash
}
},
groupBy: 'tag',
commitGroupsSort: 'title',
Expand Down
10 changes: 7 additions & 3 deletions packages/conventional-changelog-express/writerOpts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ export async function createWriterOpts () {
function getWriterOpts () {
return {
transform: (commit) => {
let component = commit.component

if (commit.component === 'perf') {
commit.component = 'Performance'
component = 'Performance'
} else if (commit.component === 'deps') {
commit.component = 'Dependencies'
component = 'Dependencies'
} else {
return
}

return commit
return {
component
}
},
groupBy: 'component',
commitGroupsSort: 'title',
Expand Down

0 comments on commit 8af364f

Please sign in to comment.