runAddUpdate

tcmts.cli.run.runAddUpdate · #54eph2rt · term · runs on the server

uses node:fs

Runs tcm add and tcm update.

import { routeAdd, routeUpdate } from "#codebase/tcmts/cli/commands";
import { checkInto, execute, followImports, save } from "#codebase/tcmts/cli/main";
import type { Context } from "#codebase/tcmts/cli/run";
import { parse } from "@bomb.sh/router";
import fs from "node:fs";

export function runAddUpdate({ cb, rest, scratch, ...cx }: Context): number {
  const cmd = cx.cmd as "add" | "update";
  const { model } = execute(
    cmd,
    parse({ add: routeAdd, update: routeUpdate }[cmd](), { argv: rest }),
  );
  let ok = true;
  const clear = "clear" in model && !!model.clear;
  for (const f of model.files.length ? model.files : [undefined]) {
    const file = scratch(f);
    // names the code uses and doesn't import are imported, and imports nothing uses go, as `tcm patch` does:
    // imports follow the code
    followImports(cb, file, checkInto(model.into), "save");
    const saved = save(cb, file, cmd, checkInto(model.into));
    // (what it held is in the codebase now)
    if (saved && clear) fs.rmSync(file, { force: true });
    ok = saved && ok;
  }
  return ok ? 0 : 1;
}

uses

used by