-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(changelog): add changelog style configs (#669)
* ✨ feat(changelog): add configs `showAuthorAvatar` `showSummary` `reduceHeadingLevel` `newlineTimestamp` `addBackToTop` * ✅ test(changelog): fix changelog test * 🛠️ chore(changelog): clean * ✨ feat(changelog): better summary style * ✨ feat(changelog): add summary avatar list * 💄 style(changelog): better summary layout * 🐛 fix(changelog): footer position * 🐛 fix(changelog): fix summary spacing * 💄 style(changelog): fix typo * 📝 docs(changlog): update README.md * ✅ test(changelog): add `finalizeContext` and `transformer` tests
- Loading branch information
1 parent
678d734
commit f9e6585
Showing
17 changed files
with
446 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ const base = require('../../.changelogrc'); | |
|
||
module.exports = { | ||
...base, | ||
scopeDisplayName: { | ||
'*': 'misc', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/changelog/src/finalizeContext/__snapshots__/index.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`test transformCommitGroups should transform commitGroups correctly 1`] = ` | ||
{ | ||
"authors": [ | ||
{ | ||
"authorEmail": "test@test.com", | ||
"authorName": "test", | ||
"authorNameEncode": "test", | ||
}, | ||
], | ||
"commitGroups": [ | ||
{ | ||
"commits": [ | ||
{ | ||
"authorEmail": "test@test.com", | ||
"authorName": "test", | ||
"authorNameEncode": "test", | ||
"first": true, | ||
"hash": "1234", | ||
"last": true, | ||
"scope": "test", | ||
"subject": "test commit", | ||
"title": "✨ Features", | ||
}, | ||
], | ||
"subtitle": "What's improved", | ||
"title": "✨ Features", | ||
}, | ||
], | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { Context } from 'conventional-changelog-writer'; | ||
import { CustomConfig } from '../customConfig'; | ||
import { typeMap } from '../transformer/typeDisplayName'; | ||
|
||
describe('test transformCommitGroups', () => { | ||
const customConfig: CustomConfig = { | ||
scopeDisplayName: { | ||
'*': 'all', | ||
}, | ||
}; | ||
const context: Context = { | ||
commitGroups: [ | ||
{ | ||
title: '✨ Features', | ||
commits: [ | ||
{ | ||
hash: '1234', | ||
subject: 'test commit', | ||
scope: 'test', | ||
title: '✨ Features', | ||
authorName: 'test', | ||
authorEmail: 'test@test.com', | ||
authorNameEncode: 'test', | ||
}, | ||
], | ||
}, | ||
], | ||
authors: [], | ||
}; | ||
|
||
it('should transform commitGroups correctly', () => { | ||
const subCommitScope = customConfig?.scopeDisplayName?.['*'] || ''; | ||
const authors = {}; | ||
context.commitGroups = context.commitGroups.map((item) => { | ||
const subtitle = Object.values(typeMap).find( | ||
(i) => | ||
item.title.includes(i['emoji']) || | ||
item.title.includes(i['en-US']) || | ||
item.title.includes(i['zh-CN']), | ||
).subtitle; | ||
let group; | ||
let commits = item.commits.sort((a, b) => { | ||
if (a.scope === subCommitScope && b.scope === subCommitScope) { | ||
return 0; | ||
} else if (a.scope === subCommitScope) { | ||
return 1; | ||
} else if (b.scope === subCommitScope) { | ||
return -1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
commits = commits.map((c, index) => { | ||
if (group !== c.scope) { | ||
group = c.scope; | ||
c.first = true; | ||
} else { | ||
c.first = false; | ||
} | ||
if (!commits[index + 1] || group !== commits[index + 1].scope) { | ||
c.last = true; | ||
} else { | ||
c.last = false; | ||
} | ||
if (c.authorNameEncode && !authors[c.authorNameEncode]) { | ||
authors[c.authorNameEncode] = { | ||
authorName: c.authorName, | ||
authorEmail: c.authorEmail, | ||
authorNameEncode: c.authorNameEncode, | ||
}; | ||
} | ||
return c; | ||
}); | ||
return { | ||
title: item.title, | ||
subtitle, | ||
commits, | ||
}; | ||
}); | ||
context.authors = Object.values(authors); | ||
expect(context).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { Context } from 'conventional-changelog-writer'; | ||
import { CustomConfig } from '../customConfig'; | ||
import { typeMap } from '../transformer/typeDisplayName'; | ||
export default (customConfig: CustomConfig) => (context: Context): Context => { | ||
const subCommitScope = customConfig?.scopeDisplayName?.['*'] || ''; | ||
const authors = {}; | ||
context.commitGroups = context.commitGroups.map((item) => { | ||
const subtitle = Object.values(typeMap).find( | ||
(i) => | ||
item.title.includes(i['emoji']) || | ||
item.title.includes(i['en-US']) || | ||
item.title.includes(i['zh-CN']), | ||
).subtitle; | ||
let group; | ||
let commits = item.commits.sort((a, b) => { | ||
if (a.scope === subCommitScope && b.scope === subCommitScope) { | ||
return 0; | ||
} else if (a.scope === subCommitScope) { | ||
return 1; | ||
} else if (b.scope === subCommitScope) { | ||
return -1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
commits = commits.map((c, index) => { | ||
if (group !== c.scope) { | ||
group = c.scope; | ||
c.first = true; | ||
} else { | ||
c.first = false; | ||
} | ||
if (!commits[index + 1] || group !== commits[index + 1].scope) { | ||
c.last = true; | ||
} else { | ||
c.last = false; | ||
} | ||
if (c.authorNameEncode && !authors[c.authorNameEncode]) { | ||
authors[c.authorNameEncode] = { | ||
authorName: c.authorName, | ||
authorEmail: c.authorEmail, | ||
authorNameEncode: c.authorNameEncode, | ||
}; | ||
} | ||
return c; | ||
}); | ||
return { | ||
title: item.title, | ||
subtitle, | ||
commits, | ||
}; | ||
}); | ||
context.authors = Object.values(authors); | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,47 @@ | ||
import type { Options } from 'conventional-changelog-writer'; | ||
import { readFileSync } from 'fs'; | ||
import { resolve } from 'path'; | ||
|
||
import type { CustomConfig } from './customConfig'; | ||
|
||
import finalizeContext from './finalizeContext'; | ||
import transformer from './transformer'; | ||
|
||
const basePath = resolve(__dirname, './templates'); | ||
|
||
const template = readFileSync(`${basePath}/template.hbs`, 'utf-8'); | ||
const summaryTemplate = readFileSync(`${basePath}/summary-template.hbs`, 'utf-8'); | ||
const header = readFileSync(`${basePath}/header.hbs`, 'utf-8'); | ||
const headerNewlineTimestamp = readFileSync(`${basePath}/header-newline-timestamp.hbs`, 'utf-8'); | ||
const commit = readFileSync(`${basePath}/commit.hbs`, 'utf-8'); | ||
const footer = readFileSync(`${basePath}/footer.hbs`, 'utf-8'); | ||
const author = readFileSync(`${basePath}/author.hbs`, 'utf-8'); | ||
|
||
export default (customConfig: CustomConfig): Options => ({ | ||
transform: transformer(customConfig), | ||
groupBy: 'type', | ||
commitGroupsSort: 'title', | ||
commitsSort: ['scope', 'subject'], | ||
noteGroupsSort: 'title', | ||
mainTemplate: template, | ||
headerPartial: header, | ||
// 替换 commit.hbs 模板中的 gitUserInfo | ||
commitPartial: commit.replace(/{{gitUserInfo}}/g, customConfig.showAuthor ? author : ''), | ||
footerPartial: footer, | ||
}); | ||
const authorAvatar = readFileSync(`${basePath}/author-avatar.hbs`, 'utf-8'); | ||
const backToTop = readFileSync(`${basePath}/back-to-top.hbs`, 'utf-8'); | ||
const reduceHeadingLevel = (skip: boolean, template: string): string => { | ||
if (skip) return template; | ||
return template.replace(/(^|\n)(#+)/g, (match, p1, p2) => p1 + '#' + p2); | ||
}; | ||
export default (customConfig: CustomConfig): Options => { | ||
const mainTemplate = customConfig.showSummary ? summaryTemplate : template; | ||
const commitPartial = commit.replace( | ||
/{{gitUserInfo}}/g, | ||
customConfig.showAuthor ? (customConfig.showAuthorAvatar ? authorAvatar : author) : '', | ||
); | ||
const headerPartial = customConfig.newlineTimestamp ? headerNewlineTimestamp : header; | ||
const footerPartial = footer.replace( | ||
/{{backToTop}}/g, | ||
customConfig.addBackToTop ? backToTop : '', | ||
); | ||
return { | ||
transform: transformer(customConfig), | ||
groupBy: 'type', | ||
commitGroupsSort: 'title', | ||
commitsSort: ['scope', 'subject'], | ||
noteGroupsSort: 'title', | ||
mainTemplate: reduceHeadingLevel(!customConfig.reduceHeadingLevel, mainTemplate), | ||
headerPartial: reduceHeadingLevel(!customConfig.reduceHeadingLevel, headerPartial), | ||
// 替换 commit.hbs 模板中的 gitUserInfo | ||
commitPartial: reduceHeadingLevel(!customConfig.reduceHeadingLevel, commitPartial), | ||
footerPartial: reduceHeadingLevel(!customConfig.reduceHeadingLevel, footerPartial), | ||
finalizeContext: finalizeContext(customConfig), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#if authorName}} - by [<img width="16" height="16" src="https://avatars.githubusercontent.com/{{authorNameEncode}}" /> **{{authorName}}**](https://github.com/{{authorNameEncode}}){{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div align="right"> | ||
|
||
[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top) | ||
|
||
</div> |
Oops, something went wrong.