Skip to content

Commit

Permalink
Automatically discover readme files in packages mode
Browse files Browse the repository at this point in the history
Resolves #2065
  • Loading branch information
Gerrit0 committed Oct 10, 2022
1 parent 535f3f3 commit 7a3f521
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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
Expand Down
36 changes: 27 additions & 9 deletions src/lib/utils/entry-point.ts
Expand Up @@ -422,19 +422,37 @@ 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,
});
}

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));
}
}
}
27 changes: 26 additions & 1 deletion src/test/slow/entry-point.test.ts
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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"));
});
});

0 comments on commit 7a3f521

Please sign in to comment.