Skip to content

Commit

Permalink
style(@142vip/changelog): 代码整理、风格优化
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl committed Oct 5, 2024
1 parent 09b8857 commit efeba87
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
4 changes: 3 additions & 1 deletion packages/changelog/src/core/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ async function resolveConfig(options: ChangelogOptions) {

/**
* 生成markdown文档信息
* @param options
*/
async function generate(options: ChangelogOptions) {
const config = await resolveConfig(options)
Expand Down Expand Up @@ -102,6 +101,9 @@ async function generate(options: ChangelogOptions) {
return { config, markdown, commits }
}

/**
* 处理changelog业务
*/
async function dealChangelog(args: ChangelogOptions) {
args.token = args.token || process.env.GITHUB_TOKEN

Expand Down
4 changes: 2 additions & 2 deletions packages/changelog/src/core/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from 'node:process'
import type { ChangelogOptions } from '../types/changelog.interface'
import type { ChangelogOptions } from '../types'

/**
* 默认配置
Expand Down Expand Up @@ -29,7 +29,7 @@ export const ChangelogDefaultConfig: ChangelogOptions = {
}

/**
* 定义@142vip/changelog模块的默认配置文件
* 定义配置文件
*/
export function defineChangelogDefaultConfig(config: ChangelogOptions) {
return config
Expand Down
3 changes: 3 additions & 0 deletions packages/changelog/src/utils/git.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import semver from 'semver'

/**
* 获取github仓库
*/
export async function getGitHubRepo(baseUrl: string) {
const url = await execCommand('git', ['config', '--get', 'remote.origin.url'])
const escapedBaseUrl = baseUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
Expand Down
10 changes: 5 additions & 5 deletions packages/changelog/src/utils/github.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $fetch } from 'ofetch'
import { cyan, green, red, yellow } from 'kolorist'
import qs from 'qs'
import { vipColor } from '@142vip/utils'
import type {
AuthorInfo,
ChangelogOptions,
Expand Down Expand Up @@ -36,7 +36,7 @@ export async function sendRelease(
prerelease: options.prerelease,
tag_name: options.to,
}
console.log(cyan(method === 'POST'
console.log(vipColor.cyan(method === 'POST'
? 'Creating release notes...'
: 'Updating release notes...'),
)
Expand All @@ -45,7 +45,7 @@ export async function sendRelease(
body: JSON.stringify(body),
headers,
})
console.log(green(`Released on ${res.html_url}`))
console.log(vipColor.green(`Released on ${res.html_url}`))
}

function getHeaders(options: ChangelogOptions) {
Expand Down Expand Up @@ -172,9 +172,9 @@ export function generateWebUrl(config: any, markdown: string) {
*/
export function printUrl(webUrl: string, success: boolean = true) {
if (success) {
console.error(`\n${yellow('使用以下链接手动发布新的版本:')}\n${yellow(webUrl)}\n`)
console.error(`\n${vipColor.yellow('使用以下链接手动发布新的版本:')}\n${vipColor.yellow(webUrl)}\n`)
}
else {
console.error(`\n${red('无法创建发布。使用以下链接手动创建:')}\n${yellow(webUrl)}\n`)
console.error(`\n${vipColor.red('无法创建发布。使用以下链接手动创建:')}\n${vipColor.yellow(webUrl)}\n`)
}
}
15 changes: 9 additions & 6 deletions packages/changelog/src/utils/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ function formatLine(commit: Commit, options: ResolvedChangelogOptions) {
return [description, refs].filter(i => i?.trim()).join(' ')
}

// 标题
/**
* 格式化标题
*/
function formatTitle(name: string, options: ResolvedChangelogOptions) {
// 加表情包
if (!options.emoji)
Expand All @@ -58,6 +60,9 @@ function formatTitle(name: string, options: ResolvedChangelogOptions) {
return `### ${name.trim()}`
}

/**
* 格式化Section
*/
function formatSection(commits: Commit[], sectionName: string, options: ResolvedChangelogOptions) {
if (!commits.length)
return []
Expand Down Expand Up @@ -106,10 +111,12 @@ function formatSection(commits: Commit[], sectionName: string, options: Resolved
.map(commit => `${padding}- ${prefix}${formatLine(commit, options)}`))
})
}

return lines
}

/**
* 生成Markdown文档
*/
export async function generateMarkdown(commits: Commit[], options: ResolvedChangelogOptions) {
const lines: string[] = []

Expand Down Expand Up @@ -154,7 +161,6 @@ export async function generateMarkdown(commits: Commit[], options: ResolvedChang
/**
* 年月日格式化当前时间
* - 格式: 2024-08-09
* @param date
*/
function formatDateToYMD(date: Date = new Date()): string {
const year = date.getFullYear()
Expand All @@ -166,9 +172,6 @@ function formatDateToYMD(date: Date = new Date()): string {

/**
* 更新changelog
* @param outputPath
* @param markdown
* @param releaseVersionName
*/
export async function updateChangelog(outputPath: string, markdown: string, releaseVersionName: string) {
let changelogMD: string
Expand Down

0 comments on commit efeba87

Please sign in to comment.