export interface EnvelopeError { code: string; message: string; } export interface Envelope { ok: boolean; command: string; data?: Record; error?: EnvelopeError; } export const ExitCode = { Ok: 0, ValidationFailed: 1, Usage: 2, NotImplemented: 3, Internal: 4, } as const; export type ExitCodeValue = (typeof ExitCode)[keyof typeof ExitCode]; export function okEnvelope(command: string, data: Record): Envelope { return { ok: true, command, data }; } export function failEnvelope(command: string, code: string, message: string): Envelope { return { ok: false, command, error: { code, message } }; } export function emit(envelope: Envelope): void { process.stdout.write(`${JSON.stringify(envelope, null, 2)}\n`); }