Skip to content

Commit 27597d3

Browse files
committed
Only search for readme next to package.json
Resolves #2875
1 parent fb7d24a commit 27597d3

File tree

7 files changed

+29
-38
lines changed

7 files changed

+29
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ title: Changelog
1111
- Removed `jp` translations from `lang`, to migrate switch to `ja`.
1212
- File name references in `intentionallyNotExported` now use a package name/package relative path instead of an absolute path for matching.
1313
- The `source-order` sort ordering now considers package names / package relative paths instead of using the absolute paths to a file.
14+
- TypeDoc will only check for a project README file next to the discovered `package.json` file if `--readme` is not set
15+
this change improves handling of monorepo setups where some packages have readme files and others do not, #2875.
1416

1517
### API Breaking Changes
1618

scripts/testcase.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async function main() {
5858
if (!code) {
5959
console.log("No codeblock found");
6060
const file = `src/test/converter2/issues/gh${issue}.ts`;
61-
await exec(`code ${file}`);
61+
await exec(`code ${file} src/test/issues.c2.test.ts`);
6262
return;
6363
}
6464

src/lib/converter/plugins/PackagePlugin.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { ConverterEvents } from "../converter-events.js";
88
import type { Converter } from "../converter.js";
99
import { type GlobString, i18n, MinimalSourceFile, type NormalizedPath } from "#utils";
1010
import {
11-
discoverInParentDir,
1211
discoverPackageJson,
1312
type EntryPointStrategy,
1413
getCommonDirectory,
@@ -17,6 +16,7 @@ import {
1716
Option,
1817
readFile,
1918
} from "#node-utils";
19+
import { existsSync } from "fs";
2020

2121
/**
2222
* A handler that tries to find the package.json and readme.md files of the
@@ -82,10 +82,11 @@ export class PackagePlugin extends ConverterComponent {
8282
Path.resolve(getCommonDirectory(this.entryPoints.map(g => `${g}/`)));
8383

8484
this.application.logger.verbose(
85-
`Begin readme.md/package.json search at ${nicePath(dirName)}`,
85+
`Begin package.json search at ${nicePath(dirName)}`,
8686
);
8787

88-
this.packageJson = discoverPackageJson(dirName)?.content;
88+
const packageJson = discoverPackageJson(dirName);
89+
this.packageJson = packageJson?.content;
8990

9091
// Path will be resolved already. This is kind of ugly, but...
9192
if (this.readme.endsWith("none")) {
@@ -105,17 +106,22 @@ export class PackagePlugin extends ConverterComponent {
105106
),
106107
);
107108
}
108-
} else {
109+
} else if (packageJson) {
109110
// No readme provided, automatically find the readme
110-
const result = discoverInParentDir(
111+
const possibleReadmePaths = [
112+
"README.md",
111113
"readme.md",
112-
dirName,
113-
(content) => content,
114-
);
114+
"Readme.md",
115+
].map(name => Path.join(Path.dirname(packageJson.file), name));
116+
117+
const readmePath = possibleReadmePaths.find(path => {
118+
this.application.watchFile(path);
119+
return existsSync(path);
120+
});
115121

116-
if (result) {
117-
this.readmeFile = normalizePath(result.file);
118-
this.readmeContents = result.content;
122+
if (readmePath) {
123+
this.readmeFile = normalizePath(readmePath);
124+
this.readmeContents = readFile(readmePath);
119125
this.application.watchFile(this.readmeFile);
120126
}
121127
}

src/lib/utils/fs.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -260,32 +260,6 @@ export function hasDeclarationFileExtension(path: string) {
260260
return /\.d\.[cm]?ts$/.test(path);
261261
}
262262

263-
export function discoverInParentDir<T extends {}>(
264-
name: string,
265-
dir: string,
266-
read: (content: string) => T | undefined,
267-
): { file: string; content: T } | undefined {
268-
if (!isDir(dir)) return;
269-
270-
const reachedTopDirectory = (dirName: string) => dirName === resolve(join(dirName, ".."));
271-
272-
while (!reachedTopDirectory(dir)) {
273-
for (const file of fs.readdirSync(dir)) {
274-
if (file.toLowerCase() !== name.toLowerCase()) continue;
275-
276-
try {
277-
const content = read(readFile(join(dir, file)));
278-
if (content != null) {
279-
return { file: join(dir, file), content };
280-
}
281-
} catch {
282-
// Ignore, file didn't pass validation
283-
}
284-
}
285-
dir = resolve(join(dir, ".."));
286-
}
287-
}
288-
289263
export function discoverInParentDirExactMatch<T extends {}>(
290264
name: string,
291265
dir: string,

src/test/behavior.c2.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,4 +1396,10 @@ describe("Behavior Tests", () => {
13961396
"error: @include *#circular* resulted in a circular include:*",
13971397
);
13981398
});
1399+
1400+
it("#2875 discovers readme files directly adjacent to the package.json file", () => {
1401+
const project = convert("asConstEnum");
1402+
// Previously, would find TypeDoc's root readme
1403+
equal(project.readme, undefined);
1404+
});
13991405
});

src/test/converter2/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2+
"name": "typedoc",
3+
"private": true,
24
"type": "commonjs"
35
}

src/test/output/router.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ describe("KindRouter", () => {
178178

179179
it("Can link to the modules page", () => {
180180
const project = getProject();
181+
project.readme = [{ kind: "text", text: "Needed so we create a modules page" }];
181182
const router = new KindRouter(app);
182183
router.buildPages(project);
183184

0 commit comments

Comments
 (0)