Skip to content

Commit 453f669

Browse files
committed
Expose ts.matchFiles as public API (microsoft#13793)
1 parent 4eb633e commit 453f669

File tree

1 file changed

+64
-64
lines changed

1 file changed

+64
-64
lines changed

src/compiler/core.ts

+64-64
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,70 @@ namespace ts {
1616
// 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.
1717
return pathIsRelative(moduleName) || isRootedDiskPath(moduleName);
1818
}
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+
}
1983
}
2084

2185
/* @internal */
@@ -2454,11 +2518,6 @@ namespace ts {
24542518
return match === "*" ? singleAsteriskRegexFragment : match === "?" ? "[^/]" : "\\" + match;
24552519
}
24562520

2457-
export interface FileSystemEntries {
2458-
readonly files: ReadonlyArray<string>;
2459-
readonly directories: ReadonlyArray<string>;
2460-
}
2461-
24622521
export interface FileMatcherPatterns {
24632522
/** One pattern for each "include" spec. */
24642523
includeFilePatterns: ReadonlyArray<string>;
@@ -2483,65 +2542,6 @@ namespace ts {
24832542
};
24842543
}
24852544

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-
25452545
/**
25462546
* Computes the unique non-wildcard base paths amongst the provided include patterns.
25472547
*/

0 commit comments

Comments
 (0)