fileContent

tcmts.cli.files.fileContent · #cmes5bfc · term · runs on the server

uses Buffer

A file definition's content as it reads now: its {@link}s naming what they reach by its current name, from ns.

import type { CodebaseCore } from "#codebase/tcmts/cli/codebase";
import { isBytes, linkedCode } from "#codebase/tcmts/cli/files";
import type { DefRecord } from "#codebase/tcmts/core/types";

export function fileContent(cb: CodebaseCore, rec: DefRecord, ns: string): Buffer {
  if (isBytes(rec)) return Buffer.from(rec.sourceText, "base64");
  let text = rec.sourceText;
  for (const s of [...rec.spans].sort((a, b) => b.start - a.start)) {
    const p = s.hash && cb.store.pathsOf(s.hash)[0];
    if (!p) continue;
    // as short as it reads from here: within the nearest namespace above that has it, if that finds it still
    let short = p;
    for (let n = ns; n; n = n.slice(0, Math.max(0, n.lastIndexOf(".")))) {
      if (p.startsWith(`${n}.`) && linkedCode(cb, ns, p.slice(n.length + 1)) === s.hash) {
        short = p.slice(n.length + 1);
        break;
      }
    }
    text = text.slice(0, s.start) + short + text.slice(s.end);
  }
  return Buffer.from(text, "utf8");
}

uses

used by