|
1 | 1 | import type { Options } from 'conventional-changelog-writer'; |
2 | 2 | import { readFileSync } from 'fs'; |
3 | 3 | import { resolve } from 'path'; |
4 | | - |
5 | 4 | import type { CustomConfig } from './customConfig'; |
6 | | - |
| 5 | +import finalizeContext from './finalizeContext'; |
7 | 6 | import transformer from './transformer'; |
8 | 7 |
|
9 | 8 | const basePath = resolve(__dirname, './templates'); |
10 | 9 |
|
11 | 10 | const template = readFileSync(`${basePath}/template.hbs`, 'utf-8'); |
| 11 | +const summaryTemplate = readFileSync(`${basePath}/summary-template.hbs`, 'utf-8'); |
12 | 12 | const header = readFileSync(`${basePath}/header.hbs`, 'utf-8'); |
| 13 | +const headerNewlineTimestamp = readFileSync(`${basePath}/header-newline-timestamp.hbs`, 'utf-8'); |
13 | 14 | const commit = readFileSync(`${basePath}/commit.hbs`, 'utf-8'); |
14 | 15 | const footer = readFileSync(`${basePath}/footer.hbs`, 'utf-8'); |
15 | 16 | const author = readFileSync(`${basePath}/author.hbs`, 'utf-8'); |
16 | | - |
17 | | -export default (customConfig: CustomConfig): Options => ({ |
18 | | - transform: transformer(customConfig), |
19 | | - groupBy: 'type', |
20 | | - commitGroupsSort: 'title', |
21 | | - commitsSort: ['scope', 'subject'], |
22 | | - noteGroupsSort: 'title', |
23 | | - mainTemplate: template, |
24 | | - headerPartial: header, |
25 | | - // 替换 commit.hbs 模板中的 gitUserInfo |
26 | | - commitPartial: commit.replace(/{{gitUserInfo}}/g, customConfig.showAuthor ? author : ''), |
27 | | - footerPartial: footer, |
28 | | -}); |
| 17 | +const authorAvatar = readFileSync(`${basePath}/author-avatar.hbs`, 'utf-8'); |
| 18 | +const backToTop = readFileSync(`${basePath}/back-to-top.hbs`, 'utf-8'); |
| 19 | +const reduceHeadingLevel = (skip: boolean, template: string): string => { |
| 20 | + if (skip) return template; |
| 21 | + return template.replace(/(^|\n)(#+)/g, (match, p1, p2) => p1 + '#' + p2); |
| 22 | +}; |
| 23 | +export default (customConfig: CustomConfig): Options => { |
| 24 | + const mainTemplate = customConfig.showSummary ? summaryTemplate : template; |
| 25 | + const commitPartial = commit.replace( |
| 26 | + /{{gitUserInfo}}/g, |
| 27 | + customConfig.showAuthor ? (customConfig.showAuthorAvatar ? authorAvatar : author) : '', |
| 28 | + ); |
| 29 | + const headerPartial = customConfig.newlineTimestamp ? headerNewlineTimestamp : header; |
| 30 | + const footerPartial = footer.replace( |
| 31 | + /{{backToTop}}/g, |
| 32 | + customConfig.addBackToTop ? backToTop : '', |
| 33 | + ); |
| 34 | + return { |
| 35 | + transform: transformer(customConfig), |
| 36 | + groupBy: 'type', |
| 37 | + commitGroupsSort: 'title', |
| 38 | + commitsSort: ['scope', 'subject'], |
| 39 | + noteGroupsSort: 'title', |
| 40 | + mainTemplate: reduceHeadingLevel(!customConfig.reduceHeadingLevel, mainTemplate), |
| 41 | + headerPartial: reduceHeadingLevel(!customConfig.reduceHeadingLevel, headerPartial), |
| 42 | + // 替换 commit.hbs 模板中的 gitUserInfo |
| 43 | + commitPartial: reduceHeadingLevel(!customConfig.reduceHeadingLevel, commitPartial), |
| 44 | + footerPartial: reduceHeadingLevel(!customConfig.reduceHeadingLevel, footerPartial), |
| 45 | + finalizeContext: finalizeContext(customConfig), |
| 46 | + }; |
| 47 | +}; |
0 commit comments