startArriving

tcmts.cli.sync.startArriving · #4u4e4cak · function · runs on the server

uses node:fs

Start fetching what's arriving of history in the background, unless a process is already: a process of its own, detached, so it runs on after the command that started it, its output in .tcm/cache/history.log.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { historyArriving } from "#codebase/tcmts/cli/sync";
import { spawn } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

export function startArriving(cb: Codebase) {
  if (historyArriving(cb).fetcher) return;
  const log = fs.openSync(path.join(cb.lay.tcmDir, "cache", "history.log"), "w");
  // (the codebase's, whatever the caller's environment names)
  const { TCM_CODEBASE: _, TCM_BRANCH: __, ...env } = process.env;
  const child = spawn(
    process.execPath,
    [...process.execArgv, process.argv[1]!, "internal-history"],
    { cwd: cb.lay.root, env, detached: true, stdio: ["ignore", log, log] },
  );
  child.unref();
  fs.closeSync(log);
  if (child.pid) cb.store.setSetting("history.fetcher", String(child.pid));
}

uses

used by