tcmInBackground

tcmts.cli.test.e2e.harness.tcmInBackground · #tzolqjka · term · runs on the server

uses node:child_process

Run tcm in dir alongside other things (another tcm, a server it calls), as tcmIn does, kept by withChildren's keep: its exit status, and what it printed on both streams, once it has ended.

import { plainEnv, strip, tcmBin } from "#codebase/tcmts/cli/test/e2e/harness";
import { spawn } from "node:child_process";
import type { ChildProcess } from "node:child_process";

export function tcmInBackground(
  keep: <C extends ChildProcess>(c: C) => C,
  dir: string,
  ...args: string[]
): Promise<{ status: number | null; out: string }> {
  return new Promise((resolve) => {
    const c = keep(spawn(process.execPath, [tcmBin(), ...args], { cwd: dir, env: plainEnv() }));
    let out = "";
    c.stdout!.setEncoding("utf8").on("data", (d: string) => (out += d));
    c.stderr!.setEncoding("utf8").on("data", (d: string) => (out += d));
    c.on("close", (status) => resolve({ status, out: strip(out) }));
  });
}

uses

used by