/
tsconfig.ts
100 lines (85 loc) · 2.99 KB
/
tsconfig.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { resolve, join } from "path";
import { existsSync, statSync } from "fs";
import * as ts from "typescript";
import type { OptionsReader, Options } from "../options";
import type { Logger } from "../../loggers";
import { normalizePath } from "../../fs";
import { ok } from "assert";
function isFile(file: string) {
return existsSync(file) && statSync(file).isFile();
}
function isDir(path: string) {
return existsSync(path) && statSync(path).isDirectory();
}
export class TSConfigReader implements OptionsReader {
/**
* Note: Runs after the {@link TypeDocReader}.
*/
priority = 200;
name = "tsconfig-json";
read(container: Options, logger: Logger): void {
const file = container.getValue("tsconfig");
let fileToRead: string | undefined = file;
if (isDir(fileToRead)) {
fileToRead = ts.findConfigFile(file, isFile);
}
if (!fileToRead || !isFile(fileToRead)) {
// If the user didn't give us this option, we shouldn't complain about not being able to find it.
if (container.isSet("tsconfig")) {
logger.error(`The tsconfig file ${file} does not exist`);
}
return;
}
fileToRead = normalizePath(resolve(fileToRead));
let fatalError = false as boolean;
const parsed = ts.getParsedCommandLineOfConfigFile(
fileToRead,
{},
{
...ts.sys,
onUnRecoverableConfigFileDiagnostic(error) {
logger.diagnostic(error);
fatalError = true;
},
}
);
if (!parsed || fatalError) {
return;
}
logger.diagnostics(parsed.errors);
const typedocOptions = parsed.raw?.typedocOptions ?? {};
if (typedocOptions.options) {
logger.error(
[
"typedocOptions in tsconfig file specifies an option file to read but the option",
"file has already been read. This is likely a misconfiguration.",
].join(" ")
);
delete typedocOptions.options;
}
if (typedocOptions.tsconfig) {
logger.error(
"typedocOptions in tsconfig file may not specify a tsconfig file to read"
);
delete typedocOptions.tsconfig;
}
container.setCompilerOptions(
parsed.fileNames,
parsed.options,
parsed.projectReferences
);
for (const [key, val] of Object.entries(typedocOptions || {})) {
try {
// We catch the error, so can ignore the strict type checks
container.setValue(
key as never,
val as never,
join(fileToRead, "..")
);
} catch (error) {
ok(error instanceof Error);
logger.error(error.message);
}
}
}
}