codebaseOf

tcmts.cli.test.shapes.codebaseOf · #r57vpgag · term · runs on the server

uses node:path

A codebase holding only defs, each named by its own name: as much of one as code reading definitions asks of it (their store, their source, their names, where packages are).

import type { CodebaseCore } from "#codebase/tcmts/cli/codebase";
import type { DefRecord } from "#codebase/tcmts/core/types";
import os from "node:os";
import path from "node:path";

export function codebaseOf(defs: DefRecord[]): CodebaseCore {
  const byHash = new Map(defs.map((d) => [d.hash, d]));
  const members = (c: string) =>
    defs.filter((d) => d.componentHash === c).sort((a, b) => a.componentIndex - b.componentIndex);
  const pathsOf = (h: string) => {
    const d = byHash.get(h);
    return d ? [d.nameHint] : [];
  };
  const store = {
    componentMembers: members,
    getDef: (h: string) => byHash.get(h),
    pathsOf,
    getMetadata: () => undefined,
    setMetadata: () => {},
    transaction: <T>(fn: () => T) => fn(),
  };
  const source = {
    get: (h: string) => byHash.get(h),
    members,
    nameOf: (h: string) => pathsOf(h)[0],
  };
  return {
    store,
    source: () => source,
    displayRef: (ref: string) =>
      (ref.startsWith("#") ? pathsOf(ref.slice(1))[0] : undefined) ?? ref,
    lay: { cacheDir: path.join(os.tmpdir(), "tcm-no-cache") },
  } as unknown as CodebaseCore;
}

uses

used by