@@ -16,6 +16,70 @@ namespace ts {
16
16
// Update: We also consider a path like `C:\foo.ts` "relative" because we do not search for it in `node_modules` or treat it as an ambient module.
17
17
return pathIsRelative ( moduleName ) || isRootedDiskPath ( moduleName ) ;
18
18
}
19
+
20
+ export interface FileSystemEntries {
21
+ readonly files : ReadonlyArray < string > ;
22
+ readonly directories : ReadonlyArray < string > ;
23
+ }
24
+
25
+ export function matchFiles ( path : string , extensions : ReadonlyArray < string > , excludes : ReadonlyArray < string > , includes : ReadonlyArray < string > , useCaseSensitiveFileNames : boolean , currentDirectory : string , depth : number | undefined , getFileSystemEntries : ( path : string ) => FileSystemEntries ) : string [ ] {
26
+ path = normalizePath ( path ) ;
27
+ currentDirectory = normalizePath ( currentDirectory ) ;
28
+
29
+ const comparer = useCaseSensitiveFileNames ? compareStringsCaseSensitive : compareStringsCaseInsensitive ;
30
+ const patterns = getFileMatcherPatterns ( path , excludes , includes , useCaseSensitiveFileNames , currentDirectory ) ;
31
+
32
+ const regexFlag = useCaseSensitiveFileNames ? "" : "i" ;
33
+ const includeFileRegexes = patterns . includeFilePatterns && patterns . includeFilePatterns . map ( pattern => new RegExp ( pattern , regexFlag ) ) ;
34
+ const includeDirectoryRegex = patterns . includeDirectoryPattern && new RegExp ( patterns . includeDirectoryPattern , regexFlag ) ;
35
+ const excludeRegex = patterns . excludePattern && new RegExp ( patterns . excludePattern , regexFlag ) ;
36
+
37
+ // Associate an array of results with each include regex. This keeps results in order of the "include" order.
38
+ // If there are no "includes", then just put everything in results[0].
39
+ const results : string [ ] [ ] = includeFileRegexes ? includeFileRegexes . map ( ( ) => [ ] ) : [ [ ] ] ;
40
+
41
+ for ( const basePath of patterns . basePaths ) {
42
+ visitDirectory ( basePath , combinePaths ( currentDirectory , basePath ) , depth ) ;
43
+ }
44
+
45
+ return flatten < string > ( results ) ;
46
+
47
+ function visitDirectory ( path : string , absolutePath : string , depth : number | undefined ) {
48
+ const { files, directories } = getFileSystemEntries ( path ) ;
49
+
50
+ for ( const current of sort ( files , comparer ) ) {
51
+ const name = combinePaths ( path , current ) ;
52
+ const absoluteName = combinePaths ( absolutePath , current ) ;
53
+ if ( extensions && ! fileExtensionIsOneOf ( name , extensions ) ) continue ;
54
+ if ( excludeRegex && excludeRegex . test ( absoluteName ) ) continue ;
55
+ if ( ! includeFileRegexes ) {
56
+ results [ 0 ] . push ( name ) ;
57
+ }
58
+ else {
59
+ const includeIndex = findIndex ( includeFileRegexes , re => re . test ( absoluteName ) ) ;
60
+ if ( includeIndex !== - 1 ) {
61
+ results [ includeIndex ] . push ( name ) ;
62
+ }
63
+ }
64
+ }
65
+
66
+ if ( depth !== undefined ) {
67
+ depth -- ;
68
+ if ( depth === 0 ) {
69
+ return ;
70
+ }
71
+ }
72
+
73
+ for ( const current of sort ( directories , comparer ) ) {
74
+ const name = combinePaths ( path , current ) ;
75
+ const absoluteName = combinePaths ( absolutePath , current ) ;
76
+ if ( ( ! includeDirectoryRegex || includeDirectoryRegex . test ( absoluteName ) ) &&
77
+ ( ! excludeRegex || ! excludeRegex . test ( absoluteName ) ) ) {
78
+ visitDirectory ( name , absoluteName , depth ) ;
79
+ }
80
+ }
81
+ }
82
+ }
19
83
}
20
84
21
85
/* @internal */
@@ -2454,11 +2518,6 @@ namespace ts {
2454
2518
return match === "*" ? singleAsteriskRegexFragment : match === "?" ? "[^/]" : "\\" + match ;
2455
2519
}
2456
2520
2457
- export interface FileSystemEntries {
2458
- readonly files : ReadonlyArray < string > ;
2459
- readonly directories : ReadonlyArray < string > ;
2460
- }
2461
-
2462
2521
export interface FileMatcherPatterns {
2463
2522
/** One pattern for each "include" spec. */
2464
2523
includeFilePatterns : ReadonlyArray < string > ;
@@ -2483,65 +2542,6 @@ namespace ts {
2483
2542
} ;
2484
2543
}
2485
2544
2486
- export function matchFiles ( path : string , extensions : ReadonlyArray < string > , excludes : ReadonlyArray < string > , includes : ReadonlyArray < string > , useCaseSensitiveFileNames : boolean , currentDirectory : string , depth : number | undefined , getFileSystemEntries : ( path : string ) => FileSystemEntries ) : string [ ] {
2487
- path = normalizePath ( path ) ;
2488
- currentDirectory = normalizePath ( currentDirectory ) ;
2489
-
2490
- const comparer = useCaseSensitiveFileNames ? compareStringsCaseSensitive : compareStringsCaseInsensitive ;
2491
- const patterns = getFileMatcherPatterns ( path , excludes , includes , useCaseSensitiveFileNames , currentDirectory ) ;
2492
-
2493
- const regexFlag = useCaseSensitiveFileNames ? "" : "i" ;
2494
- const includeFileRegexes = patterns . includeFilePatterns && patterns . includeFilePatterns . map ( pattern => new RegExp ( pattern , regexFlag ) ) ;
2495
- const includeDirectoryRegex = patterns . includeDirectoryPattern && new RegExp ( patterns . includeDirectoryPattern , regexFlag ) ;
2496
- const excludeRegex = patterns . excludePattern && new RegExp ( patterns . excludePattern , regexFlag ) ;
2497
-
2498
- // Associate an array of results with each include regex. This keeps results in order of the "include" order.
2499
- // If there are no "includes", then just put everything in results[0].
2500
- const results : string [ ] [ ] = includeFileRegexes ? includeFileRegexes . map ( ( ) => [ ] ) : [ [ ] ] ;
2501
-
2502
- for ( const basePath of patterns . basePaths ) {
2503
- visitDirectory ( basePath , combinePaths ( currentDirectory , basePath ) , depth ) ;
2504
- }
2505
-
2506
- return flatten < string > ( results ) ;
2507
-
2508
- function visitDirectory ( path : string , absolutePath : string , depth : number | undefined ) {
2509
- const { files, directories } = getFileSystemEntries ( path ) ;
2510
-
2511
- for ( const current of sort ( files , comparer ) ) {
2512
- const name = combinePaths ( path , current ) ;
2513
- const absoluteName = combinePaths ( absolutePath , current ) ;
2514
- if ( extensions && ! fileExtensionIsOneOf ( name , extensions ) ) continue ;
2515
- if ( excludeRegex && excludeRegex . test ( absoluteName ) ) continue ;
2516
- if ( ! includeFileRegexes ) {
2517
- results [ 0 ] . push ( name ) ;
2518
- }
2519
- else {
2520
- const includeIndex = findIndex ( includeFileRegexes , re => re . test ( absoluteName ) ) ;
2521
- if ( includeIndex !== - 1 ) {
2522
- results [ includeIndex ] . push ( name ) ;
2523
- }
2524
- }
2525
- }
2526
-
2527
- if ( depth !== undefined ) {
2528
- depth -- ;
2529
- if ( depth === 0 ) {
2530
- return ;
2531
- }
2532
- }
2533
-
2534
- for ( const current of sort ( directories , comparer ) ) {
2535
- const name = combinePaths ( path , current ) ;
2536
- const absoluteName = combinePaths ( absolutePath , current ) ;
2537
- if ( ( ! includeDirectoryRegex || includeDirectoryRegex . test ( absoluteName ) ) &&
2538
- ( ! excludeRegex || ! excludeRegex . test ( absoluteName ) ) ) {
2539
- visitDirectory ( name , absoluteName , depth ) ;
2540
- }
2541
- }
2542
- }
2543
- }
2544
-
2545
2545
/**
2546
2546
* Computes the unique non-wildcard base paths amongst the provided include patterns.
2547
2547
*/
0 commit comments