Skip to content

Commit 2d253e0

Browse files
committed
feat(@142vip/fairy-cli): 拓展installcleanchangelog等命令
1 parent 13d97a3 commit 2d253e0

File tree

4 files changed

+66
-53
lines changed

4 files changed

+66
-53
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ async function generateChangelog(args: ChangelogOptions): Promise<void> {
1010
await VipExecutor.commandStandardExecutor(`npx changelog ${args.dry ? '--dry' : ''}`)
1111
}
1212

13+
/**
14+
* changelog命令
15+
* - 生成CHANGELOG文档
16+
*/
1317
export async function changelogMain(program: VipCommander): Promise<void> {
1418
program
1519
.command(CliCommandEnum.CHANGELOG)

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

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { VipCommander } from '@142vip/utils'
2-
import { VipInquirer, VipNodeJS } from '@142vip/utils'
2+
import { VipColor, VipConsole, VipInquirer, vipLogger, VipNodeJS } from '@142vip/utils'
33
import { deleteAsync } from 'del'
44
import { CliCommandEnum } from '../shared'
55

@@ -10,6 +10,7 @@ interface DelOptions {
1010
dryRun?: boolean
1111
force?: boolean
1212
all?: boolean
13+
logger?: boolean
1314
}
1415

1516
interface CleanUpOptions extends DelOptions {
@@ -24,6 +25,28 @@ interface CleanUpOptions extends DelOptions {
2425
gitHooks?: boolean
2526
}
2627

28+
function generateDirPatterns(dirName: string | string[], delAll?: boolean): string[] {
29+
let delDirs: string[] = []
30+
31+
if (typeof dirName === 'string') {
32+
delDirs.push(dirName)
33+
}
34+
else {
35+
delDirs.push(...dirName)
36+
}
37+
38+
if (delAll) {
39+
// 删除程序上下文中所有的该目录,注意路径取反规则
40+
delDirs = delDirs.map(dir => dir.startsWith('!') ? `!**/${dir.substring(1)}` : `**/${dir}`)
41+
}
42+
else {
43+
// 只删除该目录
44+
delDirs = delDirs.map(dir => `${dir}`)
45+
}
46+
47+
return delDirs
48+
}
49+
2750
/**
2851
* 删除文件或文件夹
2952
* - 恢复项目初始状态
@@ -72,52 +95,34 @@ async function execCleanUp(args: CleanUpOptions): Promise<void> {
7295
}
7396

7497
if (dirPatterns.length === 0) {
75-
console.log('删除规则为空,不做删除操作处理,请传入有效参数!!')
98+
vipLogger.log(VipColor.red('删除规则为空,不做删除操作处理,请传入有效参数!!'))
7699
return VipNodeJS.exitProcess(1)
77100
}
78101

79102
// 删除前,对话框确认
80103
if (!args.ignoreTips) {
81104
const deleted = await VipInquirer.promptConfirm('是否需要删除?', true)
82105

106+
// 不删除,非0退出
83107
if (!deleted) {
84-
// 不删除,非0退出
85108
return VipNodeJS.exitProcess(1)
86109
}
87110
}
88111

89-
// 需要删除的目录
90-
console.log('删除规则:', dirPatterns)
91-
92112
// 删除
93113
const deletedDirs = await deleteAsync(dirPatterns, {
94114
dryRun: args.dryRun,
95115
force: args.force,
96116
dot: true,
97117
})
98-
console.log(deletedDirs)
99-
}
100-
101-
function generateDirPatterns(dirName: string | string[], delAll?: boolean): string[] {
102-
let delDirs: string[] = []
103118

104-
if (typeof dirName === 'string') {
105-
delDirs.push(dirName)
119+
// 日志追踪
120+
if (args.logger) {
121+
// 需要删除的目录
122+
VipConsole.trace('删除规则:', dirPatterns)
123+
vipLogger.println()
124+
VipConsole.trace('删除的文件和目录:', deletedDirs)
106125
}
107-
else {
108-
delDirs.push(...dirName)
109-
}
110-
111-
if (delAll) {
112-
// 删除程序上下文中所有的该目录,注意路径取反规则
113-
delDirs = delDirs.map(dir => dir.startsWith('!') ? `!**/${dir.substring(1)}` : `**/${dir}`)
114-
}
115-
else {
116-
// 只删除该目录
117-
delDirs = delDirs.map(dir => `${dir}`)
118-
}
119-
120-
return delDirs
121126
}
122127

123128
/**
@@ -126,18 +131,19 @@ function generateDirPatterns(dirName: string | string[], delAll?: boolean): stri
126131
export async function cleanUpMain(program: VipCommander): Promise<void> {
127132
program
128133
.command(CliCommandEnum.CLEAN)
129-
.description('清除开发、构建等环境下的无用目录')
134+
.description('清除开发、构建环境下产生的无用文件')
130135
.option('-n,--nuxt', '删除nuxt构建目录,包括.nuxt、.output目录', false)
131136
.option('-d,--dist', '删除dist目录', false)
132137
.option('-m,--midway', '删除midway构建目录', false)
133-
.option('--turbo', '删除turbo缓存目录', false)
138+
.option('-t,--turbo', '删除turbo缓存目录', false)
134139
.option('--vite', '删除vite缓存目录', false)
135140
.option('--deps', '删除node_modules目录', false)
136-
.option('--coverage', '删除coverage目录', false)
141+
.option('-c,--coverage', '删除coverage目录', false)
137142
.option('--git-hooks', '删除.git/hooks目录', false)
138143
.option('-f,--force', '强制删除,默认值:false', false)
139-
.option('--all', '深度删除所有', false)
144+
.option('-a,--all', '深度删除所有', false)
140145
.option('--ignore-tips', '忽略提示,直接删除', false)
146+
.option('--logger', '开启日志追踪模式', false)
141147
.option('--dry-run', '试运行,不做实际删除操作', false)
142148
.action(async (args: CleanUpOptions) => {
143149
await execCleanUp(args)
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
11
import type { VipCommander } from '@142vip/utils'
2-
import { VipExecutor } from '@142vip/utils'
2+
import { RegistryAddressEnum, VipInquirer, VipNpm } from '@142vip/utils'
33
import { CliCommandEnum } from '../shared'
44

55
interface InstallOptions {
6-
pnpm: boolean
7-
npm: boolean
86
registry?: string
9-
update: boolean
7+
force?: boolean
8+
}
9+
/**
10+
* 支持的依赖管理器
11+
*/
12+
enum InstallTypeEnum {
13+
NPM = 'npm',
14+
PNPM = 'pnpm',
1015
}
1116

1217
/**
1318
* 依赖下载
1419
* - npm
1520
* - pnpm
1621
*/
17-
async function execInstall(args: InstallOptions): Promise<void> {
18-
// pnpm i --frozen-lockfile --registry https://registry.npmmirror.com
19-
// npm ci
20-
if (args.npm) {
21-
// 使用npm下载
22-
await VipExecutor.commandStandardExecutor(`npm ${args.update ? 'i' : 'ci'} --registry ${args.registry}`)
22+
async function execInstall(installType: InstallTypeEnum, args: InstallOptions): Promise<void> {
23+
if (installType === InstallTypeEnum.NPM) {
24+
await VipNpm.installByNpm({ force: args.force, registry: args.registry })
2325
}
24-
else {
25-
// pnpm下载
26-
await VipExecutor.commandStandardExecutor(`pnpm i ${args.update ? '' : '--frozen-lockfile'} --registry ${args.registry}`)
26+
if (installType === InstallTypeEnum.PNPM) {
27+
await VipNpm.installByPnpm({ force: args.force, registry: args.registry })
2728
}
2829
}
2930

3031
/**
3132
* install 命令入口
32-
* @param program
3333
*/
3434
export async function installMain(program: VipCommander): Promise<void> {
3535
program
3636
.command(CliCommandEnum.INSTALL)
3737
.aliases(['i', 'add'])
38-
.description('pnpm ci dependencies')
39-
.option('--pnpm', 'use pnpm ', true)
40-
.option('--npm', 'use npm', false)
41-
.option('--update', 'update lockfile,not use --frozen-lockfile', false)
42-
.option('--registry', 'pnpm registry address,default ali cdn', 'https://registry.npmmirror.com')
43-
.action((args: InstallOptions) => {
44-
execInstall(args)
38+
.description('下载、升级依赖版本')
39+
.option('-f,--force', 'force the lock file to be updated', false)
40+
.option('--registry', `pnpm registry address,default ali npm:${RegistryAddressEnum.VIP_NPM_ALIBABA}`, RegistryAddressEnum.VIP_NPM_ALIBABA)
41+
.action(async (args: InstallOptions) => {
42+
const installType = await VipInquirer.promptSelect<InstallTypeEnum>('选择下载方式', [
43+
InstallTypeEnum.PNPM,
44+
InstallTypeEnum.NPM,
45+
], {
46+
default: InstallTypeEnum.PNPM,
47+
})
48+
await execInstall(installType, args)
4549
})
4650
}

packages/fairy-cli/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export * from './fairy-cli'
21
export * from './shared'

0 commit comments

Comments
 (0)