tracesFrom

tcmts.studio.tracesFrom · #hgajvzje · term · runs on the server

uses node:fs

Read traces from from: "production", its traces as last fetched; else this machine's (withTraces).

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { withTraces } from "#codebase/tcmts/cli/traced";
import { productionTraces } from "#codebase/tcmts/studio";
import fs from "node:fs";
import { DatabaseSync } from "node:sqlite";

export function tracesFrom(
  cb: Codebase,
  from: string,
): <T>(f: (db: DatabaseSync) => T) => T | undefined {
  if (from !== "production") return (f) => withTraces(cb, f);
  return (f) => {
    const file = productionTraces(cb);
    if (!fs.existsSync(file)) return undefined;
    const db = new DatabaseSync(file, { readOnly: true });
    try {
      return f(db);
    } finally {
      db.close();
    }
  };
}

uses

used by