Skip to content

Commit c4dc59a

Browse files
authored
fix(@142vip/changelog): 修复发布大版本前,monorepo子模块commit记录重复提交 (#358)
* fix(@142vip/changelog): 修复发布大版本前,`monorepo`子模块`commit`记录重复提交 * chore: update
1 parent 627c576 commit c4dc59a

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

packages/changelog/src/changelog-cli.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function changelogHandler(cliOptions: ChangelogCliOptions): Promise<void>
1919

2020
const changelogConfig = await mergeConfig(cliOptions)
2121

22-
console.log(111, changelogConfig)
22+
console.log('changelogConfig:', changelogConfig)
2323
const { markdown, commits, releaseUrl } = await changelogGenerate(changelogConfig)
2424

2525
VipConsole.log(`${VipColor.cyan(changelogConfig.from)} ${VipColor.dim(' -> ')} ${VipColor.blue(changelogConfig.to)} ${VipColor.dim(` (${commits.length} commits)`)}`)
@@ -30,8 +30,14 @@ async function changelogHandler(cliOptions: ChangelogCliOptions): Promise<void>
3030

3131
// 试运行
3232
if (changelogConfig.dryRun) {
33-
VipConsole.log(VipColor.yellow('试运行。已跳过版本发布。'))
34-
GithubAPI.printReleaseUrl(releaseUrl)
33+
if (changelogConfig.scopeName != null) {
34+
// 子模块,不触发github release发布地址
35+
VipConsole.log(VipColor.yellow('Monorepo模式的NPM包发布。不触发github release发布地址\n'))
36+
}
37+
else {
38+
VipConsole.log(VipColor.yellow('试运行。已跳过版本发布'))
39+
GithubAPI.printReleaseUrl(releaseUrl)
40+
}
3541
return
3642
}
3743

packages/changelog/src/changelog.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ export async function changelogGenerate(config: ChangelogGenerateOptions): Promi
2929
})
3030

3131
// 解析commit信息 todo 这里的参数类型需要明确
32-
let commits = parseCommits(rawCommits, config.scopeMap)
33-
34-
// 在monorepo模式下,去掉主目录下的更新
35-
// 发布子模块时,需要考虑根模块迭代一个版本,子模块迭代多个版本但只需要记录一个版本,去掉release信息
36-
if (config.scopeName != null) {
37-
commits = commits.filter(commit => !commit.message.includes(`release(${config.scopeName})`))
38-
}
32+
const commits = parseCommits(rawCommits, config.scopeMap)
3933

4034
// 添加贡献者
4135
if (config.contributors) {
@@ -191,13 +185,39 @@ export async function generateMarkdown(commits: Commit[], options: {
191185
)
192186
}
193187

194-
const changes = commits.filter(c => !c.isBreaking)
188+
let changes = commits.filter(c => !c.isBreaking)
189+
190+
if (options.scopeName != null) {
191+
// 遇到第一个release就跳出,避免重复记录版本
192+
const commitsInScopeName: Commit[] = []
193+
194+
for (const commit of commits) {
195+
if (commit.type === 'release') {
196+
break
197+
}
198+
commitsInScopeName.push(commit)
199+
}
200+
changes = commitsInScopeName
201+
}
202+
195203
// 普通提交
196204
const group = VipLodash.groupBy(changes, 'type')
197-
for (const type of Object.keys(options.types)) {
198-
const items = group[type] || []
205+
206+
let commitTypes = Object.keys(options.types)
207+
208+
// monorepo的子模块,不记录release信息
209+
if (options.scopeName != null) {
210+
commitTypes = commitTypes.filter(type => type !== 'release')
211+
}
212+
for (const type of commitTypes) {
213+
// 子模块时,不记录发布信息
214+
if (options.scopeName != null && type === 'release') {
215+
break
216+
}
217+
218+
const commitsByType = group[type] || []
199219
lines.push(
200-
...MarkdownAPI.formatSection(items, {
220+
...MarkdownAPI.formatSection(commitsByType, {
201221
emoji: options.emoji,
202222
group: options.group,
203223
scopeName: options.scopeName,

packages/changelog/src/utils/markdown.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ function formatSection(commits: Commit[], options: {
9292
if (!commits.length)
9393
return []
9494

95+
// monorepo模式下,不显示Release记录
96+
if (options.scopeName != null) {
97+
// 过滤出只包含子模块的提交记录
98+
commits = commits.filter(commit => commit.scope === options.scopeName)
99+
}
100+
95101
// 注意空行
96102
const lines: string[] = ['', formatTitle(options.sectionName, options.emoji), '']
97103

@@ -105,12 +111,14 @@ function formatSection(commits: Commit[], options: {
105111
if (scopes[options.scopeName] == null) {
106112
return []
107113
}
108-
// lines里每条记录就是一次commit提交
109-
lines.push(
110-
...scopes[options.scopeName]
111-
.reverse()
112-
.map(commit => `- ${formatLine(commit, VipLodash.pick(options, 'baseUrl', 'repo', 'capitalize'))}`),
113-
)
114+
// lines里每条记录就是一次commit提交,第一次遇到release(xxx)跳出,避免记录别的版本
115+
const commits = scopes[options.scopeName].reverse()
116+
for (const commit of commits) {
117+
if (commit.type === 'release') {
118+
break
119+
}
120+
lines.push(`- ${formatLine(commit, VipLodash.pick(options, 'baseUrl', 'repo', 'capitalize'))}`)
121+
}
114122
}
115123
// root dir 普通模式
116124
else {

0 commit comments

Comments
 (0)