codebase

tcmts.cli.test.e2e.releases.codebase · #thu6uyj2 · term · runs on the server

uses node:child_process

import { plainEnv, strip, tcmBin, tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

export function codebase(name: string) {
  const dir = tempDir(`releases ${name}`);
  const env = { ...plainEnv(), TCM_AUTHOR: "ada" };
  const run = (...args: string[]) => {
    const r = spawnSync(process.execPath, [tcmBin(), ...args], { cwd: dir, encoding: "utf8", env });
    return { status: r.status, out: strip(r.stdout + r.stderr) };
  };
  const tcm = (...args: string[]) => {
    const r = run(...args);
    if (r.status !== 0) throw new Error(`tcm ${args.join(" ")} (in ${name}) exited ${r.status}:\n${r.out}`);
    return r.out;
  };
  const save = (text: string, mode = "update") => (fs.writeFileSync(path.join(dir, "scratch.ts"), text), tcm(mode));
  tcm("init");
  fs.rmSync(path.join(dir, "scratch.ts"));
  return { dir, run, tcm, save, file: (f: string) => path.join(dir, f) };
}

uses

used by