Skip to content

Commit c2793ad

Browse files
committed
feat(@142vip/fairy-cli): 丰富TS类型,增加branch参数,默认从next分支获取commit信息,增加release交互全局错误捕获
1 parent 86edcf4 commit c2793ad

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

packages/fairy-cli/src/commands/release.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ function execVipRelease(args: { checkRelease?: boolean }) {
137137
// 发布根模块
138138
await releaseRoot()
139139
})
140+
.catch(() => {
141+
// 避免错误过分暴露
142+
})
140143
}
141144

142145
/**

packages/fairy-cli/src/shared/git.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { execSync } from 'node:child_process'
2+
import process from 'node:process'
23

34
/**
45
* 获取当前分支名
@@ -13,7 +14,7 @@ export function getBranchName(): string {
1314
* - 优先从package.json中获取version
1415
* - version对应的tag不存在时,再从git记录中获取最新tag
1516
*/
16-
export function getLatestTagName(): string | null {
17+
export function getLatestTagName(): string {
1718
try {
1819
// 读取 package.json 文件中的 version 值
1920
const packageJSON = execSync('cat package.json').toString()
@@ -30,7 +31,18 @@ export function getLatestTagName(): string | null {
3031
return execSync('git describe --tags --abbrev=0').toString().trim()
3132
}
3233
catch (error) {
33-
console.error('Failed to get the latest tag name:', error)
34-
return null
34+
console.log(error)
35+
process.exit(0)
3536
}
3637
}
38+
39+
/**
40+
* 获取某个分支上的commit日志
41+
*/
42+
export function getCommitLogs(latestTag: string, branch?: string): string[] {
43+
const command = `git log ${branch ?? ''} --pretty=format:"%s" --date=short "${latestTag}"..HEAD`
44+
const commitLogs = execSync(command).toString().trim()
45+
46+
// 整理出git提交日志
47+
return commitLogs.split('\n')
48+
}

packages/fairy-cli/src/shared/release-package.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execSync } from 'node:child_process'
22
import process from 'node:process'
33
import { versionBump } from '@142vip/release-version'
4-
import { getLatestTagName } from './git'
4+
import { getCommitLogs, getLatestTagName } from './git'
55

66
interface PackageJSON {
77
name: string
@@ -10,6 +10,15 @@ interface PackageJSON {
1010
path: string
1111
}
1212

13+
interface ValidatePkgJSON {
14+
name: string
15+
release: boolean
16+
}
17+
interface ValidateResponse {
18+
release: boolean
19+
packages: ValidatePkgJSON[]
20+
}
21+
1322
/**
1423
* 获取发布的包名
1524
* 参考:
@@ -33,17 +42,15 @@ export function getReleasePkgJSON(filter?: string) {
3342
* 提交git当前节点到上个tag的所有提交记录
3443
* 分析、判断是否有公共模块,提醒及时对公共模块发布新的版本号
3544
*/
36-
function validatePackage(packageName: string, template?: string) {
45+
function validatePackage(packageNameInCommitScope: string, template?: string) {
3746
const latestTag = getLatestTagName()
38-
const command = `git log --pretty=format:"%s" --date=short "${latestTag}"..HEAD`
39-
const commitLogs = execSync(command).toString().trim()
47+
const commitLogs = getCommitLogs(latestTag)
4048

4149
// 整理出git提交日志
42-
const logsByPackage = commitLogs.split('\n')
43-
.filter(commit => commit.includes(`${packageName}`))
50+
const logsByPackage = commitLogs.filter(commit => commit.includes(`(${packageNameInCommitScope})`))
4451

4552
// 判断是否需要发布新的版本
46-
return logsByPackage.length > 0 && !logsByPackage[0].includes(template ?? `release(${packageName})`)
53+
return logsByPackage.length > 0 && !logsByPackage[0].includes(template ?? `release(${packageNameInCommitScope})`)
4754
}
4855

4956
/**
@@ -57,8 +64,6 @@ export async function releaseMonorepoPackage(pkg: PackageJSON) {
5764
const command = `pnpm --filter "${pkg.name}" --shell-mode exec "${rpCommand}"`
5865

5966
console.log('等价命令-->', command)
60-
console.log(pkg)
61-
console.log('aaaa dir-->', pkg.path)
6267

6368
await versionBump({
6469
preid: 'alpha',
@@ -76,11 +81,14 @@ export async function releaseMonorepoPackage(pkg: PackageJSON) {
7681
})
7782
}
7883

79-
export function validateBeforeReleaseRoot() {
84+
/**
85+
* 在发布根模块前线上进行校验
86+
*/
87+
export function validateBeforeReleaseRoot(): ValidateResponse {
8088
const pkgJSON = getReleasePkgJSON()
8189
const packageNames = pkgJSON.map(pkg => pkg.name)
8290
let isRootRelease = true
83-
const packages = []
91+
const packages: ValidatePkgJSON[] = []
8492
for (const packageName of packageNames) {
8593
const isRelease = validatePackage(packageName)
8694
if (!isRelease) {

0 commit comments

Comments
 (0)