From 7a3f52175a5450e3637fb5dcd22d121028828f83 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Mon, 10 Oct 2022 15:02:23 -0600 Subject: [PATCH] Automatically discover readme files in packages mode Resolves #2065 --- CHANGELOG.md | 3 ++- src/lib/utils/entry-point.ts | 36 +++++++++++++++++++++++-------- src/test/slow/entry-point.test.ts | 27 ++++++++++++++++++++++- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eaeaf29aa..384ea169b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,9 @@ ### Features -- Added support for specifying the tsconfig.json file in packages mode with `{ "typedoc": { "tsconfig": "tsconfig.lib.json" }}` in package.json, #2061. - Object types will now be pretty printed, #1793. +- Added support for specifying the tsconfig.json file in packages mode with `{ "typedoc": { "tsconfig": "tsconfig.lib.json" }}` in package.json, #2061. +- In packages mode, readme files will now be automatically included if present, #2065. - Added support for specifying the base file url for links to source code, #2068. ### Bug Fixes diff --git a/src/lib/utils/entry-point.ts b/src/lib/utils/entry-point.ts index 2e32ecf91..ef528e2aa 100644 --- a/src/lib/utils/entry-point.ts +++ b/src/lib/utils/entry-point.ts @@ -422,15 +422,11 @@ function getEntryPointsForPackages( version: includeVersion ? (packageJson["version"] as string | undefined) : void 0, - readmeFile: typedocPackageConfig?.readmeFile - ? Path.resolve( - Path.join( - packageJsonPath, - "..", - typedocPackageConfig?.readmeFile - ) - ) - : undefined, + readmeFile: discoverReadmeFile( + logger, + Path.join(packageJsonPath, ".."), + typedocPackageConfig?.readmeFile + ), program, sourceFile, }); @@ -438,3 +434,25 @@ function getEntryPointsForPackages( 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)); + } + } +} diff --git a/src/test/slow/entry-point.test.ts b/src/test/slow/entry-point.test.ts index 85c6aba81..62a84b7d9 100644 --- a/src/test/slow/entry-point.test.ts +++ b/src/test/slow/entry-point.test.ts @@ -86,6 +86,7 @@ describe("Entry Points", () => { ok(entryPoints); equal(entryPoints.length, 1); equal(entryPoints[0].version, void 0); + equal(entryPoints[0].readmeFile, void 0); }); it("Supports resolving packages outside of cwd", () => { @@ -136,6 +137,30 @@ describe("Entry Points", () => { fixture.rm(); ok(entryPoints); equal(entryPoints.length, 1); - equal(entryPoints[0].version, void 0); + }); + + it("Supports automatically discovering the readme files", () => { + const fixture = tempdirProject(); + fixture.addJsonFile("tsconfig.json", { + include: ["."], + }); + fixture.addJsonFile("package.json", { + main: "index.ts", + }); + fixture.addFile("reaDME.md", "Text"); + fixture.addFile("index.ts", "export function fromIndex() {}"); + fixture.write(); + + app.bootstrap({ + tsconfig: tsconfig, + entryPoints: [fixture.cwd], + entryPointStrategy: EntryPointStrategy.Packages, + }); + + const entryPoints = app.getEntryPoints(); + fixture.rm(); + ok(entryPoints); + equal(entryPoints.length, 1); + equal(entryPoints[0].readmeFile, join(fixture.cwd, "reaDME.md")); }); });