1
1
import path from "node:path" ;
2
2
import { performance } from "node:perf_hooks" ;
3
3
4
+ import Ajv from "ajv" ;
4
5
import type { CAC } from "cac" ;
5
6
import enquirer from "enquirer" ;
6
7
import fs from "fs-extra" ;
@@ -9,6 +10,14 @@ import { ACTIVATION, clearGlob } from "@/config";
9
10
import { execCommand , loggerInfo , printInfo } from "@/helper" ;
10
11
import { ClearOptions , CodeGeniusOptions } from "@/types" ;
11
12
13
+ const schema = {
14
+ type : "object" ,
15
+ properties : {
16
+ paths : { type : "array" } ,
17
+ } ,
18
+ required : [ "paths" ] ,
19
+ } ;
20
+
12
21
const mergeConfig = async ( config : CodeGeniusOptions ) => {
13
22
const commands = config && config ?. commands ;
14
23
if ( commands && commands . clear ) {
@@ -61,6 +70,16 @@ export const clear = async (paths: string[]) => {
61
70
if ( ACTIVATION ) {
62
71
loggerInfo ( `clear 参数信息: \n ${ JSON . stringify ( paths ) } ` ) ;
63
72
}
73
+
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
+ }
82
+
64
83
await execCommand ( "npx" , [ "rimraf" , "--glob" , ...paths ] , {
65
84
stdio : "inherit" ,
66
85
} ) ;
@@ -74,17 +93,15 @@ export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
74
93
cli
75
94
. command ( "clear" , "运行 rimraf 删除不再需要的文件或文件夹" )
76
95
. option ( "-p, --pattern <pattern>" , "设置匹配规则" )
96
+ . option ( "-a, --ask" , "启用询问模式" )
77
97
. action ( async ( options ) => {
78
- const { pattern } = options ;
98
+ const { pattern, ask } = options ;
79
99
let paths = [ ] ;
80
- if ( ! pattern ) {
100
+ if ( ask ) {
81
101
const result = await generateEnquirer ( config ) ;
82
102
paths = result . files ;
83
103
} else {
84
- paths =
85
- typeof options . pattern === "string"
86
- ? [ options . pattern ]
87
- : options . pattern ;
104
+ paths = typeof pattern === "string" ? [ pattern ] : pattern ;
88
105
}
89
106
const start = performance . now ( ) ;
90
107
await clear ( paths ) ;
0 commit comments