runOnBuild

tcmts.cli.builds.runOnBuild · #3cadaviw · term · runs on the server

uses node:path

tcm --build <build> <command…>: the command run by a kept build (resolveBuild: known-good, previous, an id, a path) rather than this tcm, as that build runs it: here, on the codebase (and branch) TCM_CODEBASE and TCM_BRANCH name. -C <dir> after it runs it in that directory, on the codebase there and its own branch, as tcm -C does, whether that build knows -C or not. Its exit status.

import { buildsDir, resolveBuild } from "#codebase/tcmts/cli/builds";
import { UsageError } from "#codebase/tcmts/cli/ui";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

export function runOnBuild(spec: string, argv: string[], cwd = process.cwd()): number {
  const bin = resolveBuild(spec, buildsDir(), cwd);
  let dir = cwd;
  let env: NodeJS.ProcessEnv = process.env;
  let args = argv;
  if (args[0] === "-C") {
    const there = args[1] && path.resolve(cwd, args[1]);
    if (!there || !fs.statSync(there, { throwIfNoEntry: false })?.isDirectory())
      throw new UsageError(
        `-C <dir>: ${args[1] ? `${args[1]} isn't a directory` : "give the directory to run in"}`,
      );
    const { TCM_CODEBASE: _, TCM_BRANCH: __, ...rest } = process.env;
    [dir, env, args] = [there, rest, args.slice(2)];
  }
  const r = spawnSync(process.execPath, [bin, ...args], { cwd: dir, env, stdio: "inherit" });
  return r.status ?? 1;
}

uses

used by