Skip to content

Commit

Permalink
Fix expansion of globs with a single entry point
Browse files Browse the repository at this point in the history
Resolves #2235
  • Loading branch information
Gerrit0 committed Apr 15, 2023
1 parent c54329e commit c1c129d
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Updated highlight colors for semantic links to meet WCAG AA contrast requirements, #2228.
- Type parameters are now highlighted consistently, #2230.
- Fixed semantic coloring in type and function signatures, #2227.
- Fix expansion of globs if a single entry point is provided, #2235.
- Fixed broken theme toggle if the page contained a member named "theme".

### Thanks!
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export { Application } from "./lib/application";

export { EventDispatcher, Event } from "./lib/utils/events";
export { resetReflectionID } from "./lib/models/reflections/abstract";
export { normalizePath } from "./lib/utils/fs";
/**
* All symbols documented under the Models namespace are also available in the root import.
*/
Expand Down Expand Up @@ -50,6 +49,7 @@ export {
EntryPointStrategy,
EventHooks,
MinimalSourceFile,
normalizePath,
} from "./lib/utils";

export type {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { validateDocumentation } from "./validation/documentation";
import { validateLinks } from "./validation/links";
import { ApplicationEvents } from "./application-events";
import { findTsConfigFile } from "./utils/tsconfig";
import { getCommonDirectory, glob, readFile } from "./utils/fs";
import { deriveRootDir, glob, readFile } from "./utils/fs";
import { resetReflectionID } from "./models/reflections/abstract";

// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -584,7 +584,7 @@ export class Application extends ChildableComponent<
private _merge(): ProjectReflection | undefined {
const start = Date.now();

const rootDir = getCommonDirectory(this.entryPoints);
const rootDir = deriveRootDir(this.entryPoints);
const entryPoints = this.entryPoints.flatMap((entry) =>
glob(entry, rootDir)
);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/converter/plugins/PackagePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Converter } from "../converter";
import type { Context } from "../context";
import { BindOption, EntryPointStrategy, readFile } from "../../utils";
import {
deriveRootDir,
discoverInParentDir,
discoverPackageJson,
getCommonDirectory,
} from "../../utils/fs";
import { nicePath } from "../../utils/paths";
import { MinimalSourceFile } from "../../utils/minimalSourceFile";
Expand Down Expand Up @@ -81,7 +81,7 @@ export class PackagePlugin extends ConverterComponent {
? this.entryPoints.map((d) => join(d, "package.json"))
: this.entryPoints;

const dirName = Path.resolve(getCommonDirectory(entryFiles));
const dirName = Path.resolve(deriveRootDir(entryFiles));

this.application.logger.verbose(
`Begin readme.md/package.json search at ${nicePath(dirName)}`
Expand Down
3 changes: 1 addition & 2 deletions src/lib/converter/plugins/SourcePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
import { Component, ConverterComponent } from "../components";
import { Converter } from "../converter";
import type { Context } from "../context";
import { BindOption } from "../../utils";
import { BindOption, normalizePath, getCommonDirectory } from "../../utils";
import { isNamedNode } from "../utils/nodes";
import { getCommonDirectory, normalizePath } from "../../utils/fs";
import { dirname, relative } from "path";
import { SourceReference } from "../../models";
import { gitIsInstalled, Repository } from "../utils/repository";
Expand Down
22 changes: 4 additions & 18 deletions src/lib/utils/entry-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {
ignorePackage,
loadPackageManifest,
} from "./package-manifest";
import { createMinimatch, matchesAny, nicePath } from "./paths";
import { createMinimatch, matchesAny, nicePath, normalizePath } from "./paths";
import type { Logger } from "./loggers";
import type { Options } from "./options";
import { getCommonDirectory, glob, normalizePath } from "./fs";
import { filterMap } from "./array";
import { deriveRootDir, glob } from "./fs";
import { assertNever } from "./general";

/**
Expand Down Expand Up @@ -180,8 +179,7 @@ function getEntryPointsForPaths(
options: Options,
programs = getEntryPrograms(logger, options)
): DocumentationEntryPoint[] | undefined {
const baseDir =
options.getValue("basePath") || getCommonDirectory(inputFiles);
const baseDir = options.getValue("basePath") || deriveRootDir(inputFiles);
const entryPoints: DocumentationEntryPoint[] = [];

entryLoop: for (const fileOrDir of inputFiles.map(normalizePath)) {
Expand Down Expand Up @@ -237,7 +235,7 @@ export function getExpandedEntryPointsForPaths(
}

function expandGlobs(inputFiles: string[]) {
const base = getCommonDirectory(inputFiles);
const base = deriveRootDir(inputFiles);
const result = inputFiles.flatMap((entry) =>
glob(entry, base, { includeDirectories: true, followSymlinks: true })
);
Expand Down Expand Up @@ -341,18 +339,6 @@ function expandInputFiles(
return files;
}

function deriveRootDir(packageGlobPaths: string[]): string {
const globs = createMinimatch(packageGlobPaths);
const rootPaths = globs.flatMap((glob) =>
filterMap(glob.set, (set) => {
const stop = set.findIndex((part) => typeof part !== "string");
const path = stop === -1 ? set : set.slice(0, stop);
return `/${path.join("/")}`;
})
);
return getCommonDirectory(rootPaths);
}

/**
* Expand the provided packages configuration paths, determining the entry points
* and creating the ts.Programs for any which are found.
Expand Down
24 changes: 14 additions & 10 deletions src/lib/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { promises as fsp } from "fs";
import { Minimatch } from "minimatch";
import { dirname, join, relative, resolve } from "path";
import { optional, validate } from "./validation";
import { createMinimatch, normalizePath } from "./paths";
import { filterMap } from "./array";

export function isFile(file: string) {
try {
Expand All @@ -20,6 +22,18 @@ export function isDir(path: string) {
}
}

export function deriveRootDir(globPaths: string[]): string {
const globs = createMinimatch(globPaths);
const rootPaths = globs.flatMap((glob) =>
filterMap(glob.set, (set) => {
const stop = set.findIndex((part) => typeof part !== "string");
const path = stop === -1 ? set : set.slice(0, stop);
return `/${path.join("/").replace(/^\//, "")}`;
})
);
return getCommonDirectory(rootPaths);
}

/**
* Get the longest directory path common to all files.
*/
Expand All @@ -42,16 +56,6 @@ export function getCommonDirectory(files: readonly string[]): string {
return roots[0].slice(0, i).join("/");
}

/**
* Normalize the given path.
*
* @param path The path that should be normalized.
* @returns The normalized path.
*/
export function normalizePath(path: string) {
return path.replace(/\\/g, "/");
}

/**
* Load the given file and return its contents.
*
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export {
copy,
copySync,
getCommonDirectory,
normalizePath,
readFile,
writeFile,
writeFileSync,
discoverInParentDir,
discoverPackageJson,
} from "./fs";
export { normalizePath } from "./paths";
export type { IfInternal, NeverIfInternal, Chars } from "./general";
export { assertNever } from "./general";
export { ConsoleLogger, Logger, LogLevel } from "./loggers";
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/options/readers/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ts from "typescript";

import type { Options, OptionsReader } from "../options";
import type { Logger } from "../../loggers";
import { normalizePath, isFile } from "../../fs";
import { isFile } from "../../fs";
import { ok } from "assert";
import {
additionalProperties,
Expand All @@ -13,7 +13,7 @@ import {
optional,
validate,
} from "../../validation";
import { nicePath } from "../../paths";
import { nicePath, normalizePath } from "../../paths";
import { createRequire } from "module";
import {
tsdocBlockTags,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/options/readers/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { OptionsReader } from "..";
import type { Logger } from "../../loggers";
import type { Options } from "../options";
import { ok } from "assert";
import { nicePath } from "../../paths";
import { isFile, normalizePath } from "../../fs";
import { nicePath, normalizePath } from "../../paths";
import { isFile } from "../../fs";
import { createRequire } from "module";

/**
Expand Down
11 changes: 10 additions & 1 deletion src/lib/utils/paths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Minimatch } from "minimatch";
import { isAbsolute, relative } from "path";
import { normalizePath } from "./fs";

/**
* Convert array of glob patterns to array of minimatch instances.
Expand Down Expand Up @@ -30,3 +29,13 @@ export function nicePath(absPath: string) {
}
return `./${normalizePath(relativePath)}`;
}

/**
* Normalize the given path.
*
* @param path The path that should be normalized.
* @returns The normalized path.
*/
export function normalizePath(path: string) {
return path.replace(/\\/g, "/");
}

0 comments on commit c1c129d

Please sign in to comment.