-
Notifications
You must be signed in to change notification settings - Fork 191
/
TsConfigResolver.ts
74 lines (62 loc) · 2.69 KB
/
TsConfigResolver.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
64
65
66
67
68
69
70
71
72
73
74
import { Memoize } from "../decorators";
import { FileUtils, StandardizedFilePath, TransactionalFileSystem } from "../fileSystem";
import { ts } from "../typescript";
import { getTsParseConfigHost, TsParseConfigHostResult } from "./getTsParseConfigHost";
export class TsConfigResolver {
readonly #encoding: string;
readonly #fileSystem: TransactionalFileSystem;
readonly #host: TsParseConfigHostResult;
readonly #tsConfigFilePath: StandardizedFilePath;
readonly #tsConfigDirPath: StandardizedFilePath;
constructor(fileSystem: TransactionalFileSystem, tsConfigFilePath: StandardizedFilePath, encoding: string) {
this.#host = getTsParseConfigHost(fileSystem, { encoding });
this.#tsConfigFilePath = fileSystem.getStandardizedAbsolutePath(tsConfigFilePath);
this.#tsConfigDirPath = FileUtils.getDirPath(this.#tsConfigFilePath);
this.#fileSystem = fileSystem;
this.#encoding = encoding;
}
getCompilerOptions() {
return this._parseJsonConfigFileContent().options;
}
getErrors() {
return this._parseJsonConfigFileContent().errors || [];
}
@Memoize
getPaths(compilerOptions?: ts.CompilerOptions) {
const files = new Set<StandardizedFilePath>();
const fileSystem = this.#fileSystem;
const directories = new Set<StandardizedFilePath>();
compilerOptions = compilerOptions ?? this.getCompilerOptions();
const configFileContent = this._parseJsonConfigFileContent();
for (let dirName of configFileContent.directories) {
const dirPath = fileSystem.getStandardizedAbsolutePath(dirName);
if (fileSystem.directoryExistsSync(dirPath))
directories.add(dirPath);
}
for (let fileName of configFileContent.fileNames) {
const filePath = fileSystem.getStandardizedAbsolutePath(fileName);
const parentDirPath = FileUtils.getDirPath(filePath);
if (fileSystem.fileExistsSync(filePath)) {
directories.add(parentDirPath);
files.add(filePath);
}
}
return {
filePaths: Array.from(files.values()),
directoryPaths: Array.from(directories.values()),
};
}
@Memoize
private _parseJsonConfigFileContent() {
this.#host.clearDirectories();
const result = ts.parseJsonConfigFileContent(this.#getTsConfigFileJson(), this.#host, this.#tsConfigDirPath, undefined, this.#tsConfigFilePath);
return { ...result, directories: this.#host.getDirectories() };
}
#getTsConfigFileJson() {
const text = this.#fileSystem.readFileSync(this.#tsConfigFilePath, this.#encoding);
const parseResult = ts.parseConfigFileTextToJson(this.#tsConfigFilePath, text);
if (parseResult.error != null)
throw new Error(parseResult.error.messageText.toString());
return parseResult.config;
}
}