import { Command } from 'commander'; import { AuthStore } from '../lib/auth.js'; import { sendProfileOwnerControlRequest } from '../runtime/owner-control.js'; export interface RunPeekOptions { stdout: (line: string) => void; stderr?: (line: string) => void; } /** One-shot snapshot of the active game owner. Returns process exit code. */ export async function runPeek(opts: RunPeekOptions): Promise { const stderr = opts.stderr ?? ((s) => process.stderr.write(s)); const store = new AuthStore(); const profile = store.getActive(); if (!profile) { opts.stdout(JSON.stringify({ exit_reason: ['not_logged_in'], error: 'Not logged in' }) + '\n'); return 0; } try { const response = await sendProfileOwnerControlRequest(profile, 'snapshot'); if (!response?.ok) { throw new Error('ccl game runtime is not running. Start it first with `ccl game start`.'); } opts.stdout(JSON.stringify({ exit_reason: ['snapshot'], next_step: 'One-shot snapshot. No further events will follow from this invocation.', events: [], summary: response.summary ?? null, }) + '\n'); return 0; } catch (err: any) { stderr((err?.message ?? String(err)) + '\n'); return 1; } } export function createPeekCommand(): Command { return new Command('peek') .description('Print a one-shot snapshot from the active game owner.') .action(async () => { const code = await runPeek({ stdout: (s) => process.stdout.write(s) }); if (code !== 0) process.exit(code); }); }