zz_

app/cli/lib/syncState.ts

zzmq.dev@gmail.com/zz / app / cli / lib / syncState.ts @ 655fd409 Download
Ladder Summary
  • file This module manages the incremental‑sync bookkeeping for a repository by persisting the SHA of the last GitHub commit that was pushed to the remote zz repo. It exports the `SyncState` interface, along with two utility functions: `readSyncState(stateDir)` which loads the JSON file (if present) and returns a `SyncState` object or `null`, and `writeSyncState(stateDir, lastSyncedCommit)` which creates the `.zz/sync‑state.json` file (ensuring the directory exists) and writes the given commit hash.
 // The one piece of state an incremental sync needs that nothing else
 // already tracks: which commit of the real GitHub source was last
 // pushed into the remote zz repo. `zz ladder push` writes this right
 // after a successful initial import, giving `zz ladder sync` (which
 // reads it) a real baseline from the very first run onward, without
 // needing its own separate "init sync state" step.
 import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
 import { dirname, join } from "node:path";
 
 export interface SyncState {
  lastSyncedCommit: string;
 }
 
 function syncStatePath(stateDir: string): string {
  return join(stateDir, ".zz", "sync-state.json");
 }
 
 export function readSyncState(stateDir: string): SyncState | null {
  const path = syncStatePath(stateDir);
  if (!existsSync(path)) return null;
  return JSON.parse(readFileSync(path, "utf8")) as SyncState;
 }
 
 export function writeSyncState(stateDir: string, lastSyncedCommit: string): void {
  const path = syncStatePath(stateDir);
  mkdirSync(dirname(path), { recursive: true });
  writeFileSync(path, JSON.stringify({ lastSyncedCommit } satisfies SyncState, null, 2) + "\n");
 }