runTcm

tcmts.studio.runTcm · #i7cn2w5x · term · runs on the server

uses node:child_process

Run tcm (this process's, as its command line runs it) with args in the codebase's directory root, as you would: without the studio's own variables, or the codebase's or branch's, unless branch keeps the branch tcm dev serves (TCM_BRANCH). What it said, colours taken out, and its exit code.

import { tcmEnv } from "#codebase/tcmts/studio";
import { execFile } from "node:child_process";
import { stripVTControlCharacters } from "node:util";

export function runTcm(
  root: string,
  args: string[],
  how: { branch?: boolean } = {},
): Promise<{ code: number; text: string }> {
  const env: NodeJS.ProcessEnv = { ...tcmEnv(), NO_COLOR: "1" };
  if (!how.branch) delete env.TCM_BRANCH;
  return new Promise((resolve) =>
    execFile(
      process.execPath,
      [process.argv[1]!, ...args],
      { cwd: root, env, maxBuffer: 16 * 2 ** 20 },
      (e, stdout, stderr) =>
        resolve({
          code: e ? (typeof e.code === "number" ? e.code : 1) : 0,
          text: stripVTControlCharacters(`${stdout}${stderr}`).trim(),
        }),
    ),
  );
}

uses

used by