-
Notifications
You must be signed in to change notification settings - Fork 690
/
typedoc.ts
192 lines (175 loc) · 6.37 KB
/
typedoc.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { join, dirname, resolve } from "path";
import * as FS from "fs";
import ts from "typescript";
import type { OptionsReader } from "..";
import type { Logger } from "../../loggers";
import type { Options } from "../options";
import { ok } from "assert";
import { nicePath, normalizePath } from "../../paths";
import { isFile } from "../../fs";
import { createRequire } from "module";
import { pathToFileURL } from "url";
/**
* Obtains option values from typedoc.json
*
* Changes need to happen here at some point. I think we should follow ESLint's new config
* system eventually: https://eslint.org/blog/2022/08/new-config-system-part-1/
*/
export class TypeDocReader implements OptionsReader {
/**
* Should run before the tsconfig reader so that it can specify a tsconfig file to read.
*/
order = 100;
name = "typedoc-json";
supportsPackages = true;
/**
* Read user configuration from a typedoc.json or typedoc.js configuration file.
*/
async read(container: Options, logger: Logger, cwd: string): Promise<void> {
const path = container.getValue("options") || cwd;
const file = this.findTypedocFile(path);
if (!file) {
if (container.isSet("options")) {
logger.error(
`The options file ${nicePath(path)} does not exist.`,
);
}
return;
}
const seen = new Set<string>();
await this.readFile(file, container, logger, seen);
}
/**
* Read the given options file + any extended files.
* @param file
* @param container
* @param logger
*/
private async readFile(
file: string,
container: Options & { setValue(key: string, value: unknown): void },
logger: Logger,
seen: Set<string>,
) {
if (seen.has(file)) {
logger.error(
`Tried to load the options file ${nicePath(
file,
)} multiple times.`,
);
return;
}
seen.add(file);
let fileContent: any;
if (file.endsWith(".json")) {
const readResult = ts.readConfigFile(normalizePath(file), (path) =>
FS.readFileSync(path, "utf-8"),
);
if (readResult.error) {
logger.error(
`Failed to parse ${nicePath(
file,
)}, ensure it exists and contains an object.`,
);
return;
} else {
fileContent = readResult.config;
}
} else {
try {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
fileContent = await require(file);
} catch (error: any) {
if (error?.code === "ERR_REQUIRE_ESM") {
// On Windows, we need to ensure this path is a file path.
// Or we'll get ERR_UNSUPPORTED_ESM_URL_SCHEME
const esmPath = pathToFileURL(file).toString();
fileContent = await (await import(esmPath)).default;
} else {
throw error;
}
}
} catch (error) {
logger.error(
`Failed to read ${nicePath(file)}: ${
error instanceof Error ? error.message : error
}`,
);
return;
}
}
if (typeof fileContent !== "object" || !fileContent) {
logger.error(
`The root value of ${nicePath(file)} is not an object.`,
);
return;
}
// clone option object to avoid of property changes in re-calling this file
const data = { ...fileContent };
delete data["$schema"]; // Useful for better autocompletion, should not be read as a key.
if ("extends" in data) {
const resolver = createRequire(file);
const extended: string[] = getStringArray(data["extends"]);
for (const extendedFile of extended) {
let resolvedParent: string;
try {
resolvedParent = resolver.resolve(extendedFile);
} catch {
logger.error(
`Failed to resolve ${extendedFile} to a file in ${nicePath(
file,
)}`,
);
continue;
}
await this.readFile(resolvedParent, container, logger, seen);
}
delete data["extends"];
}
for (const [key, val] of Object.entries(data)) {
try {
container.setValue(
key as never,
val as never,
resolve(dirname(file)),
);
} catch (error) {
ok(error instanceof Error);
logger.error(error.message);
}
}
}
/**
* Search for the configuration file given path
*
* @param path Path to the typedoc.(js|json) file. If path is a directory
* typedoc file will be attempted to be found at the root of this path
* @return the typedoc.(js|json) file path or undefined
*/
private findTypedocFile(path: string): string | undefined {
path = resolve(path);
return [
path,
join(path, "typedoc.json"),
join(path, "typedoc.jsonc"),
join(path, "typedoc.config.js"),
join(path, "typedoc.config.cjs"),
join(path, "typedoc.config.mjs"),
join(path, "typedoc.js"),
join(path, "typedoc.cjs"),
join(path, "typedoc.mjs"),
join(path, ".config/typedoc.json"),
join(path, ".config/typedoc.jsonc"),
join(path, ".config/typedoc.config.js"),
join(path, ".config/typedoc.config.cjs"),
join(path, ".config/typedoc.config.mjs"),
join(path, ".config/typedoc.js"),
join(path, ".config/typedoc.cjs"),
join(path, ".config/typedoc.mjs"),
].find(isFile);
}
}
function getStringArray(arg: unknown): string[] {
return Array.isArray(arg) ? arg.map(String) : [String(arg)];
}