@@ -2,30 +2,47 @@ import { readdirSync } from "fs"
2
2
import { join } from "path"
3
3
import { default as anymatch } from "anymatch"
4
4
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 )
8
13
9
14
return status
10
15
}
11
16
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 } )
15
26
for ( const file of files ) {
16
- const path = join ( directory , file . name ) ;
27
+ const path = join ( directory , file . name )
17
28
if ( file . isDirectory ( ) ) {
29
+ // if the folder is not ignored, search it recursively
18
30
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
+ }
21
34
}
22
35
} 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
28
40
}
41
+
42
+ // check the exhaustive search group
43
+ status [ 1 ] = status [ 1 ] || anymatch ( exhaustiveSearchGroup , path )
29
44
}
30
45
}
46
+
47
+ return false // continue
31
48
}
0 commit comments