1
1
import type { CAC } from "cac" ;
2
2
3
+ import path from "node:path" ;
4
+
5
+ import fs from "fs-extra" ;
6
+ import enquirer from "enquirer" ;
7
+
3
8
import { execCommand , loggerInfo } from "@/shared/index" ;
4
9
import { ACTIVATION , clearGlob } from "@/shared/config" ;
10
+ import { ClearOptions } from "@/shared/types" ;
11
+
12
+ const generateEnquirer = async ( ) : Promise < ClearOptions > => {
13
+ const files = fs
14
+ . readdirSync ( path . join ( process . cwd ( ) , "." ) )
15
+ . filter ( ( v ) => ! v . startsWith ( "." ) )
16
+ . map ( ( file ) => {
17
+ return {
18
+ sort : fs . statSync ( path . join ( process . cwd ( ) , file ) ) . isFile ( ) ? 1 : 0 ,
19
+ file,
20
+ } ;
21
+ } ) ;
22
+ files . sort ( ( v1 , v2 ) => v1 . sort - v2 . sort ) ;
23
+ const fileMultiChoices = files . map ( ( v ) => {
24
+ return {
25
+ name : `./${ v . file } ` ,
26
+ message : `${ v . file } ` ,
27
+ hint : clearGlob . includes ( `./${ v . file } ` ) ? "建议清理" : "" ,
28
+ } ;
29
+ } ) ;
30
+ const result = await enquirer . prompt < ClearOptions > ( [
31
+ {
32
+ name : "files" ,
33
+ type : "multiselect" ,
34
+ message : "请选择需要清理的文件/夹" ,
35
+ choices : fileMultiChoices ,
36
+ } ,
37
+ ] ) ;
38
+ return {
39
+ files : result . files ,
40
+ } ;
41
+ } ;
5
42
6
43
export const clear = async ( paths : string [ ] ) => {
7
44
if ( ACTIVATION ) {
@@ -18,15 +55,20 @@ export default function clearInstaller(cli: CAC) {
18
55
setup : ( ) => {
19
56
cli
20
57
. command ( "clear" , "运行 rimraf 删除不再需要的文件或文件夹" )
21
- . option ( "-p, --pattern <pattern>" , "设置配置规则" , {
22
- default : [ ...clearGlob ] ,
23
- } )
58
+ . option ( "-p, --pattern <pattern>" , "设置匹配规则" )
24
59
. action ( async ( options ) => {
25
- const patterns =
26
- typeof options . pattern === "string"
27
- ? [ options . pattern ]
28
- : options . pattern ;
29
- await clear ( patterns ) ;
60
+ const { pattern } = options ;
61
+ let paths = [ ] ;
62
+ if ( ! pattern ) {
63
+ const result = await generateEnquirer ( ) ;
64
+ paths = result . files ;
65
+ } else {
66
+ paths =
67
+ typeof options . pattern === "string"
68
+ ? [ options . pattern ]
69
+ : options . pattern ;
70
+ }
71
+ await clear ( paths ) ;
30
72
} ) ;
31
73
} ,
32
74
} ;
0 commit comments