fakeBuild

tcmts.cli.builds.test.fakeBuild · #rsskevz7 · term · runs on the server

uses node:path

A stand-in for a kept tcm build, for tests: <root>/<where>, recorded as pnpm verify records one (made from head, hoursAgo), whose tcm.js prints what it was asked as JSON (says, its arguments, the directory, and the TCM_CODEBASE and TCM_BRANCH it saw), exiting 1 if the first argument is fail. Its directory.

import fs from "node:fs";
import path from "node:path";

export function fakeBuild(
  root: string,
  where: string,
  head: string,
  hoursAgo = 0,
  says = where,
): string {
  const dir = path.join(root, where);
  const bin = path.join(dir, "node_modules", "tcm-ts", "bin", "tcm.js");
  fs.mkdirSync(path.dirname(bin), { recursive: true });
  fs.writeFileSync(
    path.join(dir, "node_modules", "tcm-ts", "package.json"),
    `${JSON.stringify({ name: "tcm-ts", version: "0.0.0", bin: { tcm: "bin/tcm.js" } })}\n`,
  );
  fs.writeFileSync(
    bin,
    `const args = process.argv.slice(2);
console.log(JSON.stringify({ says: ${JSON.stringify(says)}, args, cwd: process.cwd(), codebase: process.env.TCM_CODEBASE ?? null, branch: process.env.TCM_BRANCH ?? null }));
process.exitCode = args[0] === "fail" ? 1 : 0;
`,
  );
  const when = new Date(Date.now() - hoursAgo * 3_600_000);
  fs.writeFileSync(
    path.join(dir, "VERIFIED"),
    `#${head.slice(0, 8)}, verified ${when.toISOString().slice(0, 16).replace("T", " ")}\n`,
  );
  fs.writeFileSync(path.join(dir, "verified.json"), `${JSON.stringify({ head })}\n`);
  for (const f of ["VERIFIED", "verified.json"]) fs.utimesSync(path.join(dir, f), when, when);
  return dir;
}

used by