Skip to content

Commit 9d00738

Browse files
authored
feat(@142vip/fairy-cli): 优化子命令核心逻辑,补充枚举、文档 (#546)
1 parent 63a68c5 commit 9d00738

File tree

12 files changed

+70
-37
lines changed

12 files changed

+70
-37
lines changed

packages/fairy-cli/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,27 @@ npx fa -v
2222
```text
2323
Usage: @142vip/fairy-cli [options] [command]
2424
25+
通用型Cli助手、一站式工具,支持多种命令操作
26+
2527
Options:
26-
-v --version output the version number
27-
-h, --help display help for command
28+
-v,--version VipCommander Version By @142vip
29+
-h, --help display help for command
2830
2931
Commands:
30-
login [options] <platform> 登录远程平台,支持Docker和Npm
31-
install|i [options] pnpm ci dependencies
32-
release [options] release npm version
33-
changelog [options] 快速使用@142vip/changelog模块
34-
publish [options] publish to remote platform,eg. Docker Image & Npm Package
35-
sync [packageNames...] 同步npm仓库的模块包到cnpm仓库
36-
deploy [options] 项目部署
37-
lint [options] 根据Eslint检查代码风格,支持代码格式化
38-
turbo [options] [repoName...] TurboPack工具命令
39-
clean [options] 清除开发、构建等环境下的无用目录
40-
help [command] display help for command
32+
login|l [options] 登录平台
33+
install|i [options] 安装依赖
34+
release|re [options] 发布新的版本
35+
changelog|c [options] 生成CHANGELOG文档
36+
publish|p [options] 远程镜像推送
37+
sync|s [options] [packageName] 同步NPM包
38+
deploy|de [options] 项目部署
39+
lint|li [options] 根据Eslint检查、格式化代码风格
40+
turbo|t [options] [filters...] 通用型TurboPack管理工具
41+
clean|cl [options] 快速清理项目
42+
copyright|cr [options] 软件著作权登记的源代码文档生成
43+
commit|co [options] [vip] Git Commit 提交信息
44+
help [command] display help for command
45+
4146
```
4247

4348
### copyright命令
@@ -190,8 +195,12 @@ npx fa clean --deps --ignore-tips --all
190195

191196
## 相关
192197

193-
- [rimraf](https://www.npmjs.com/package/rimraf)
194198
- [del](https://www.npmjs.com/package/del)
199+
- [turbo](https://www.npmjs.com/package/turbo)
200+
- [@142vip/changelog](https://www.npmjs.com/package/@142vip/changelog)
201+
- [@142vip/commit-linter](https://www.npmjs.com/package/@142vip/commit-linter)
202+
- [@142vip/release-version](https://www.npmjs.com/package/@142vip/release-version)
203+
- [@142vip/utils](https://www.npmjs.com/package/@142vip/utils)
195204

196205
## 证书
197206

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ interface CleanUpOptions extends DelOptions {
3131
gitHooks?: boolean
3232
}
3333

34+
/**
35+
* 生成删除目录匹配规则
36+
* @param dirName 目录名
37+
* @param delAll 是否深度删除,子目录中的文件也会被删除
38+
*/
3439
function generateDirPatterns(dirName: string | string[], delAll?: boolean): string[] {
3540
let delDirs: string[] = []
3641

@@ -102,7 +107,7 @@ async function execCleanUp(args: CleanUpOptions): Promise<void> {
102107

103108
if (dirPatterns.length === 0) {
104109
vipLogger.log(VipColor.red('删除规则为空,不做删除操作处理,请传入有效参数!!'))
105-
return VipNodeJS.exitProcess(1)
110+
return VipNodeJS.existErrorProcess()
106111
}
107112

108113
// 删除前,对话框确认
@@ -111,7 +116,7 @@ async function execCleanUp(args: CleanUpOptions): Promise<void> {
111116

112117
// 不删除,非0退出
113118
if (!deleted) {
114-
return VipNodeJS.exitProcess(1)
119+
return VipNodeJS.existErrorProcess()
115120
}
116121
}
117122

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import { CLI_COMMAND_DETAIL, CommandEnum } from '../enums'
1818

1919
const GIT_NULL_SCOPE = '没有范围,那就选这个!!!'
2020

21+
/**
22+
* commit子命令配置
23+
*/
2124
interface CommitOptions {
2225
/**
2326
* 试运行
@@ -71,7 +74,7 @@ async function execVipCodeCommit(args: CommitOptions): Promise<void> {
7174
const isYes = await VipInquirer.promptConfirm(`Git Commit信息:${VipColor.red(commitMsg)},是否继续提交${VipColor.bold('所有变更')}?`, true)
7275
if (!isYes) {
7376
vipLogger.logByBlank(`${VipColor.redBright('用户取消提交,欢迎下次使用')}`)
74-
VipNodeJS.exitProcess(1)
77+
VipNodeJS.existErrorProcess()
7578
}
7679

7780
// 校验

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ export async function copyrightMain(program: VipCommander): Promise<void> {
3535
VipConsole.log(VipColor.dim(`源代码扫描目录:${sourceCodeDir}`))
3636
VipConsole.log(VipColor.dim(`源代码编程语言类型:${fileType}`))
3737
vipLogger.println()
38-
// VipConsole.log(VipColor.red('著作权申请的一些小建议:'))
39-
VipNodeJS.exitProcess(1)
38+
VipNodeJS.existErrorProcess()
4039
}
4140

4241
const vipCopyright = new VipCopyright(copyrightTitle, copyrightVersion, {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function DeployGithubPage() {}
1717

1818
/**
1919
* deploy命令
20+
* - 支持部署github pages
2021
*/
2122
export async function deployMain(program: VipCommander): Promise<void> {
2223
program

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import type { VipCommander } from '@142vip/utils'
2-
import { VipColor, VipConsole, VipDocker, VipInquirer, vipLogger } from '@142vip/utils'
2+
import { VipColor, VipDocker, VipInquirer, vipLogger } from '@142vip/utils'
33
import { CLI_COMMAND_DETAIL, CommandEnum } from '../enums'
44

5+
/**
6+
* 支持的登录平台枚举
7+
*/
58
enum LoginPlatformEnum {
69
DOCKER = 'DOCKER',
710
NPM = 'NPM',
811
}
912

13+
/**
14+
* 支持的注册地址枚举
15+
*/
1016
enum RegistryURLEnum {
1117
DOCKER = 'https://registry.docker.io',
1218
NPM = 'https://registry.npmjs.org',
@@ -37,9 +43,7 @@ async function loginDocker(): Promise<void> {
3743
async function loginNpm(): Promise<void> {
3844
const registry = await VipInquirer.promptInput(`请输入NPM地址:`, RegistryURLEnum.NPM)
3945
const command = `npm login --registry ${registry}`
40-
vipLogger.println()
41-
VipConsole.log(`${VipColor.red('请粘贴到终端执行,NPM登录命令:')} ${VipColor.green(command)}`)
42-
vipLogger.println()
46+
vipLogger.logByBlank(`${VipColor.red('请粘贴到终端执行,NPM登录命令:')} ${VipColor.green(command)}`)
4347
}
4448

4549
/**

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { VipCommander } from '@142vip/utils'
22
import {
33
RegistryAddressEnum,
44
VipColor,
5-
VipConsole,
65
VipExecutor,
76
VipInquirer,
87
vipLogger,
@@ -26,9 +25,7 @@ async function publishNpm(args: PublishOptions): Promise<void> {
2625
const command = `npm publish --access public --registry=${args.registry ?? RegistryAddressEnum.NPM}`
2726

2827
if (args.dryRun) {
29-
vipLogger.println()
30-
VipConsole.log(`试运行,不执行发布命令:${VipColor.green(command)}`)
31-
vipLogger.println()
28+
vipLogger.logByBlank(`试运行,不执行发布命令:${VipColor.green(command)}`)
3229
}
3330
else {
3431
await VipExecutor.commandStandardExecutor(command)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ async function execNormalRelease(args: ReleaseOptions): Promise<void> {
4747
// 指定包
4848
if (args.package != null) {
4949
const packageJSONList = VipMonorepo.getPackageJSONPathList()
50-
// const packageJSONList: string[] = []
5150
if (!packageJSONList.includes(`${args.package}/package.json`)) {
5251
// 抛错,提醒用户包在monorepo下找不到
5352
VipConsole.log(VipColor.red('需要发布的包的package.json文件缺失!!'))
@@ -171,7 +170,7 @@ async function printPkgCommitLogs(pnpmFilter?: string | string[]): Promise<void>
171170
vipLogger.logByBlank(`${VipPackageJSON.getPkgRedLabel(pkgName)} ${VipColor.red('模块没有任何版本迭代信息!!')}`)
172171
}
173172
else {
174-
VipConsole.log(`${VipPackageJSON.getPkgRedLabel(pkgName)} ${VipColor.green('模块的版本迭代信息:')}`)
173+
vipLogger.logByBlank(`${VipPackageJSON.getPkgRedLabel(pkgName)} ${VipColor.green('模块的版本迭代信息:')}`)
175174
vipLogger.logByBlank(VipColor.green(commits.map(c => ` - ${c}`).join('\n')))
176175
}
177176
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,23 @@ async function requestSync(packageName: string): Promise<void> {
5353
//
5454
// }
5555

56-
const { ok, id: logId } = await response.json() as RequestSync
56+
const responseJSON = await response.json() as RequestSync
5757

58-
if (!ok) {
59-
VipConsole.log(`requestSync--json : ${await response.json()}`)
60-
VipNodeJS.exitProcess(1)
58+
if (!responseJSON.ok) {
59+
VipConsole.log(`requestSync--json : ${responseJSON}`)
60+
VipNodeJS.existErrorProcess()
6161
}
6262
setTimeout(async () => {
63-
const logUrl = await getPackageSyncLogUrl(packageName, logId)
63+
const logUrl = await getPackageSyncLogUrl(packageName, responseJSON.id)
6464
if (logUrl != null) {
6565
await getPackageSyncLog(logUrl)
6666
}
6767
}, 2000)
6868
}
6969

70+
/**
71+
* 同步的状态
72+
*/
7073
interface SyncState {
7174
ok: boolean
7275
id: string
@@ -88,7 +91,7 @@ async function getPackageSyncLogUrl(packageName: string, logId: string): Promise
8891
return stateRes.logUrl
8992
}
9093
vipLogger.error(`getPackageSyncState-->err:${stateRes}`)
91-
VipNodeJS.exitProcess(1)
94+
VipNodeJS.existErrorProcess()
9295
return null
9396
}
9497

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ enum TurboPackCommandModeEnum {
2525

2626
/**
2727
* 执行turbo命令
28+
* @param repoNames 子模块名
29+
* @param args turbo 命令参数
2830
*/
2931
async function execTurboPack(repoNames: string[], args: TurboPackOptions): Promise<void> {
3032
// 判断当前目录下是否有turbo.json配置文件
3133
if (!VipNodeJS.isExistFile('turbo.json')) {
3234
VipConsole.log(VipColor.red('项目根目录下缺少turbo.json配置文件,查看Turborepo官网:<https://turbo.build/repo/docs>'))
33-
VipNodeJS.exitProcess(1)
35+
VipNodeJS.existErrorProcess()
3436
}
3537

3638
const command = `${args.mode} ${getFilterRepo(repoNames)} --color --only`
3739
if (args.logger) {
3840
VipConsole.log(VipColor.yellow(`执行命令:${command}`))
3941
}
42+
43+
// 命令执行
4044
await VipExecutor.commandStandardExecutor(command)
4145
}
4246

@@ -64,7 +68,7 @@ export async function turboPackMain(program: VipCommander): Promise<void> {
6468
const turboVersion = await VipNpm.getTurboPackVersion()
6569
if (turboVersion == null) {
6670
vipLogger.error('未检测到TurboPack环境,请全局安装turbo。命令:npm i -g turbo')
67-
VipNodeJS.exitProcess(1)
71+
VipNodeJS.existErrorProcess()
6872
}
6973
if (filters.length === 0) {
7074
const apps = await VipNpm.getTurboPackApps()

packages/fairy-cli/src/enums/command.enum.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { VipCommanderDetailRecord } from '@142vip/utils'
22

3+
/**
4+
* fairy-cli 命令枚举
5+
*/
36
export enum CommandEnum {
47
LOGIN = 'login',
58
RELEASE = 'release',
@@ -15,6 +18,9 @@ export enum CommandEnum {
1518
COMMIT = 'commit',
1619
}
1720

21+
/**
22+
* 命令详细信息
23+
*/
1824
export const CLI_COMMAND_DETAIL: VipCommanderDetailRecord<CommandEnum> = {
1925
[CommandEnum.LOGIN]: {
2026
command: 'login',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import type { VipCommanderOptions } from '@142vip/utils'
22

3+
/**
4+
* fairy-cli 命令选项
5+
*/
36
export interface FairyCommandOptions extends Omit<VipCommanderOptions, 'help'> {}

0 commit comments

Comments
 (0)