buildsDir

tcmts.cli.builds.buildsDir · #lyimv5pi · term · runs on the server

uses node:path

The directory tcm builds are kept in: the one TCM_BUILDS names (tcm's repository's dist/: pnpm tcm sets it), or, when the tcm running is a kept build itself (one saying what it was made from, as dist/known-good does), the directory holding it. Undefined otherwise: a tcm installed from npm keeps no builds.

import { buildRecord } from "#codebase/tcmts/cli/builds";
import fs from "node:fs";
import path from "node:path";

export function buildsDir(
  env: NodeJS.ProcessEnv = process.env,
  running: string | undefined = process.argv[1],
): string | undefined {
  if (env.TCM_BUILDS) return path.resolve(env.TCM_BUILDS);
  if (!running) return undefined;
  let bin: string;
  try {
    bin = fs.realpathSync(running);
  } catch {
    return undefined;
  }
  // <dir>/<build>/node_modules/<package>/bin/tcm.js
  const modules = path.resolve(path.dirname(bin), "..", "..");
  if (path.basename(modules) !== "node_modules") return undefined;
  const install = path.dirname(modules);
  if (!buildRecord(install)) return undefined;
  const holder = path.dirname(install);
  return path.basename(holder) === "builds" ? path.dirname(holder) : holder;
}

uses

used by