import { randomUUID } from 'crypto'; import { ResponseClassifier } from './classifier.js'; import { ShortCommandPresenter } from './presenter.js'; import type { ResponseContract } from './response-contract.js'; import type { TransportResponse } from './transport-response.js'; import type { CommandDescriptor, CommandSource, LocalCommandError, PresentationOverrides, PresentedCommand, } from './types.js'; export type ShortCommandOperationResult = | { kind: 'backend'; response: TransportResponse; contract: ResponseContract; presentation?: PresentationOverrides; } | { /** Backend/owner adapter has already determined the business outcome. */ kind: 'backend_outcome'; status: 'success' | 'partial' | 'failure'; value: Record; response?: Pick; presentation?: PresentationOverrides; } | { /** A local/owner/transport composite operation with an explicit outcome. */ kind: 'local_outcome'; status: 'success' | 'partial' | 'failure'; source: Exclude; value: Record; reason?: string; diagnosticId?: string; presentation?: PresentationOverrides; } | { kind: 'local_success'; source: Exclude; value: Record; presentation?: PresentationOverrides; } | { kind: 'local_failure'; source: Exclude; error: LocalCommandError; reason?: string; diagnosticId?: string; presentation?: PresentationOverrides; } | { kind: 'transport_failure'; error: LocalCommandError; reason?: string; diagnosticId?: string; presentation?: PresentationOverrides; }; export interface CommandIo { writeStdout(text: string): void; setExitCode(code: number): void; } export interface ShortCommandRunOptions { descriptor: CommandDescriptor; operation: () => Promise | ShortCommandOperationResult; io: CommandIo; pretty?: boolean; } export class JsonCommandWriter { constructor(private readonly io: CommandIo, private readonly pretty = true) {} write(command: PresentedCommand): void { this.io.writeStdout(`${JSON.stringify(command.value, null, this.pretty ? 2 : 0)}\n`); this.io.setExitCode(command.exitCode); } } export interface ShortCommandRunnerOptions { classifier?: ResponseClassifier; presenter?: ShortCommandPresenter; commandIdFactory?: () => string; } export class ShortCommandRunner { private readonly classifier: ResponseClassifier; private readonly presenter: ShortCommandPresenter; private readonly commandIdFactory: () => string; constructor(options: ShortCommandRunnerOptions = {}) { this.classifier = options.classifier ?? new ResponseClassifier(); this.presenter = options.presenter ?? new ShortCommandPresenter(); this.commandIdFactory = options.commandIdFactory ?? (() => `cmd_${randomUUID()}`); } async run(options: ShortCommandRunOptions): Promise { const commandId = this.commandIdFactory(); let presented: PresentedCommand; try { const result = await options.operation(); if (result.kind === 'backend') { const classified = this.classifier.classify(result.response, result.contract); presented = this.presenter.presentBackend( classified, { commandId, descriptor: options.descriptor }, result.presentation, ); } else if (result.kind === 'backend_outcome') { presented = this.presenter.presentBackendOutcome( result.value, result.status, { commandId, descriptor: options.descriptor }, result.presentation, result.response, ); } else if (result.kind === 'local_outcome') { presented = this.presenter.presentLocalOutcome( result.value, result.status, result.source, { commandId, descriptor: options.descriptor, diagnosticId: result.diagnosticId, }, result.presentation, result.reason, ); } else if (result.kind === 'local_success') { presented = this.presenter.presentLocalSuccess( result.value, result.source, { commandId, descriptor: options.descriptor }, result.presentation, ); } else { presented = this.presenter.presentLocalFailure( result.error, result.kind === 'transport_failure' ? 'transport' : result.source, { commandId, descriptor: options.descriptor, diagnosticId: result.diagnosticId, }, result.presentation, result.reason, ); } } catch (error) { const message = error instanceof Error ? error.message : String(error); presented = this.presenter.presentLocalFailure( { code: 'COMMAND_EXECUTION_FAILED', message }, 'local', { commandId, descriptor: options.descriptor }, ); } new JsonCommandWriter(options.io, options.pretty ?? true).write(presented); return presented; } }