11import path from "node:path" ;
22import { performance } from "node:perf_hooks" ;
33
4+ import Ajv from "ajv" ;
45import type { CAC } from "cac" ;
56import enquirer from "enquirer" ;
67import fs from "fs-extra" ;
@@ -9,6 +10,14 @@ import { ACTIVATION, clearGlob } from "@/config";
910import { execCommand , loggerInfo , printInfo } from "@/helper" ;
1011import { ClearOptions , CodeGeniusOptions } from "@/types" ;
1112
13+ const schema = {
14+ type : "object" ,
15+ properties : {
16+ paths : { type : "array" } ,
17+ } ,
18+ required : [ "paths" ] ,
19+ } ;
20+
1221const mergeConfig = async ( config : CodeGeniusOptions ) => {
1322 const commands = config && config ?. commands ;
1423 if ( commands && commands . clear ) {
@@ -61,6 +70,16 @@ export const clear = async (paths: string[]) => {
6170 if ( ACTIVATION ) {
6271 loggerInfo ( `clear 参数信息: \n ${ JSON . stringify ( paths ) } ` ) ;
6372 }
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+
6483 await execCommand ( "npx" , [ "rimraf" , "--glob" , ...paths ] , {
6584 stdio : "inherit" ,
6685 } ) ;
@@ -74,17 +93,15 @@ export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
7493 cli
7594 . command ( "clear" , "运行 rimraf 删除不再需要的文件或文件夹" )
7695 . option ( "-p, --pattern <pattern>" , "设置匹配规则" )
96+ . option ( "-a, --ask" , "启用询问模式" )
7797 . action ( async ( options ) => {
78- const { pattern } = options ;
98+ const { pattern, ask } = options ;
7999 let paths = [ ] ;
80- if ( ! pattern ) {
100+ if ( ask ) {
81101 const result = await generateEnquirer ( config ) ;
82102 paths = result . files ;
83103 } else {
84- paths =
85- typeof options . pattern === "string"
86- ? [ options . pattern ]
87- : options . pattern ;
104+ paths = typeof pattern === "string" ? [ pattern ] : pattern ;
88105 }
89106 const start = performance . now ( ) ;
90107 await clear ( paths ) ;
0 commit comments