From f66dbd52d9a5b465cafef75e950adcd00d337045 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Fri, 1 Jul 2022 20:01:19 -0600 Subject: [PATCH] Add support for filtering packages with --exclude Resolves #1959 --- CHANGELOG.md | 4 ++++ src/lib/utils/entry-point.ts | 8 +++++++- src/lib/utils/package-manifest.ts | 12 ++++++++++-- src/test/packages.test.ts | 23 ++++++++++++++++++++--- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f476d8b6..c8f7d016e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/lib/utils/entry-point.ts b/src/lib/utils/entry-point.ts index 7a7b2559b..b5b9e977a 100644 --- a/src/lib/utils/entry-point.ts +++ b/src/lib/utils/entry-point.ts @@ -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); diff --git a/src/lib/utils/package-manifest.ts b/src/lib/utils/package-manifest.ts index e19d87e47..753ad9c38 100644 --- a/src/lib/utils/package-manifest.ts +++ b/src/lib/utils/package-manifest.ts @@ -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 @@ -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 @@ -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}`); @@ -98,7 +105,8 @@ export function expandPackages( return expandPackages( logger, dirname(packageJsonPath), - packagePaths + packagePaths, + exclude ); }); }); diff --git a/src/test/packages.test.ts b/src/test/packages.test.ts index 18847691f..872d030a4 100644 --- a/src/test/packages.test.ts +++ b/src/test/packages.test.ts @@ -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; @@ -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, @@ -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)]); @@ -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)]);