Skip to content

Commit

Permalink
Support for specifying globs in entry points
Browse files Browse the repository at this point in the history
Resolves #1926.
  • Loading branch information
Gerrit0 committed Jun 4, 2022
1 parent f57c812 commit 9029777
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -41,6 +41,7 @@ These TODOs will be resolved before a full release. ([GitHub project](https://gi

- TypeDoc now supports the `@group` tag to group reflections in a page. If no `@group` tag is specified, reflections will be grouped according to their kind, #1652.
- TypeDoc will now search for `typedoc.js(on)` in the `.config` folder in the current working directory.
- Entry point strategies `Resolve` and `Expand` may now specify globs, #1926.
- `typedoc.json` now supports comments like `tsconfig.json`.
- TypeDoc will now read the `blockTags`, `inlineTags`, and `modifierTags` out of `tsdoc.json` in the same directory as `tsconfig.json` if it exists.
It is recommended to add `"extends": ["typedoc/tsdoc.json"]`, which defines TypeDoc specific tags to your `tsdoc.json` if you create one.
Expand Down
18 changes: 15 additions & 3 deletions src/lib/utils/entry-point.ts
Expand Up @@ -10,7 +10,7 @@ import {
import { createMinimatch, matchesAny } from "./paths";
import type { Logger } from "./loggers";
import type { Options } from "./options";
import { getCommonDirectory, normalizePath } from "./fs";
import { getCommonDirectory, glob, normalizePath } from "./fs";

/**
* Defines how entry points are interpreted.
Expand Down Expand Up @@ -52,13 +52,17 @@ export function getEntryPoints(
let result: DocumentationEntryPoint[] | undefined;
switch (options.getValue("entryPointStrategy")) {
case EntryPointStrategy.Resolve:
result = getEntryPointsForPaths(logger, entryPoints, options);
result = getEntryPointsForPaths(
logger,
expandGlobs(entryPoints),
options
);
break;

case EntryPointStrategy.Expand:
result = getExpandedEntryPointsForPaths(
logger,
entryPoints,
expandGlobs(entryPoints),
options
);
break;
Expand Down Expand Up @@ -185,6 +189,14 @@ export function getExpandedEntryPointsForPaths(
);
}

function expandGlobs(inputFiles: string[]) {
const base = getCommonDirectory(inputFiles);
const result = inputFiles.flatMap((entry) =>
glob(entry, base, { includeDirectories: true })
);
return result;
}

function getEntryPrograms(logger: Logger, options: Options) {
const rootProgram = ts.createProgram({
rootNames: options.getFileNames(),
Expand Down
23 changes: 16 additions & 7 deletions src/lib/utils/fs.ts
Expand Up @@ -134,9 +134,13 @@ export function copySync(src: string, dest: string): void {
}

/**
* Simpler version of `glob.sync` that only covers our use cases, only ever matching files, and ignoring node_modules.
* Simpler version of `glob.sync` that only covers our use cases, always ignoring node_modules.
*/
export function glob(pattern: string, root: string): string[] {
export function glob(
pattern: string,
root: string,
options?: { includeDirectories?: boolean }
): string[] {
const result: string[] = [];
const mini = new Minimatch(normalizePath(pattern));
const dirs: string[][] = [normalizePath(root).split("/")];
Expand All @@ -147,6 +151,16 @@ export function glob(pattern: string, root: string): string[] {
for (const child of fs.readdirSync(dir.join("/"), {
withFileTypes: true,
})) {
if (
child.isFile() ||
(options?.includeDirectories && child.isDirectory())
) {
const childPath = [...dir, child.name].join("/");
if (mini.match(childPath)) {
result.push(childPath);
}
}

if (child.isDirectory() && child.name !== "node_modules") {
const childPath = dir.concat(child.name);
if (
Expand All @@ -156,11 +170,6 @@ export function glob(pattern: string, root: string): string[] {
) {
dirs.push(childPath);
}
} else if (child.isFile()) {
const childPath = [...dir, child.name].join("/");
if (mini.match(childPath)) {
result.push(childPath);
}
}
}
} while (dirs.length);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/options/sources/typedoc.ts
Expand Up @@ -26,7 +26,7 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
options.addDeclaration({
name: "entryPoints",
help: "The entry points of your documentation.",
type: ParameterType.PathArray,
type: ParameterType.GlobArray,
});
options.addDeclaration({
name: "entryPointStrategy",
Expand Down
38 changes: 34 additions & 4 deletions src/test/slow/entry-point.test.ts
@@ -1,18 +1,48 @@
import { tempdirProject } from "@typestrong/fs-fixture-builder";
import { deepStrictEqual as equal, ok } from "assert";
import { join } from "path";
import { Application, EntryPointStrategy, TSConfigReader } from "../..";

const root = join(__dirname, "entry-points");
const fixture = tempdirProject();
fixture.addJsonFile("tsconfig.json", {
include: ["."],
});
fixture.addFile("index.ts", "export function fromIndex() {}");
fixture.addFile("extra.ts", "export function extra() {}");

describe("Entry Points", () => {
beforeEach(() => {
fixture.write();
});

afterEach(() => {
fixture.rm();
});

const app = new Application();
const tsconfig = join(root, "tsconfig.json");
const tsconfig = join(fixture.cwd, "tsconfig.json");
app.options.addReader(new TSConfigReader());

it("Supports expanding existing paths", () => {
app.bootstrap({
tsconfig,
entryPoints: [root],
entryPoints: [fixture.cwd],
entryPointStrategy: EntryPointStrategy.Expand,
});

const entryPoints = app.getEntryPoints();
ok(entryPoints);
equal(
entryPoints.length,
2,
"There are two files, so both should be expanded"
);
});

it("Supports expanding globs in paths", () => {
app.bootstrap({
tsconfig,
entryPoints: [`${fixture.cwd}/*.ts`],
entryPointStrategy: EntryPointStrategy.Expand,
});

Expand All @@ -28,7 +58,7 @@ describe("Entry Points", () => {
it("Supports resolving directories", () => {
app.bootstrap({
tsconfig,
entryPoints: [root],
entryPoints: [fixture.cwd],
entryPointStrategy: EntryPointStrategy.Resolve,
});

Expand Down
3 changes: 0 additions & 3 deletions src/test/slow/entry-points/extra.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/test/slow/entry-points/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/test/slow/entry-points/tsconfig.json

This file was deleted.

0 comments on commit 9029777

Please sign in to comment.