watchCodebase
uses tcmts.cli.style.red, which uses node:util
Call onChange when the codebase changes (tcm update, mv, pull, ... in another shell). One command can
change names in several steps (an update, then its propagation), so act only once things have settled for a poll.
import type { Codebase } from "#codebase/tcmts/cli/codebase"; import { red } from "#codebase/tcmts/cli/style"; export function watchCodebase(cb: Codebase, onChange: () => void | Promise<void>): () => void { const headOf = () => { cb.store.forgetBranch(); return `${cb.store.branch().name}:${cb.store.logHead()}`; }; let seen = headOf(); let pending: string | undefined; let busy = false; const timer = setInterval(async () => { if (busy) return; const head = headOf(); if (head === seen) return; if (head !== pending) { pending = head; return; } seen = head; busy = true; try { await onChange(); } catch (e) { console.log(red(` ${(e as Error).message}`)); } finally { busy = false; } }, 400); return () => clearInterval(timer); }