codebaseRoot

tcmts.registry.codebaseRoot · #7mwwexfj · term · runs anywhere

Where a mirrored codebase's page starts: its mount, or, while that holds no definitions of its own and one namespace alone (as tcm's holds tcmts), that namespace. Its README and its code are shown from there.

import type { NameRow } from "#codebase/tcmts/store";

export function codebaseRoot(rows: NameRow[], mount: string): string {
  let root = mount;
  for (;;) {
    const under = rows.filter((r) => r.path.startsWith(`${root}.`));
    const rest = under.map((r) => r.path.slice(root.length + 1));
    if (!rest.length || rest.some((p) => !p.includes("."))) return root;
    const firsts = new Set(rest.map((p) => p.slice(0, p.indexOf("."))));
    if (firsts.size !== 1) return root;
    root = `${root}.${[...firsts][0]}`;
  }
}

uses

used by