1
1
import path from "node:path" ;
2
2
import { performance } from "node:perf_hooks" ;
3
3
4
- import Ajv from "ajv" ;
5
- import type { CAC } from "cac" ;
6
4
import enquirer from "enquirer" ;
7
5
import fs from "fs-extra" ;
8
6
9
- import { ACTIVATION , clearGlob } from "code-genius" ;
10
- import { execCommand , loggerInfo , printInfo } from "code-genius" ;
11
- import { ClearOptions , CodeGeniusOptions } from "code-genius" ;
12
-
13
- const schema = {
14
- type : "object" ,
15
- properties : {
16
- paths : { type : "array" } ,
17
- } ,
18
- required : [ "paths" ] ,
19
- } ;
20
-
21
- const mergeConfig = async ( config : CodeGeniusOptions ) => {
22
- const commands = config && config ?. commands ;
23
- if ( commands && commands . clear ) {
24
- const { files } = commands . clear ;
25
- return {
26
- paths : files && files . length > 0 ? files : clearGlob ,
27
- } ;
28
- }
29
- return {
30
- paths : clearGlob ,
31
- } ;
32
- } ;
7
+ import { ACTIVATION , execCommand , loggerInfo , printInfo } from "code-genius" ;
8
+ import { ClearOptions , clearGlob , schema , validateArgs } from "./common" ;
33
9
34
10
const generateEnquirer = async (
35
- config : CodeGeniusOptions
36
- ) : Promise < ClearOptions > => {
11
+ paths : Array < string >
12
+ ) : Promise < Array < string > > => {
37
13
const files = fs
38
14
. readdirSync ( path . join ( process . cwd ( ) , "." ) )
39
15
. filter ( ( v ) => ! v . startsWith ( "." ) )
@@ -45,7 +21,6 @@ const generateEnquirer = async (
45
21
} ) ;
46
22
files . sort ( ( v1 , v2 ) => v1 . sort - v2 . sort ) ;
47
23
48
- const { paths } = await mergeConfig ( config ) ;
49
24
const fileMultiChoices = files . map ( ( v ) => {
50
25
return {
51
26
name : `./${ v . file } ` ,
@@ -61,32 +36,24 @@ const generateEnquirer = async (
61
36
choices : fileMultiChoices ,
62
37
} ,
63
38
] ) ;
64
- return {
65
- files : result . files ,
66
- } ;
39
+ return result . files ;
67
40
} ;
68
41
69
42
const clear = async ( paths : string [ ] ) => {
70
43
if ( ACTIVATION ) {
71
44
loggerInfo ( `clear 参数信息: \n ${ JSON . stringify ( paths ) } ` ) ;
72
45
}
73
46
74
- const ajv = new Ajv ( ) ;
75
- const validate = ajv . compile ( schema ) ;
76
- const valid = validate ( {
77
- paths,
78
- } ) ;
79
- if ( ! valid && validate . errors && validate . errors ?. length > 0 ) {
80
- throw new Error ( validate . errors [ 0 ] . message ) ;
81
- }
47
+ validateArgs ( schema , paths ) ;
82
48
83
49
await execCommand ( "npx" , [ "rimraf" , "--glob" , ...paths ] , {
84
50
stdio : "inherit" ,
85
51
} ) ;
86
52
printInfo ( "清理结束" ) ;
87
53
} ;
88
54
89
- export default function clearInstaller ( cli : CAC , config : CodeGeniusOptions ) {
55
+ const clearInstaller = ( config : ClearOptions ) => {
56
+ const { cli, files } = config ;
90
57
return {
91
58
name : "clearInstaller" ,
92
59
setup : ( ) => {
@@ -98,8 +65,7 @@ export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
98
65
const { pattern, ask } = options ;
99
66
let paths = [ ] ;
100
67
if ( ask ) {
101
- const result = await generateEnquirer ( config ) ;
102
- paths = result . files ;
68
+ paths = await generateEnquirer ( files || clearGlob ) ;
103
69
} else {
104
70
paths = typeof pattern === "string" ? [ pattern ] : pattern ;
105
71
}
@@ -110,4 +76,6 @@ export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
110
76
} ) ;
111
77
} ,
112
78
} ;
113
- }
79
+ } ;
80
+
81
+ export { clear , clearInstaller } ;
0 commit comments