codebaseHolding

tcmts.registry.test.codebaseHolding · #gr4eybp4 · term · runs on the server

uses node:path

A codebase of its own, in a directory of its own, holding code (a scratch file's text); and ns's namespace hash.

import { Codebase } from "#codebase/tcmts/cli/codebase";
import { releaseNamespace } from "#codebase/tcmts/cli/libraries";
import { save } from "#codebase/tcmts/cli/main";
import { tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import fs from "node:fs";
import path_1 from "node:path";

export function codebaseHolding(
  name: string,
  code: string,
  ns: string,
): { cb: Codebase; dir: string; namespace: () => Promise<string> } {
  const dir = tempDir(`registry ${name}`);
  const session = process.env.TCM_BRANCH;
  delete process.env.TCM_BRANCH;
  try {
    const cb = Codebase.init(dir);
    const file = path_1.join(dir, "scratch.ts");
    fs.writeFileSync(file, code);
    save(cb, file, "add");
    // (as a release names it: the namespace in the head's history entry)
    const namespace = () =>
      releaseNamespace(cb.store, {
        package: ns,
        version: "now",
        namespace: ns,
        causal: cb.store.head(),
      });
    return { cb, dir, namespace };
  } finally {
    if (session !== undefined) process.env.TCM_BRANCH = session;
  }
}

uses

used by