Files
zalbot/scripts/pull-specs.mjs
2026-06-20 04:20:47 +00:00

57 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
// Pull the encounter spec corpus from a Gitea git source into SPECS_DIR.
// Opt-in dev helper mirroring the Docker build-time pull (CAP-17). When
// SPECS_GIT_URL is unset it does nothing — use the bundled ./specs examples,
// or set SPECS_GIT_URL to pull the production corpus.
//
// Usage:
// SPECS_GIT_URL=https://gitea.example/you/mardonar-specs.git \
// SPECS_GIT_REF=main \
// node scripts/pull-specs.mjs [--dir ./specs]
//
// --dir overrides the target directory (defaults to SPECS_DIR env, then ./specs).
import { execFileSync } from 'node:child_process';
import { rmSync, renameSync, cpSync } from 'node:fs';
import { resolve } from 'node:path';
const url = process.env.SPECS_GIT_URL;
const ref = process.env.SPECS_GIT_REF ?? '';
const dirArgIdx = process.argv.indexOf('--dir');
const dir =
dirArgIdx >= 0 && process.argv[dirArgIdx + 1]
? process.argv[dirArgIdx + 1]
: (process.env.SPECS_DIR ?? './specs');
if (!url) {
console.error(
'pull-specs: SPECS_GIT_URL is not set — nothing to pull. ' +
'Use the bundled ./specs examples, or set SPECS_GIT_URL to pull the production corpus.',
);
process.exit(0);
}
const target = resolve(dir);
const tmp = resolve('.specs-pulled-tmp');
console.log(`pull-specs: cloning ${url}${ref ? ` @ ${ref}` : ''}${target}`);
rmSync(tmp, { recursive: true, force: true });
try {
execFileSync('git', ['clone', url, tmp], { stdio: 'inherit' });
if (ref) execFileSync('git', ['-C', tmp, 'checkout', ref], { stdio: 'inherit' });
rmSync(target, { recursive: true, force: true });
// renameSync fails with EXDEV across filesystem mounts (e.g. repo dir vs
// /tmp on different devices); fall back to a recursive copy + remove.
try {
renameSync(tmp, target);
} catch (err) {
if (err.code !== 'EXDEV') throw err;
cpSync(tmp, target, { recursive: true });
rmSync(tmp, { recursive: true, force: true });
}
console.log(`pull-specs: spec corpus updated in ${target}`);
} catch (err) {
console.error('pull-specs: failed —', err.message);
rmSync(tmp, { recursive: true, force: true });
process.exit(1);
}