@@ -2,30 +2,47 @@ import { readdirSync } from "fs"
22import { join } from "path"
33import { default as anymatch } from "anymatch"
44
5- export function findFilesForGroups ( cwd : string , searchGroups : string [ ] [ ] , ignored : string [ ] ) {
6- const status = searchGroups . map ( ( ) => false ) ;
7- searchDirectory ( cwd , status , searchGroups , ignored ) ;
5+ export function findFilesForGroups (
6+ cwd : string ,
7+ earlyExitSearchGroup : string [ ] ,
8+ exhaustiveSearchGroup : string [ ] ,
9+ ignored : string [ ] ,
10+ ) {
11+ const status = [ false , false ]
12+ searchDirectory ( cwd , status , earlyExitSearchGroup , exhaustiveSearchGroup , ignored )
813
914 return status
1015}
1116
12- function searchDirectory ( directory : string , status : boolean [ ] , searchGroups : string [ ] [ ] , ignored : string [ ] ) {
13- // recursively search the current folder for a file with the given fileEnding, ignoring the given folders, and return true as soon as one is found
14- const files = readdirSync ( directory , { withFileTypes : true , recursive : false } ) ;
17+ function searchDirectory (
18+ directory : string ,
19+ status : boolean [ ] ,
20+ earlyExitSearchGroup : string [ ] ,
21+ exhaustiveSearchGroup : string [ ] ,
22+ ignored : string [ ] ,
23+ ) : boolean {
24+ // recursively search the current folder for a file with the given fileEnding, ignoring the given folders, and return true as soon as one is found
25+ const files = readdirSync ( directory , { withFileTypes : true , recursive : false } )
1526 for ( const file of files ) {
16- const path = join ( directory , file . name ) ;
27+ const path = join ( directory , file . name )
1728 if ( file . isDirectory ( ) ) {
29+ // if the folder is not ignored, search it recursively
1830 if ( ! anymatch ( ignored , path ) ) {
19- // if the folder is not ignored, search it recursively
20- searchDirectory ( path , status , searchGroups , ignored ) ;
31+ if ( searchDirectory ( path , status , earlyExitSearchGroup , exhaustiveSearchGroup , ignored ) ) {
32+ return true // exit
33+ }
2134 }
2235 } else {
23- // for each search group, check if the file matches any of the patterns
24- for ( const [ index , searchGroup ] of searchGroups . entries ( ) ) {
25- if ( ! status [ index ] && anymatch ( searchGroup , path ) ) {
26- status [ index ] = true ;
27- }
36+ // check the early exit search group first
37+ status [ 0 ] = status [ 0 ] || anymatch ( exhaustiveSearchGroup , path )
38+ if ( status [ 0 ] ) {
39+ return true // exit
2840 }
41+
42+ // check the exhaustive search group
43+ status [ 1 ] = status [ 1 ] || anymatch ( exhaustiveSearchGroup , path )
2944 }
3045 }
46+
47+ return false // continue
3148}
0 commit comments