viewOf

tcmts.cli.views.viewOf · #vvkvim3s · term · runs on the server

uses node:fs

Where a definition reads as written, for source maps: an unsaved one in its scratch file, a stored one in its view file (written now if it isn't there yet).

import type { CodebaseCore } from "#codebase/tcmts/cli/codebase";
import { lineOf, writeView } from "#codebase/tcmts/cli/views";
import type { ProjectDef } from "#codebase/tcmts/core/analyze";
import type { DefRecord } from "#codebase/tcmts/core/types";
import fs from "node:fs";

export function viewOf(
  cb: CodebaseCore,
  rec: DefRecord | undefined,
  cache: Map<string, { file: string; line: number; inserts?: number[] } | undefined>,
) {
  if (!rec) return undefined;
  if (cache.has(rec.hash)) return cache.get(rec.hash);
  const scratch = rec as Partial<ProjectDef>;
  let out: { file: string; line: number; inserts?: number[] } | undefined;
  if (!cb.store.hasDef(rec.hash) && scratch.file && scratch.range && fs.existsSync(scratch.file)) {
    out = {
      file: scratch.file,
      line: lineOf(fs.readFileSync(scratch.file, "utf8"), scratch.range.start),
    };
  } else if (cb.store.hasDef(rec.hash)) {
    out = writeView(cb, rec.hash);
  }
  cache.set(rec.hash, out);
  return out;
}

uses

used by