1
- import glob from 'fast-glob '
2
- import os from 'node:os '
1
+ import { fdir } from 'fdir '
2
+ import picomatch from 'picomatch '
3
3
4
4
interface SearchFilesOptions {
5
5
/** The directory to search in */
@@ -13,15 +13,51 @@ interface SearchFilesOptions {
13
13
}
14
14
15
15
export async function searchFiles ( opts : SearchFilesOptions ) {
16
- let results = await glob ( opts . include , {
17
- cwd : opts . root ,
18
- ignore : opts . exclude ,
19
- onlyFiles : true ,
20
- absolute : true ,
16
+ // Ignore patterns in the project search are generally intended to match
17
+ // directories but _may_ also match files.
18
+ //
19
+ // A pattern like `**/node_modules` should match like these:
20
+ // - `**/node_modules/` to exclude directories from traversal
21
+ // - `**/node_modules/**/*` to exclude files from results
22
+ //
23
+ // This approximately matches how fast-glob's `ignore` option works.
24
+ let shouldIgnore = picomatch (
25
+ [ ...new Set ( opts . exclude . flatMap ( ( x ) => [ x , `${ x } /` , `${ x } /**/*` ] ) ) ] ,
26
+ { dot : true } ,
27
+ )
28
+
29
+ let crawler = new fdir ( {
30
+ // onlyFiles: true
31
+ includeDirs : false ,
32
+ excludeFiles : false ,
33
+
34
+ // absolute: true
35
+ relativePaths : false ,
36
+ resolvePaths : false ,
37
+ includeBasePath : true ,
38
+
39
+ // followSymbolicLinks: true
40
+ resolveSymlinks : false ,
41
+ excludeSymlinks : false ,
42
+
43
+ //
21
44
suppressErrors : true ,
22
- dot : true ,
23
- concurrency : Math . max ( os . cpus ( ) . length , 1 ) ,
45
+
46
+ // Normalize Windows paths to use forward slashes
47
+ pathSeparator : '/' ,
48
+
49
+ // Don't return files that are ignored
50
+ filters : [ ( path ) => ! shouldIgnore ( path ) ] ,
51
+
52
+ // Don't traverse into directories that are ignored
53
+ exclude : ( _ , path ) => shouldIgnore ( path ) ,
54
+
55
+ globFunction : picomatch ,
24
56
} )
25
57
58
+ crawler = crawler . globWithOptions ( opts . include , { dot : true } )
59
+
60
+ let results = await crawler . crawl ( opts . root ) . withPromise ( )
61
+
26
62
return results
27
63
}
0 commit comments