Skip to content

Commit

Permalink
feat(changelog): 支持多模块生成日志文档
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl committed Jul 14, 2024
1 parent 8128340 commit f4e420e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

坚持开源,x代表一切都有可能

cd

## 证书

[MIT](https://opensource.org/license/MIT)
Expand Down
2 changes: 2 additions & 0 deletions packages/changelog/src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cli
.option('--name <name>', 'Name of the release')
.option('--prerelease', 'Mark release as prerelease')
.option('--output <path>', 'Output to file instead of sending to GitHub')
.option('--scopeName <scopeName>', 'Output to file instead of sending to GitHub')
.option('--dry', 'Dry run')
.help()

Expand All @@ -35,6 +36,7 @@ cli
console.log(dim(`${bold('@142vip/changelog')} `) + dim(`v${version}`))

const { config, markdown, commits } = await generate(args)
console.log(111, args, config)
webUrl = `https://${config.baseUrl}/${config.repo}/releases/new?title=${encodeURIComponent(String(config.name || config.to))}&body=${encodeURIComponent(String(markdown))}&tag=${encodeURIComponent(String(config.to))}&prerelease=${config.prerelease}`

console.log(cyan(config.from) + dim(' -> ') + blue(config.to) + dim(` (${commits.length} commits)`))
Expand Down
3 changes: 3 additions & 0 deletions packages/changelog/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export async function resolveConfig(options: ChangelogOptions) {
config.repo = config.repo || config.github || await getGitHubRepo(config.baseUrl)
config.prerelease = config.prerelease ?? isPrerelease(config.to)

// todo 支持多个scope生成
config.scopeName = options.scopeName

if (typeof config.repo !== 'string')
throw new Error(`Invalid GitHub repository, expected a string but got ${JSON.stringify(config.repo)}`)

Expand Down
25 changes: 18 additions & 7 deletions packages/changelog/src/core/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function formatLine(commit: Commit, options: ResolvedChangelogOptions) {

// 标题
function formatTitle(name: string, options: ResolvedChangelogOptions) {
// 加表情包
if (!options.emoji)
name = name.replace(emojisRE, '')

Expand All @@ -73,17 +74,27 @@ function formatSection(commits: Commit[], sectionName: string, options: Resolved
useScopeGroup = false

Object.keys(scopes).sort().forEach((scope) => {
console.log(options.scopeName, scope)
let padding = ''
let prefix = ''
const scopeText = `**${options.scopeMap[scope] || scope}**`
if (scope && (useScopeGroup === true || (useScopeGroup === 'multiple' && scopes[scope].length > 1))) {
lines.push(`- ${scopeText}:`)
padding = ' '

// 生成monorepo中的md
if (options.scopeName != null && options.scopeName === scope) {
// package dir in monorepo

}
else if (scope) {
prefix = `${scopeText}: `
else {
// root dir

const scopeText = `**${options.scopeMap[scope] || scope}**`
if (scope && (useScopeGroup === true || (useScopeGroup === 'multiple' && scopes[scope].length > 1))) {
lines.push(`- ${scopeText}:`)
padding = ' '
}
else if (scope) {
prefix = `${scopeText}: `
}
}

lines.push(...scopes[scope]
.reverse()
.map(commit => `${padding}- ${prefix}${formatLine(commit, options)}`),
Expand Down
2 changes: 2 additions & 0 deletions packages/changelog/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export interface ChangelogOptions extends Partial<ChangelogEnOptions> {
* @default api.github.com
*/
baseUrlApi?: string

scopeName?: string
}

export type ResolvedChangelogOptions = Required<ChangelogOptions>
Expand Down

0 comments on commit f4e420e

Please sign in to comment.