searchableTexts

tcmts.cli.pages.searchableTexts · #e4thme6f · term · runs on the server

uses node:fs

Every named definition's and page's text, as this branch shows it (its text versions), made again when what it shows changes ([searchTextsKey]). A definition's is its view's: read from the branch's own file when that's there and current, rendered otherwise (a branch only read, --branch, has none: never another branch's).

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { isBytes } from "#codebase/tcmts/cli/files";
import { searchTexts, searchTextsKey } from "#codebase/tcmts/cli/pages";
import type { SearchText } from "#codebase/tcmts/cli/pages";
import { Placement } from "#codebase/tcmts/cli/placement";
import { viewFile, viewsCurrent, viewText } from "#codebase/tcmts/cli/views";
import fs from "node:fs";

export function searchableTexts(cb: Codebase): SearchText[] {
  const key = searchTextsKey(cb);
  const known = searchTexts.get(key);
  if (known) return known;
  const placement = new Placement(cb);
  const current = viewsCurrent(cb);
  const out: SearchText[] = [];
  const seen = new Set<string>();
  for (const n of cb.store.names(undefined, true)) {
    if (seen.has(n.path)) continue;
    seen.add(n.path);
    const rec = cb.store.getDef(n.hash);
    if (!rec) continue;
    if (rec.kind === "file") {
      if (!isBytes(rec))
        out.push({ path: n.path, hash: n.hash, kind: "file", text: rec.sourceText, first: 0 });
      continue;
    }
    const hashes = cb.lookup(n.path);
    const file = viewFile(cb, n.hash);
    const text =
      current && fs.existsSync(file)
        ? fs.readFileSync(file, "utf8")
        : viewText(cb, hashes, placement)?.text;
    if (text) out.push({ path: n.path, hash: n.hash, kind: rec.kind, text, first: hashes.length });
  }
  searchTexts.clear();
  searchTexts.set(key, out);
  return out;
}

uses

used by