mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 19:36:04 +00:00
Both sync-schemas.ts and packages/codex-plugin/build.mts were hand-rolling
the same "compare source to target, print drift, exit 1 in --check mode"
pattern with slightly different output shapes. Extract:
- walkFiles(root, filter?) — recursive relative-path lister shared with
the codex-plugin drift checker
- reportAndExit(drifted, { checkMode, label, fixCommand }) — prints a
unified ✓/✗ summary, caps drift list, exits non-zero in check mode
Both callers now produce the same output shape so contributors see the
same UI whether they're syncing schemas or the codex plugin:
✗ <label> is out of sync:
changed <path>
missing <path>
extra <path>
Run `<fix command>` and commit the result.
Verified: lint, format:check, sync-schemas:check, check:codex-plugin all
pass. Drift injection tests pass on both sync paths. Pre-commit hook still
auto-rebuilds the plugin skills/ copy on source changes and correctly
skips when only the plugin copy is edited (no clobber cycle).
79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
/**
|
|
* Shared primitives for scripts that mirror one tree/file set into another
|
|
* and need a --check mode that fails CI on drift.
|
|
*
|
|
* Used by `scripts/sync-schemas.ts` (flat file list) and
|
|
* `packages/codex-plugin/build.mts` (recursive skill directories).
|
|
*/
|
|
|
|
import { readdirSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
export type DriftKind = "changed" | "missing" | "extra";
|
|
|
|
export interface DriftEntry {
|
|
kind: DriftKind;
|
|
/** Display path, relative to the source/target roots. */
|
|
path: string;
|
|
}
|
|
|
|
/**
|
|
* Recursively list file paths under `root`, returning paths relative to
|
|
* `root`. Skips everything that isn't a regular file (symlinks, sockets).
|
|
* Optional `filter` returns true to keep the file.
|
|
*/
|
|
export function walkFiles(root: string, filter?: (relPath: string) => boolean): string[] {
|
|
const out: string[] = [];
|
|
function visit(dir: string, prefix: string) {
|
|
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
const abs = join(dir, entry.name);
|
|
const rel = prefix ? join(prefix, entry.name) : entry.name;
|
|
if (entry.isDirectory()) {
|
|
visit(abs, rel);
|
|
} else if (entry.isFile() && (!filter || filter(rel))) {
|
|
out.push(rel);
|
|
}
|
|
}
|
|
}
|
|
visit(root, "");
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Print a sync/check result and, in check mode, exit non-zero on drift.
|
|
*
|
|
* The output shape is shared across sync scripts so users learn one pattern:
|
|
* a ✓/✗ header, a per-entry list (capped), and a "run X to fix" hint.
|
|
*/
|
|
export function reportAndExit(
|
|
drifted: DriftEntry[],
|
|
options: {
|
|
checkMode: boolean;
|
|
/** What is being synced. Used in the success message. */
|
|
label: string;
|
|
/** Command the user should run to fix drift. */
|
|
fixCommand: string;
|
|
/** Max entries to print before truncating. Default 30. */
|
|
maxEntries?: number;
|
|
},
|
|
): void {
|
|
const max = options.maxEntries ?? 30;
|
|
if (drifted.length === 0) {
|
|
console.log(`\n✓ ${options.label} is up to date.`);
|
|
return;
|
|
}
|
|
if (!options.checkMode) {
|
|
console.log(`\n${options.label}: ${drifted.length} file(s) updated.`);
|
|
return;
|
|
}
|
|
console.error(`\n✗ ${options.label} is out of sync:\n`);
|
|
for (const entry of drifted.slice(0, max)) {
|
|
console.error(` ${entry.kind.padEnd(7)} ${entry.path}`);
|
|
}
|
|
if (drifted.length > max) {
|
|
console.error(` … and ${drifted.length - max} more`);
|
|
}
|
|
console.error(`\nRun \`${options.fixCommand}\` and commit the result.`);
|
|
process.exit(1);
|
|
}
|