Skip to content

Commit

Permalink
Take into account that when merging monorepos each project is a modul…
Browse files Browse the repository at this point in the history
…e that can contain submodules
  • Loading branch information
krisztianb committed Sep 21, 2024
1 parent 5e31694 commit 68238da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/merger/project_merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DeclarationReflection, DocumentReflection, ProjectReflection, Reflectio
import {
addDeclarationReflectionToTarget,
addDocumentReflectionToTarget,
getModules as getModulesFrom,
removeDeclarationReflectionFromModule,
removeDocumentReflectionFromModule,
} from "../utils";
Expand All @@ -26,18 +27,22 @@ export class ProjectMerger {
* Performs the merging routine.
*/
public execute(): void {
const modules = (this.project.children ?? []).filter((c) => c.kindOf(ReflectionKind.Module));
// In monorepo project each project is also a module => Recursively collect all modules
const allModules = getModulesFrom(this.project);

if (modules.length > 0) {
if (allModules.length > 0) {
this.clearProject();

for (const mod of modules) {
for (const module of allModules) {
// Here we create a copy because the next loop modifies the collection
const reflections = [...(mod.childrenIncludingDocuments ?? [])];
const reflections = [...(module.childrenIncludingDocuments ?? [])];

for (const ref of reflections) {
// Drop aliases (= ReflectionKind.Reference)
if (ref instanceof DeclarationReflection && !ref.kindOf(ReflectionKind.Reference)) {
// Drop aliases (= ReflectionKind.Reference) and modules
if (
ref instanceof DeclarationReflection &&
!ref.kindOf([ReflectionKind.Reference, ReflectionKind.Module])
) {
this.moveDeclarationReflectionToProject(ref);
} else if (ref instanceof DocumentReflection) {
this.moveDocumentReflectionFromToProject(ref);
Expand Down
18 changes: 17 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, DeclarationReflection, DocumentReflection, ProjectReflection } from "typedoc";
import { Context, DeclarationReflection, DocumentReflection, ProjectReflection, ReflectionKind } from "typedoc";
import * as ts from "typescript";

/**
Expand Down Expand Up @@ -124,3 +124,19 @@ export function removeDocumentReflectionFromModule(ref: DocumentReflection): voi
module.childrenIncludingDocuments?.splice(indexInChildrenIncludingDocuments, 1);
}
}

/**
* Returns the modules within the given module parent. Searches recursively.
* @param moduleParent The element in which to search for modules.
* @returns The modules within the given module parent.
*/
export function getModules(moduleParent: ProjectReflection | DeclarationReflection): DeclarationReflection[] {
const modules = (moduleParent.children ?? []).filter((c) => c.kindOf(ReflectionKind.Module));

for (const mod of modules) {
const subModules = getModules(mod);
modules.push(...subModules);
}

return modules;
}

0 comments on commit 68238da

Please sign in to comment.