1
1
import type { VipCommander } from '@142vip/utils'
2
- import { VipInquirer , VipNodeJS } from '@142vip/utils'
2
+ import { VipColor , VipConsole , VipInquirer , vipLogger , VipNodeJS } from '@142vip/utils'
3
3
import { deleteAsync } from 'del'
4
4
import { CliCommandEnum } from '../shared'
5
5
@@ -10,6 +10,7 @@ interface DelOptions {
10
10
dryRun ?: boolean
11
11
force ?: boolean
12
12
all ?: boolean
13
+ logger ?: boolean
13
14
}
14
15
15
16
interface CleanUpOptions extends DelOptions {
@@ -24,6 +25,28 @@ interface CleanUpOptions extends DelOptions {
24
25
gitHooks ?: boolean
25
26
}
26
27
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
+
27
50
/**
28
51
* 删除文件或文件夹
29
52
* - 恢复项目初始状态
@@ -72,52 +95,34 @@ async function execCleanUp(args: CleanUpOptions): Promise<void> {
72
95
}
73
96
74
97
if ( dirPatterns . length === 0 ) {
75
- console . log ( '删除规则为空,不做删除操作处理,请传入有效参数!!' )
98
+ vipLogger . log ( VipColor . red ( '删除规则为空,不做删除操作处理,请传入有效参数!!' ) )
76
99
return VipNodeJS . exitProcess ( 1 )
77
100
}
78
101
79
102
// 删除前,对话框确认
80
103
if ( ! args . ignoreTips ) {
81
104
const deleted = await VipInquirer . promptConfirm ( '是否需要删除?' , true )
82
105
106
+ // 不删除,非0退出
83
107
if ( ! deleted ) {
84
- // 不删除,非0退出
85
108
return VipNodeJS . exitProcess ( 1 )
86
109
}
87
110
}
88
111
89
- // 需要删除的目录
90
- console . log ( '删除规则:' , dirPatterns )
91
-
92
112
// 删除
93
113
const deletedDirs = await deleteAsync ( dirPatterns , {
94
114
dryRun : args . dryRun ,
95
115
force : args . force ,
96
116
dot : true ,
97
117
} )
98
- console . log ( deletedDirs )
99
- }
100
-
101
- function generateDirPatterns ( dirName : string | string [ ] , delAll ?: boolean ) : string [ ] {
102
- let delDirs : string [ ] = [ ]
103
118
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 )
106
125
}
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
121
126
}
122
127
123
128
/**
@@ -126,18 +131,19 @@ function generateDirPatterns(dirName: string | string[], delAll?: boolean): stri
126
131
export async function cleanUpMain ( program : VipCommander ) : Promise < void > {
127
132
program
128
133
. command ( CliCommandEnum . CLEAN )
129
- . description ( '清除开发、构建等环境下的无用目录 ' )
134
+ . description ( '清除开发、构建环境下产生的无用文件 ' )
130
135
. option ( '-n,--nuxt' , '删除nuxt构建目录,包括.nuxt、.output目录' , false )
131
136
. option ( '-d,--dist' , '删除dist目录' , false )
132
137
. option ( '-m,--midway' , '删除midway构建目录' , false )
133
- . option ( '--turbo' , '删除turbo缓存目录' , false )
138
+ . option ( '-t,- -turbo' , '删除turbo缓存目录' , false )
134
139
. option ( '--vite' , '删除vite缓存目录' , false )
135
140
. option ( '--deps' , '删除node_modules目录' , false )
136
- . option ( '--coverage' , '删除coverage目录' , false )
141
+ . option ( '-c,- -coverage' , '删除coverage目录' , false )
137
142
. option ( '--git-hooks' , '删除.git/hooks目录' , false )
138
143
. option ( '-f,--force' , '强制删除,默认值:false' , false )
139
- . option ( '--all' , '深度删除所有' , false )
144
+ . option ( '-a,- -all' , '深度删除所有' , false )
140
145
. option ( '--ignore-tips' , '忽略提示,直接删除' , false )
146
+ . option ( '--logger' , '开启日志追踪模式' , false )
141
147
. option ( '--dry-run' , '试运行,不做实际删除操作' , false )
142
148
. action ( async ( args : CleanUpOptions ) => {
143
149
await execCleanUp ( args )
0 commit comments