Skip to content

Commit

Permalink
Prevent unnecessary outer directory searches (#1082)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
spiltcoffee authored and aciccarello committed Jul 29, 2019
1 parent 73a13dd commit 8ace3f5
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/lib/converter/plugins/PackagePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,31 @@ export class PackagePlugin extends ConverterComponent {
* @param node The node that is currently processed if available.
*/
private onBeginDocument(context: Context, reflection: Reflection, node?: ts.SourceFile) {
let packageAndReadmeFound = () => (this.noReadmeFile || this.readmeFile) && this.packageFile;
let reachedTopDirectory = dirName => dirName === Path.resolve(Path.join(dirName, '..'));
let visitedDirBefore = dirName => this.visited.includes(dirName);

if (!node) {
return;
}
if (this.readmeFile && this.packageFile) {
return;
}

const fileName = node.fileName;
let dirName: string, parentDir = Path.resolve(Path.dirname(fileName));
do {
dirName = parentDir;
if (this.visited.includes(dirName)) {
break;
}

let dirName = Path.resolve(Path.dirname(fileName));
while (!packageAndReadmeFound() && !reachedTopDirectory(dirName) && !visitedDirBefore(dirName)) {
FS.readdirSync(dirName).forEach((file) => {
const lfile = file.toLowerCase();
if (!this.noReadmeFile && !this.readmeFile && lfile === 'readme.md') {
const lowercaseFileName = file.toLowerCase();
if (!this.noReadmeFile && !this.readmeFile && lowercaseFileName === 'readme.md') {
this.readmeFile = Path.join(dirName, file);
}

if (!this.packageFile && lfile === 'package.json') {
if (!this.packageFile && lowercaseFileName === 'package.json') {
this.packageFile = Path.join(dirName, file);
}
});

this.visited.push(dirName);
parentDir = Path.resolve(Path.join(dirName, '..'));
} while (dirName !== parentDir);
dirName = Path.resolve(Path.join(dirName, '..'));
}
}

/**
Expand Down

0 comments on commit 8ace3f5

Please sign in to comment.