-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
getProjectConfigFiles.ts
63 lines (53 loc) · 1.98 KB
/
getProjectConfigFiles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import debug from 'debug';
import * as fs from 'fs';
import * as path from 'path';
import type { ParseSettings } from '.';
const log = debug('typescript-eslint:typescript-estree:getProjectConfigFiles');
const TSCONFIG_MATCH_CACHE = new Map<string, string | undefined>();
/**
* @remarks Only use this for tests!
*/
export function clearMatchCacheForTests(): void {
tsconfigMatchCache.clear();
}
/**
* Checks for a matching TSConfig to a file including its parent directories,
* permanently caching results under each directory it checks.
*
* @remarks
* We don't (yet!) have a way to attach file watchers on disk, but still need to
* cache file checks for rapid subsequent calls to fs.existsSync. See discussion
* in https://github.com/typescript-eslint/typescript-eslint/issues/101.
*/
export function getProjectConfigFiles(
parseSettings: Pick<ParseSettings, 'filePath' | 'tsconfigRootDir'>,
project: string | string[] | true | undefined,
): string | string[] | undefined {
if (project !== true) {
return Array.isArray(project) ? project : [project];
}
log('Looking for tsconfig.json at or above file: %s', parseSettings.filePath);
let directory = path.dirname(parseSettings.filePath);
const checkedDirectories = [directory];
do {
log('Checking tsconfig.json path: %s', directory);
const tsconfigPath = path.join(directory, 'tsconfig.json');
const cached =
tsconfigMatchCache.get(directory) ??
(fs.existsSync(tsconfigPath) && tsconfigPath);
if (cached) {
for (const directory of checkedDirectories) {
tsconfigMatchCache.set(directory, cached);
}
return [cached];
}
directory = path.dirname(directory);
checkedDirectories.push(directory);
} while (
directory.length > 1 &&
directory.length >= parseSettings.tsconfigRootDir.length
);
throw new Error(
`project was set to \`true\` but couldn't find any tsconfig.json relative to '${parseSettings.filePath}' within '${parseSettings.tsconfigRootDir}'.`,
);
}