Skip to content

Commit 2e634b9

Browse files
committed
fix: improve printed commit messages
1 parent 0e3777d commit 2e634b9

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

src/get-new-version.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,14 @@ export async function printRecentCommits(operation: Operation): Promise<void> {
216216
return
217217
}
218218

219-
const parsed = lines.map((line) => {
219+
interface ParsedCommit {
220+
hash: string
221+
tag: string
222+
message: string
223+
color: (c: string) => string
224+
}
225+
226+
const parsed = lines.map((line): ParsedCommit => {
220227
const [hash, ...parts] = line.split(' ')
221228
const message = parts.join(' ')
222229
const match = message.match(/^(\w+)(\([^)]+\))?(!)?:(.*)$/)
@@ -225,24 +232,41 @@ export async function printRecentCommits(operation: Operation): Promise<void> {
225232
if (match[3] === '!') {
226233
color = c.red
227234
}
228-
return [
229-
c.dim(hash),
230-
' ',
231-
c.bold(color([match[1], match[2], match[3]].filter(Boolean).join('').padStart(7, ' '))),
232-
c.dim(':'),
233-
' ',
234-
color === c.gray ? color(match[4].trim()) : match[4].trim(),
235-
].join('')
235+
const tag = [match[1], match[2], match[3]].filter(Boolean).join('')
236+
return {
237+
hash,
238+
tag,
239+
message: match[4].trim(),
240+
color,
241+
}
236242
}
237-
return `${c.gray(hash)} ${message}`
243+
return {
244+
hash,
245+
tag: '',
246+
message,
247+
color: c => c,
248+
}
249+
})
250+
const tagLength = parsed.map(({ tag }) => tag.length).reduce((a, b) => Math.max(a, b), 0)
251+
const prettified = parsed.map(({ hash, tag, message, color }) => {
252+
const paddedTag = tag.padStart(tagLength + 2, ' ')
253+
return [
254+
c.dim(hash),
255+
' ',
256+
color === c.gray ? color(paddedTag) : c.bold(color(paddedTag)),
257+
c.dim(':'),
258+
' ',
259+
color === c.gray ? color(message) : message,
260+
].join('')
238261
})
262+
239263
console.log()
240264
console.log(
241265
c.bold(
242266
`${c.green(lines.length)} Commits since ${c.gray(sha.slice(0, 7))}:`,
243267
),
244268
)
245269
console.log()
246-
console.log(parsed.join('\n'))
270+
console.log(prettified.reverse().join('\n'))
247271
console.log()
248272
}

0 commit comments

Comments
 (0)