Skip to content

Commit 26f97ac

Browse files
committed
feat: add commit-plaintext changelog formatting
1 parent bcdc15b commit 26f97ac

4 files changed

Lines changed: 80 additions & 11 deletions

File tree

changelog-maker.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,35 @@ const stripAnsi = require('strip-ansi')
1010
const pkgtoId = require('pkg-to-id')
1111
const commitStream = require('commit-stream')
1212
const gitexec = require('gitexec')
13-
const commitToOutput = require('./commit-to-output')
13+
const { commitToOutput, formatType } = require('./commit-to-output')
1414
const groupCommits = require('./group-commits')
1515
const collectCommitLabels = require('./collect-commit-labels')
16-
const { isReleaseCommit } = require('./groups')
16+
const { isReleaseCommit, toGroups } = require('./groups')
1717
const pkg = require('./package.json')
1818
const debug = require('debug')(pkg.name)
1919
const argv = require('minimist')(process.argv.slice(2))
2020

2121
const quiet = argv.quiet || argv.q
22-
const simple = argv.simple || argv.s
2322
const help = argv.h || argv.help
2423
const commitUrl = argv['commit-url'] || 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
2524
const pkgFile = path.join(process.cwd(), 'package.json')
2625
const pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
2726
const pkgId = pkgtoId(pkgData)
2827

28+
const getFormat = () => {
29+
if (argv.simple || argv.s) {
30+
return formatType.SIMPLE
31+
} else if (argv.plaintext || argv.p) {
32+
return formatType.PLAINTEXT
33+
}
34+
return formatType.MARKDOWN
35+
}
36+
2937
const ghId = {
3038
user: argv._[0] || pkgId.user || 'nodejs',
3139
repo: argv._[1] || (pkgId.name && stripScope(pkgId.name)) || 'node'
3240
}
41+
3342
const gitcmd = 'git log --pretty=full --since="{{sincecmd}}" --until="{{untilcmd}}"'
3443
const commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
3544
const untilcmd = ''
@@ -119,9 +128,26 @@ function onCommitList (err, list) {
119128
list = groupCommits(list)
120129
}
121130

122-
list = list.map((commit) => {
123-
return commitToOutput(commit, simple, ghId, commitUrl)
124-
})
131+
const format = getFormat()
132+
if (format === formatType.PLAINTEXT) {
133+
const formatted = []
134+
135+
let currentGroup
136+
for (const commit of list) {
137+
const commitGroup = toGroups(commit.summary)
138+
if (currentGroup !== commitGroup) {
139+
formatted.push(`${commitGroup}:`)
140+
currentGroup = commitGroup
141+
}
142+
formatted.push(commitToOutput(commit, formatType.PLAINTEXT, ghId, commitUrl))
143+
}
144+
145+
list = formatted
146+
} else {
147+
list = list.map((commit) => {
148+
return commitToOutput(commit, format, ghId, commitUrl)
149+
})
150+
}
125151

126152
if (!quiet) {
127153
printCommits(list)

commit-to-output.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ function cleanMarkdown (txt) {
99
return txt.replace(/([_~*\\[\]<>])/g, '\\$1')
1010
}
1111

12+
const formatType = {
13+
PLAINTEXT: 'plaintext',
14+
MARKDOWN: 'markdown',
15+
SIMPLE: 'simple'
16+
}
17+
18+
function toStringPlaintext (data) {
19+
let s = ''
20+
s += (data.semver || []).length ? `(${data.semver.join(', ').toUpperCase()}) ` : ''
21+
22+
if (data.revert) {
23+
s += `Revert "${data.group}: ${data.summary} `
24+
} else {
25+
s += `${data.summary} `
26+
}
27+
28+
s += data.author ? `(${data.author}) ` : ''
29+
s += data.pr ? data.prUrl : ''
30+
31+
return ` * ${s.trim()}`
32+
}
33+
1234
function toStringSimple (data) {
1335
let s = ''
1436
s += `* [${data.sha.substr(0, 10)}] - `
@@ -47,7 +69,7 @@ function toStringMarkdown (data) {
4769
: s)
4870
}
4971

50-
function commitToOutput (commit, simple, ghId, commitUrl) {
72+
function commitToOutput (commit, format, ghId, commitUrl) {
5173
const data = {}
5274
const prUrlMatch = commit.prUrl && commit.prUrl.match(/^https?:\/\/.+\/([^/]+\/[^/]+)\/\w+\/\d+$/i)
5375
const urlHash = `#${commit.ghIssue}` || commit.prUrl
@@ -63,7 +85,16 @@ function commitToOutput (commit, simple, ghId, commitUrl) {
6385
data.pr = prUrlMatch && ((prUrlMatch[1] !== `${ghId.user}/${ghId.repo}` ? prUrlMatch[1] : '') + urlHash)
6486
data.prUrl = prUrlMatch && commit.prUrl
6587

66-
return (simple ? toStringSimple : toStringMarkdown)(data)
88+
if (format === formatType.SIMPLE) {
89+
return toStringSimple(data)
90+
} else if (format === formatType.PLAINTEXT) {
91+
return toStringPlaintext(data)
92+
}
93+
94+
return toStringMarkdown(data)
6795
}
6896

69-
module.exports = commitToOutput
97+
module.exports = {
98+
commitToOutput,
99+
formatType
100+
}

group-commits.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { toGroups } = require('./groups')
44

5-
function groupCommits (list) {
5+
function groupCommits (list, plaintext = false) {
66
const groupList = list.reduce((groupList, commit) => {
77
const group = toGroups(commit.summary) || '*'
88

@@ -14,9 +14,11 @@ function groupCommits (list) {
1414
return groupList
1515
}, {})
1616

17-
return Object.keys(groupList).sort().reduce((p, group) => {
17+
const grouped = Object.keys(groupList).sort().reduce((p, group) => {
1818
return p.concat(groupList[group])
1919
}, [])
20+
21+
return grouped
2022
}
2123

2224
module.exports = groupCommits

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ test('test simple', (t) => {
4444
t.end()
4545
})
4646

47+
test('test plaintext', (t) => {
48+
t.equal(exec('--start-ref=9c700d2 --end-ref=dd937e9 --group --filter-release --plaintext'),
49+
`feature:
50+
* refactor and improve --commit-url (Rod Vagg)
51+
test:
52+
* update refs for testing (Rod Vagg)
53+
`)
54+
t.end()
55+
})
56+
4757
test('test group, semver labels, PR-URL', (t) => {
4858
t.equal(exec('--start-ref=v2.2.7 --end-ref=9c700d29 --group --filter-release --simple'),
4959
`* [cc442b6534] - (SEMVER-MINOR) minor nit (Rod Vagg) https://github.com/nodejs/node/pull/23715

0 commit comments

Comments
 (0)