11import path from "node:path" ;
22import { performance } from "node:perf_hooks" ;
33
4- import Ajv from "ajv" ;
5- import type { CAC } from "cac" ;
64import enquirer from "enquirer" ;
75import fs from "fs-extra" ;
86
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" ;
339
3410const generateEnquirer = async (
35- config : CodeGeniusOptions
36- ) : Promise < ClearOptions > => {
11+ paths : Array < string >
12+ ) : Promise < Array < string > > => {
3713 const files = fs
3814 . readdirSync ( path . join ( process . cwd ( ) , "." ) )
3915 . filter ( ( v ) => ! v . startsWith ( "." ) )
@@ -45,7 +21,6 @@ const generateEnquirer = async (
4521 } ) ;
4622 files . sort ( ( v1 , v2 ) => v1 . sort - v2 . sort ) ;
4723
48- const { paths } = await mergeConfig ( config ) ;
4924 const fileMultiChoices = files . map ( ( v ) => {
5025 return {
5126 name : `./${ v . file } ` ,
@@ -61,32 +36,24 @@ const generateEnquirer = async (
6136 choices : fileMultiChoices ,
6237 } ,
6338 ] ) ;
64- return {
65- files : result . files ,
66- } ;
39+ return result . files ;
6740} ;
6841
6942const clear = async ( paths : string [ ] ) => {
7043 if ( ACTIVATION ) {
7144 loggerInfo ( `clear 参数信息: \n ${ JSON . stringify ( paths ) } ` ) ;
7245 }
7346
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 ) ;
8248
8349 await execCommand ( "npx" , [ "rimraf" , "--glob" , ...paths ] , {
8450 stdio : "inherit" ,
8551 } ) ;
8652 printInfo ( "清理结束" ) ;
8753} ;
8854
89- export default function clearInstaller ( cli : CAC , config : CodeGeniusOptions ) {
55+ const clearInstaller = ( config : ClearOptions ) => {
56+ const { cli, files } = config ;
9057 return {
9158 name : "clearInstaller" ,
9259 setup : ( ) => {
@@ -98,8 +65,7 @@ export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
9865 const { pattern, ask } = options ;
9966 let paths = [ ] ;
10067 if ( ask ) {
101- const result = await generateEnquirer ( config ) ;
102- paths = result . files ;
68+ paths = await generateEnquirer ( files || clearGlob ) ;
10369 } else {
10470 paths = typeof pattern === "string" ? [ pattern ] : pattern ;
10571 }
@@ -110,4 +76,6 @@ export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
11076 } ) ;
11177 } ,
11278 } ;
113- }
79+ } ;
80+
81+ export { clear , clearInstaller } ;
0 commit comments