/
entry-point.ts
458 lines (416 loc) · 14.5 KB
/
entry-point.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import { join, relative, resolve } from "path";
import * as ts from "typescript";
import * as FS from "fs";
import * as Path from "path";
import {
expandPackages,
extractTypedocConfigFromPackageManifest,
getTsEntryPointForPackage,
ignorePackage,
loadPackageManifest,
} from "./package-manifest";
import { createMinimatch, matchesAny, nicePath } from "./paths";
import type { Logger } from "./loggers";
import type { Options } from "./options";
import { getCommonDirectory, glob, normalizePath } from "./fs";
import { validate } from "./validation";
import { filterMap } from "./array";
/**
* Defines how entry points are interpreted.
* @enum
*/
export const EntryPointStrategy = {
/**
* The default behavior in v0.22+, expects all provided entry points as being part of a single program.
* Any directories included in the entry point list will result in `dir/index.([cm][tj]s|[tj]sx?)` being used.
*/
Resolve: "resolve",
/**
* The default behavior in v0.21 and earlier. Behaves like the resolve behavior, but will recursively
* expand directories into an entry point for each file within the directory.
*/
Expand: "expand",
/**
* Alternative resolution mode useful for monorepos. With this mode, TypeDoc will look for a package.json
* and tsconfig.json under each provided entry point. The `main` field of each package will be documented.
*/
Packages: "packages",
} as const;
export type EntryPointStrategy =
typeof EntryPointStrategy[keyof typeof EntryPointStrategy];
export interface DocumentationEntryPoint {
displayName: string;
readmeFile?: string;
program: ts.Program;
sourceFile: ts.SourceFile;
version?: string;
}
export function getEntryPoints(
logger: Logger,
options: Options
): DocumentationEntryPoint[] | undefined {
const entryPoints = options.getValue("entryPoints");
let result: DocumentationEntryPoint[] | undefined;
switch (options.getValue("entryPointStrategy")) {
case EntryPointStrategy.Resolve:
result = getEntryPointsForPaths(
logger,
expandGlobs(entryPoints),
options
);
break;
case EntryPointStrategy.Expand:
result = getExpandedEntryPointsForPaths(
logger,
expandGlobs(entryPoints),
options
);
break;
case EntryPointStrategy.Packages:
result = getEntryPointsForPackages(logger, entryPoints, options);
break;
}
if (result && result.length === 0) {
logger.error(
"Unable to find any entry points. Make sure TypeDoc can find your tsconfig"
);
return;
}
return result;
}
export function getWatchEntryPoints(
logger: Logger,
options: Options,
program: ts.Program
): DocumentationEntryPoint[] | undefined {
let result: DocumentationEntryPoint[] | undefined;
const entryPoints = options.getValue("entryPoints");
switch (options.getValue("entryPointStrategy")) {
case EntryPointStrategy.Resolve:
result = getEntryPointsForPaths(logger, entryPoints, options, [
program,
]);
break;
case EntryPointStrategy.Expand:
result = getExpandedEntryPointsForPaths(
logger,
entryPoints,
options,
[program]
);
break;
case EntryPointStrategy.Packages:
logger.error(
"Watch mode does not support 'packages' style entry points."
);
break;
}
if (result && result.length === 0) {
logger.error("Unable to find any entry points.");
return;
}
return result;
}
function getModuleName(fileName: string, baseDir: string) {
return normalizePath(relative(baseDir, fileName)).replace(
/(\/index)?(\.d)?\.([cm][tj]s|[tj]sx?)$/,
""
);
}
/**
* Converts a list of file-oriented paths in to DocumentationEntryPoints for conversion.
* This is in contrast with the package-oriented `getEntryPointsForPackages`
*/
function getEntryPointsForPaths(
logger: Logger,
inputFiles: string[],
options: Options,
programs = getEntryPrograms(logger, options)
): DocumentationEntryPoint[] | undefined {
const baseDir =
options.getValue("basePath") || getCommonDirectory(inputFiles);
const entryPoints: DocumentationEntryPoint[] = [];
entryLoop: for (const fileOrDir of inputFiles.map(normalizePath)) {
const toCheck = [fileOrDir];
if (!/\.([cm][tj]s|[tj]sx?)$/.test(fileOrDir)) {
toCheck.push(
`${fileOrDir}/index.ts`,
`${fileOrDir}/index.cts`,
`${fileOrDir}/index.mts`,
`${fileOrDir}/index.tsx`,
`${fileOrDir}/index.js`,
`${fileOrDir}/index.cjs`,
`${fileOrDir}/index.mjs`,
`${fileOrDir}/index.jsx`
);
}
for (const program of programs) {
for (const check of toCheck) {
const sourceFile = program.getSourceFile(check);
if (sourceFile) {
entryPoints.push({
displayName: getModuleName(resolve(check), baseDir),
sourceFile,
program,
});
continue entryLoop;
}
}
}
logger.warn(`Unable to locate entry point: ${fileOrDir}`);
}
return entryPoints;
}
export function getExpandedEntryPointsForPaths(
logger: Logger,
inputFiles: string[],
options: Options,
programs = getEntryPrograms(logger, options)
): DocumentationEntryPoint[] | undefined {
return getEntryPointsForPaths(
logger,
expandInputFiles(logger, inputFiles, options),
options,
programs
);
}
function expandGlobs(inputFiles: string[]) {
const base = getCommonDirectory(inputFiles);
const result = inputFiles.flatMap((entry) =>
glob(entry, base, { includeDirectories: true })
);
return result;
}
function getEntryPrograms(logger: Logger, options: Options) {
const rootProgram = ts.createProgram({
rootNames: options.getFileNames(),
options: options.getCompilerOptions(),
projectReferences: options.getProjectReferences(),
});
const programs = [rootProgram];
// This might be a solution style tsconfig, in which case we need to add a program for each
// reference so that the converter can look through each of these.
if (rootProgram.getRootFileNames().length === 0) {
logger.verbose(
"tsconfig appears to be a solution style tsconfig - creating programs for references"
);
const resolvedReferences = rootProgram.getResolvedProjectReferences();
for (const ref of resolvedReferences ?? []) {
if (!ref) continue; // This indicates bad configuration... will be reported later.
programs.push(
ts.createProgram({
options: options.fixCompilerOptions(
ref.commandLine.options
),
rootNames: ref.commandLine.fileNames,
projectReferences: ref.commandLine.projectReferences,
})
);
}
}
return programs;
}
/**
* Expand a list of input files.
*
* Searches for directories in the input files list and replaces them with a
* listing of all TypeScript files within them. One may use the ```--exclude``` option
* to filter out files with a pattern.
*
* @param inputFiles The list of files that should be expanded.
* @returns The list of input files with expanded directories.
*/
function expandInputFiles(
logger: Logger,
entryPoints: string[],
options: Options
): string[] {
const files: string[] = [];
const exclude = createMinimatch(options.getValue("exclude"));
const compilerOptions = options.getCompilerOptions();
const supportedFileRegex =
compilerOptions.allowJs || compilerOptions.checkJs
? /\.([cm][tj]s|[tj]sx?)$/
: /\.([cm]ts|tsx?)$/;
function add(file: string, entryPoint: boolean) {
let stats: FS.Stats;
try {
stats = FS.statSync(file);
} catch {
// No permission or a symbolic link, do not resolve.
return;
}
const fileIsDir = stats.isDirectory();
if (fileIsDir && !file.endsWith("/")) {
file = `${file}/`;
}
if (fileIsDir) {
FS.readdirSync(file).forEach((next) => {
add(join(file, next), false);
});
} else if (supportedFileRegex.test(file)) {
if (!entryPoint && matchesAny(exclude, file)) {
return;
}
files.push(normalizePath(file));
}
}
entryPoints.forEach((file) => {
const resolved = resolve(file);
if (!FS.existsSync(resolved)) {
logger.warn(
`Provided entry point ${file} does not exist and will not be included in the docs.`
);
return;
}
add(resolved, true);
});
return files;
}
function deriveRootDir(packageGlobPaths: string[]): string {
const globs = createMinimatch(packageGlobPaths);
const rootPaths = globs.flatMap((glob) =>
filterMap(glob.set, (set) => {
const stop = set.findIndex((part) => typeof part !== "string");
const path = stop === -1 ? set : set.slice(0, stop);
return `/${path.join("/")}`;
})
);
return getCommonDirectory(rootPaths);
}
/**
* Expand the provided packages configuration paths, determining the entry points
* and creating the ts.Programs for any which are found.
* @param logger
* @param packageGlobPaths
* @returns The information about the discovered programs, undefined if an error occurs.
*/
function getEntryPointsForPackages(
logger: Logger,
packageGlobPaths: string[],
options: Options
): DocumentationEntryPoint[] | undefined {
const results: DocumentationEntryPoint[] = [];
const exclude = createMinimatch(options.getValue("exclude"));
const rootDir = deriveRootDir(packageGlobPaths);
// packages arguments are workspace tree roots, or glob patterns
// This expands them to leave only leaf packages
const expandedPackages = expandPackages(
logger,
rootDir,
packageGlobPaths,
exclude
);
for (const packagePath of expandedPackages) {
const packageJsonPath = resolve(packagePath, "package.json");
const packageJson = loadPackageManifest(logger, packageJsonPath);
const includeVersion = options.getValue("includeVersion");
const typedocPackageConfig = packageJson
? extractTypedocConfigFromPackageManifest(logger, packageJsonPath)
: undefined;
if (packageJson === undefined) {
logger.error(`Could not load package manifest ${packageJsonPath}`);
return;
}
const packageEntryPoint = getTsEntryPointForPackage(
logger,
packageJsonPath,
packageJson
);
if (packageEntryPoint === undefined) {
logger.error(
`Could not determine TS entry point for package ${packageJsonPath}`
);
return;
}
if (packageEntryPoint === ignorePackage) {
continue;
}
const tsconfigFile = ts.findConfigFile(
packageEntryPoint,
ts.sys.fileExists,
typedocPackageConfig?.tsconfig
);
if (tsconfigFile === undefined) {
logger.error(
`Could not determine tsconfig.json for source file ${packageEntryPoint} (it must be on an ancestor path)`
);
return;
}
// Consider deduplicating this with similar code in src/lib/utils/options/readers/tsconfig.ts
let fatalError = false;
const parsedCommandLine = ts.getParsedCommandLineOfConfigFile(
tsconfigFile,
{},
{
...ts.sys,
onUnRecoverableConfigFileDiagnostic: (error) => {
logger.diagnostic(error);
fatalError = true;
},
}
);
if (!parsedCommandLine) {
return;
}
logger.diagnostics(parsedCommandLine.errors);
if (fatalError) {
return;
}
const program = ts.createProgram({
rootNames: parsedCommandLine.fileNames,
options: options.fixCompilerOptions(parsedCommandLine.options),
projectReferences: parsedCommandLine.projectReferences,
});
const sourceFile = program.getSourceFile(packageEntryPoint);
if (sourceFile === undefined) {
logger.error(
`Entry point "${packageEntryPoint}" does not appear to be built by/included in the tsconfig found at "${tsconfigFile}"`
);
return;
}
if (includeVersion && !validate({ version: String }, packageJson)) {
logger.warn(
`--includeVersion was specified, but "${nicePath(
packageJsonPath
)}" does not properly specify a version.`
);
}
results.push({
displayName:
typedocPackageConfig?.displayName ??
(packageJson["name"] as string),
version: includeVersion
? (packageJson["version"] as string | undefined)
: void 0,
readmeFile: discoverReadmeFile(
logger,
Path.join(packageJsonPath, ".."),
typedocPackageConfig?.readmeFile
),
program,
sourceFile,
});
}
return results;
}
function discoverReadmeFile(
logger: Logger,
packageDir: string,
userReadme: string | undefined
): string | undefined {
if (userReadme) {
if (!FS.existsSync(Path.join(packageDir, userReadme))) {
logger.warn(
`Failed to find ${userReadme} in ${nicePath(packageDir)}`
);
return;
}
return Path.resolve(Path.join(packageDir, userReadme));
}
for (const file of FS.readdirSync(packageDir)) {
if (file.toLowerCase() === "readme.md") {
return Path.resolve(Path.join(packageDir, file));
}
}
}