Skip to content

Commit

Permalink
Add support for filtering packages with --exclude
Browse files Browse the repository at this point in the history
Resolves #1959
  • Loading branch information
Gerrit0 committed Jul 2, 2022
1 parent 04da924 commit f66dbd5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@

- TypeDoc no longer ignores project references if `--entryPointStrategy Packages` is set, #1976.

### Features

- The `--exclude` option will now be respected by `--entryPointStrategy Packages` and can be used to exclude package directories, #1959.

## v0.23.3 (2022-07-01)

### Bug Fixes
Expand Down
8 changes: 7 additions & 1 deletion src/lib/utils/entry-point.ts
Expand Up @@ -308,10 +308,16 @@ function getEntryPointsForPackages(
options: Options
): DocumentationEntryPoint[] | undefined {
const results: DocumentationEntryPoint[] = [];
const exclude = createMinimatch(options.getValue("exclude"));

// packages arguments are workspace tree roots, or glob patterns
// This expands them to leave only leaf packages
const expandedPackages = expandPackages(logger, ".", packageGlobPaths);
const expandedPackages = expandPackages(
logger,
".",
packageGlobPaths,
exclude
);
for (const packagePath of expandedPackages) {
const packageJsonPath = resolve(packagePath, "package.json");
const packageJson = loadPackageManifest(logger, packageJsonPath);
Expand Down
12 changes: 10 additions & 2 deletions src/lib/utils/package-manifest.ts
Expand Up @@ -5,6 +5,8 @@ import { existsSync } from "fs";

import { readFile, glob } from "./fs";
import type { Logger } from "./loggers";
import type { IMinimatch } from "minimatch";
import { matchesAny } from "./paths";

/**
* Helper for the TS type system to understand hasOwnProperty
Expand Down Expand Up @@ -71,7 +73,8 @@ function getPackagePaths(
export function expandPackages(
logger: Logger,
packageJsonDir: string,
workspaces: string[]
workspaces: string[],
exclude: IMinimatch[]
): string[] {
// Technically npm and Yarn workspaces don't support recursive nesting,
// however we support the passing of paths to either packages or
Expand All @@ -84,6 +87,10 @@ export function expandPackages(
resolve(packageJsonDir)
);
return globbedPackageJsonPaths.flatMap((packageJsonPath) => {
if (matchesAny(exclude, dirname(packageJsonPath))) {
return [];
}

const packageJson = loadPackageManifest(logger, packageJsonPath);
if (packageJson === undefined) {
logger.error(`Failed to load ${packageJsonPath}`);
Expand All @@ -98,7 +105,8 @@ export function expandPackages(
return expandPackages(
logger,
dirname(packageJsonPath),
packagePaths
packagePaths,
exclude
);
});
});
Expand Down
23 changes: 20 additions & 3 deletions src/test/packages.test.ts
Expand Up @@ -9,6 +9,7 @@ import {

import { tempdirProject } from "@typestrong/fs-fixture-builder";
import { TestLogger } from "./TestLogger";
import { createMinimatch } from "../lib/utils/paths";

describe("Packages support", () => {
let project: ReturnType<typeof tempdirProject>;
Expand Down Expand Up @@ -73,9 +74,25 @@ describe("Packages support", () => {
});
project.addJsonFile("packages/foo/tsconfig.json", childTsconfig);

// Ign, ignored package
project.addFile("packages/ign/dist/index.js", "module.exports = 123");
project.addFile("packages/ign/index.ts", "export function ign() {}");
project.addJsonFile("packages/ign/package.json", {
name: "typedoc-multi-package-ign",
version: "1.0.0",
main: "dist/index",
typedocMain: "index.ts",
});
project.addJsonFile("packages/ign/tsconfig.json", childTsconfig);

project.write();
const logger = new TestLogger();
const packages = expandPackages(logger, project.cwd, [project.cwd]);
const packages = expandPackages(
logger,
project.cwd,
[project.cwd],
createMinimatch(["**/ign"])
);

equal(
packages,
Expand Down Expand Up @@ -134,7 +151,7 @@ describe("Packages support", () => {
project.write();

const logger = new TestLogger();
const packages = expandPackages(logger, project.cwd, [project.cwd]);
const packages = expandPackages(logger, project.cwd, [project.cwd], []);

logger.expectNoOtherMessages();
equal(packages, [normalizePath(project.cwd)]);
Expand Down Expand Up @@ -169,7 +186,7 @@ describe("Packages support", () => {
project.write();

const logger = new TestLogger();
const packages = expandPackages(logger, project.cwd, [project.cwd]);
const packages = expandPackages(logger, project.cwd, [project.cwd], []);

logger.expectNoOtherMessages();
equal(packages, [normalizePath(project.cwd)]);
Expand Down

0 comments on commit f66dbd5

Please sign in to comment.