70 lines
2.7 KiB
JavaScript
70 lines
2.7 KiB
JavaScript
// server/clerk.mjs -- the clerk boundary. Every state operation goes
|
|
// through the memory.mjs CLI; the server never touches the database
|
|
// directly. The clerk stays the one truth.
|
|
/** @typedef {import('./shapes.mjs').ClerkResult} ClerkResult */
|
|
/** @typedef {import('./shapes.mjs').User} User */
|
|
|
|
import { spawnSync } from 'node:child_process';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
const CLERK = path.join(ROOT, 'tool', 'memory.mjs');
|
|
|
|
// every spawn strips the sqlite experimental warning from stderr; the
|
|
// rest is the clerk's own voice
|
|
/** @param {string} runId @param {...string} args @returns {ClerkResult} */
|
|
export function clerk(runId, ...args) {
|
|
const res = spawnSync(process.execPath, [CLERK, '--run', runId, '--quiet', ...args], { encoding: 'utf8' });
|
|
return {
|
|
code: res.status ?? 1, // null only on a signal kill; callers only ever test against 0
|
|
out: (res.stdout || '').trim(),
|
|
err: (res.stderr || '').split('\n')
|
|
.filter((l) => !l.includes('ExperimentalWarning') && !l.includes('trace-warnings'))
|
|
.join('\n').trim(),
|
|
};
|
|
}
|
|
|
|
/** Run a clerk verb and parse its --json stdout as T.
|
|
* @template T @param {string} runId @param {...string} args @returns {T} */
|
|
export function clerkJson(runId, ...args) {
|
|
const r = clerk(runId, ...args);
|
|
if (r.code !== 0) throw new Error(r.err || r.out || `clerk ${args[0]} failed`);
|
|
try {
|
|
return JSON.parse(r.out);
|
|
} catch {
|
|
throw new Error(`clerk ${args[0]}: unparseable output: ${r.out.slice(0, 200)}`);
|
|
}
|
|
}
|
|
|
|
// a clerk call that is allowed to fail (returns the result either way)
|
|
/** @param {string} runId @param {...string} args @returns {ClerkResult} */
|
|
export function clerkTry(runId, ...args) {
|
|
return clerk(runId, ...args);
|
|
}
|
|
|
|
// commands that address no run (packages, runs, create)
|
|
/** @param {...string} args @returns {ClerkResult} */
|
|
export function clerkRaw(...args) {
|
|
const res = spawnSync(process.execPath, [CLERK, ...args], { encoding: 'utf8' });
|
|
return {
|
|
code: res.status ?? 1, // null only on a signal kill; callers only ever test against 0
|
|
out: (res.stdout || '').trim(),
|
|
err: (res.stderr || '').split('\n')
|
|
.filter((l) => !l.includes('ExperimentalWarning') && !l.includes('trace-warnings'))
|
|
.join('\n').trim(),
|
|
};
|
|
}
|
|
|
|
// machine output for run-less commands (packages, runs)
|
|
/** @template T @param {...string} args @returns {T} */
|
|
export function clerkRawJson(...args) {
|
|
const r = clerkRaw('--json', ...args);
|
|
if (r.code !== 0) throw new Error(r.err || r.out || `clerk ${args[0]} failed`);
|
|
try {
|
|
return JSON.parse(r.out);
|
|
} catch {
|
|
throw new Error(`clerk ${args[0]}: unparseable output: ${r.out.slice(0, 200)}`);
|
|
}
|
|
}
|