Skip to content

Commit e062c06

Browse files
committed
feat(@142vip/changelog): 支持自定义版本名称
1 parent 80903ea commit e062c06

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

packages/changelog/src/changelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ cli
6161

6262
// 更新changelog文档
6363
if (typeof config.output === 'string') {
64-
await updateChangelog(config.output, markdown, config.to)
64+
await updateChangelog(config.output, markdown, config.name)
6565
return
6666
}
6767

packages/changelog/src/core/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@ export async function resolveConfig(options: ChangelogOptions) {
4444

4545
config.baseUrl = config.baseUrl ?? 'github.com'
4646
config.baseUrlApi = config.baseUrlApi ?? 'api.github.com'
47+
// 发布的版本
4748
config.to = config.to || await getCurrentGitBranch()
49+
// release name
50+
config.name = config.name ?? config.to
4851
config.from = config.from || await getLastMatchingTag(config.to) || await getFirstGitCommit()
4952
// @ts-expect-error backward compatibility
5053
config.repo = config.repo || config.github || await getGitHubRepo(config.baseUrl)
54+
55+
// 是否是预览版本
5156
config.prerelease = config.prerelease ?? isPrerelease(config.to)
5257

5358
// todo 支持多个scope生成

packages/changelog/src/core/github.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export async function resolveAuthorInfo(options: ChangelogOptions, info: AuthorI
8181
catch {
8282
}
8383
}
84-
8584
return info
8685
}
8786

packages/changelog/src/core/markdown.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ function formatReferences(references: Reference[], baseUrl: string, github: stri
1919
return ref.value
2020
if (ref.type === 'pull-request' || ref.type === 'issue')
2121
return `https://${baseUrl}/${github}/issues/${ref.value.slice(1)}`
22+
23+
// 截取前面5个字符
2224
return `[<samp>(${ref.value.slice(0, 5)})</samp>](https://${baseUrl}/${github}/commit/${ref.value})`
2325
})
2426

@@ -34,10 +36,15 @@ function formatLine(commit: Commit, options: ResolvedChangelogOptions) {
3436
const hashRefs = formatReferences(commit.references, options.baseUrl, options.repo as string, 'hash')
3537

3638
let authors = join([...new Set(commit.resolvedAuthors?.map(i => i.login ? `@${i.login}` : `**${i.name}**`))])?.trim()
39+
3740
if (authors)
3841
authors = `by ${authors}`
3942

40-
let refs = [authors, prRefs, hashRefs].filter(i => i?.trim()).join(' ')
43+
let refs = [
44+
authors,
45+
prRefs,
46+
hashRefs,
47+
].filter(i => i?.trim()).join(' ')
4148

4249
if (refs)
4350
refs = `&nbsp;-&nbsp; ${refs}`
@@ -127,9 +134,9 @@ export async function generateMarkdown(commits: Commit[], options: ResolvedChang
127134
lines.push('\n**No significant changes**')
128135
}
129136
else {
130-
const url = `https://${options.baseUrl}/${options.repo}/compare/${options.from}...${options.to}`
137+
const url = `https://${options.baseUrl}/${options.repo}/compare/${options.from}...${options.name}`
131138
// 添加版本
132-
lines.push(`\n**Release New Version v${options.to} [👉 View changes on GitHub](${url})**`)
139+
lines.push(`\n**Release New Version ${options.name} [👉 View Changes On GitHub](${url})**`)
133140
}
134141

135142
return convert(lines.join('\n').trim(), true)
@@ -139,9 +146,9 @@ export async function generateMarkdown(commits: Commit[], options: ResolvedChang
139146
* 更新changelog
140147
* @param outputPath
141148
* @param markdown
142-
* @param to
149+
* @param releaseVersionName
143150
*/
144-
export async function updateChangelog(outputPath: string, markdown: string, to: string) {
151+
export async function updateChangelog(outputPath: string, markdown: string, releaseVersionName: string) {
145152
let changelogMD: string
146153
if (existsSync(outputPath)) {
147154
console.info(`Updating ${outputPath}`)
@@ -153,7 +160,7 @@ export async function updateChangelog(outputPath: string, markdown: string, to:
153160
}
154161

155162
// 添加版本头部
156-
const newMd = `## ${to} (${dayjs().format('YYYY-MM-DD')})\n\n${markdown}`
163+
const newMd = `## ${releaseVersionName} (${dayjs().format('YYYY-MM-DD')})\n\n${markdown}`
157164

158165
const lastEntry = changelogMD.match(/^##\s+(?:\S.*)?$/m)
159166

0 commit comments

Comments
 (0)