writeRunner

tcmts.loader.writeRunner · #jqpytv4g · term · runs on the server

uses node:path

The module that runs a definition (calls it with the process's arguments, prints the result): tcm run's.

import { exportId } from "#codebase/tcmts/core/hash";
import { materialize, moduleFile, writeIfChanged } from "#codebase/tcmts/loader";
import type { DefSource, Layout } from "#codebase/tcmts/loader";
import path from "node:path";

export function writeRunner(hash: string, src: DefSource, lay: Layout): string {
  materialize([hash], src, lay);
  const rec = src.get(hash)!;
  const runner = path.join(lay.cacheDir, `run_${hash}.ts`);
  writeIfChanged(
    runner,
    `import { ${exportId(hash)} as main } from "./${moduleFile(rec.componentHash, src)}";
const value: unknown = main;
const result = typeof value === "function" ? await value(...process.argv.slice(2)) : value;
if (result !== undefined) console.log(result);
`,
  );
  return runner;
}

uses

used by