Skip to content

Commit 8ace3f5

Browse files
spiltcoffeeaciccarello
authored andcommitted
Prevent unnecessary outer directory searches (#1082)
* Prevent unnecessary outer directory searches Without the added checks, every directory in a source path would be scanned even when the project files have already been found. This can cause avoidable permission errors and thus typedoc to break on out-of-the-box builds for the simplest of projects. * Stops the search when readme.md and package.json are found. * Stops the search when `--readme = none` and package.json is found. * simplify logic While this commit was taken from the pro-src/master branch, I've made some changes as appropriate: - Dropped the specs changes as it seemed unrelated to the test failures - Fixed bug in `reachedTopDirectory` where it appeared to be accidentally inversed - Fixed merge issues
1 parent 73a13dd commit 8ace3f5

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/lib/converter/plugins/PackagePlugin.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,35 +83,31 @@ export class PackagePlugin extends ConverterComponent {
8383
* @param node The node that is currently processed if available.
8484
*/
8585
private onBeginDocument(context: Context, reflection: Reflection, node?: ts.SourceFile) {
86+
let packageAndReadmeFound = () => (this.noReadmeFile || this.readmeFile) && this.packageFile;
87+
let reachedTopDirectory = dirName => dirName === Path.resolve(Path.join(dirName, '..'));
88+
let visitedDirBefore = dirName => this.visited.includes(dirName);
89+
8690
if (!node) {
8791
return;
8892
}
89-
if (this.readmeFile && this.packageFile) {
90-
return;
91-
}
9293

9394
const fileName = node.fileName;
94-
let dirName: string, parentDir = Path.resolve(Path.dirname(fileName));
95-
do {
96-
dirName = parentDir;
97-
if (this.visited.includes(dirName)) {
98-
break;
99-
}
100-
95+
let dirName = Path.resolve(Path.dirname(fileName));
96+
while (!packageAndReadmeFound() && !reachedTopDirectory(dirName) && !visitedDirBefore(dirName)) {
10197
FS.readdirSync(dirName).forEach((file) => {
102-
const lfile = file.toLowerCase();
103-
if (!this.noReadmeFile && !this.readmeFile && lfile === 'readme.md') {
98+
const lowercaseFileName = file.toLowerCase();
99+
if (!this.noReadmeFile && !this.readmeFile && lowercaseFileName === 'readme.md') {
104100
this.readmeFile = Path.join(dirName, file);
105101
}
106102

107-
if (!this.packageFile && lfile === 'package.json') {
103+
if (!this.packageFile && lowercaseFileName === 'package.json') {
108104
this.packageFile = Path.join(dirName, file);
109105
}
110106
});
111107

112108
this.visited.push(dirName);
113-
parentDir = Path.resolve(Path.join(dirName, '..'));
114-
} while (dirName !== parentDir);
109+
dirName = Path.resolve(Path.join(dirName, '..'));
110+
}
115111
}
116112

117113
/**

0 commit comments

Comments
 (0)