|
| 1 | +import type { BatchStep } from '../client-types.ts'; |
| 2 | +import { readInputFromCli } from '../commands/cli-grammar.ts'; |
| 3 | +import type { CliFlags } from '../utils/command-schema.ts'; |
| 4 | +import { AppError } from '../utils/errors.ts'; |
| 5 | + |
| 6 | +type LegacyCliBatchStep = { |
| 7 | + command: string; |
| 8 | + positionals?: string[]; |
| 9 | + flags?: Record<string, unknown>; |
| 10 | + runtime?: unknown; |
| 11 | +}; |
| 12 | + |
| 13 | +export function readCliBatchStepsJson(raw: string): BatchStep[] { |
| 14 | + let parsed: unknown; |
| 15 | + try { |
| 16 | + parsed = JSON.parse(raw); |
| 17 | + } catch { |
| 18 | + throw new AppError('INVALID_ARGS', 'Batch steps must be valid JSON.'); |
| 19 | + } |
| 20 | + if (!Array.isArray(parsed) || parsed.length === 0) { |
| 21 | + throw new AppError('INVALID_ARGS', 'Batch steps must be a non-empty JSON array.'); |
| 22 | + } |
| 23 | + return normalizeCliBatchSteps(parsed); |
| 24 | +} |
| 25 | + |
| 26 | +function normalizeCliBatchSteps(steps: unknown[]): BatchStep[] { |
| 27 | + let sawLegacyStep = false; |
| 28 | + const normalized = steps.map((step, index) => { |
| 29 | + if (isStructuredBatchStep(step)) return step; |
| 30 | + const legacyStep = readLegacyCliBatchStep(step, index + 1); |
| 31 | + sawLegacyStep = true; |
| 32 | + return legacyStepToStructuredStep(legacyStep); |
| 33 | + }); |
| 34 | + if (sawLegacyStep) { |
| 35 | + process.stderr.write( |
| 36 | + 'Warning: batch steps using positionals/flags are deprecated and will be removed in the next major version. Use {"command":"...","input":{...}} steps instead.\n', |
| 37 | + ); |
| 38 | + } |
| 39 | + return normalized; |
| 40 | +} |
| 41 | + |
| 42 | +function legacyStepToStructuredStep(legacyStep: LegacyCliBatchStep): BatchStep { |
| 43 | + const input = readInputFromCli( |
| 44 | + legacyStep.command, |
| 45 | + legacyStep.positionals ?? [], |
| 46 | + cliFlagsFromBatchStep(legacyStep.flags), |
| 47 | + ); |
| 48 | + return { |
| 49 | + command: legacyStep.command, |
| 50 | + input, |
| 51 | + ...(legacyStep.runtime === undefined ? {} : { runtime: legacyStep.runtime }), |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +function isStructuredBatchStep(step: unknown): step is BatchStep { |
| 56 | + return step !== null && typeof step === 'object' && !Array.isArray(step) && 'input' in step; |
| 57 | +} |
| 58 | + |
| 59 | +function readLegacyCliBatchStep(step: unknown, stepNumber: number): LegacyCliBatchStep { |
| 60 | + if (!step || typeof step !== 'object' || Array.isArray(step)) { |
| 61 | + throw new AppError('INVALID_ARGS', `Invalid batch step ${stepNumber}.`); |
| 62 | + } |
| 63 | + const record = step as Record<string, unknown>; |
| 64 | + assertLegacyBatchStepKeys(record, stepNumber); |
| 65 | + const command = typeof record.command === 'string' ? record.command.trim().toLowerCase() : ''; |
| 66 | + if (!command) throw new AppError('INVALID_ARGS', `Batch step ${stepNumber} requires command.`); |
| 67 | + const positionals = readLegacyPositionals(record.positionals, stepNumber); |
| 68 | + const flags = readLegacyFlags(record.flags, stepNumber); |
| 69 | + return { |
| 70 | + command, |
| 71 | + ...(positionals === undefined ? {} : { positionals }), |
| 72 | + ...(flags === undefined ? {} : { flags }), |
| 73 | + ...(record.runtime === undefined ? {} : { runtime: record.runtime }), |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +function assertLegacyBatchStepKeys(record: Record<string, unknown>, stepNumber: number): void { |
| 78 | + const unknownKeys = Object.keys(record).filter( |
| 79 | + (key) => !['command', 'positionals', 'flags', 'runtime'].includes(key), |
| 80 | + ); |
| 81 | + if (unknownKeys.length > 0) { |
| 82 | + throw new AppError( |
| 83 | + 'INVALID_ARGS', |
| 84 | + `Batch step ${stepNumber} has unknown legacy field(s): ${unknownKeys.join(', ')}.`, |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function readLegacyPositionals(value: unknown, stepNumber: number): string[] | undefined { |
| 90 | + if (value === undefined) return undefined; |
| 91 | + if (!Array.isArray(value) || value.some((item) => typeof item !== 'string')) { |
| 92 | + throw new AppError( |
| 93 | + 'INVALID_ARGS', |
| 94 | + `Batch step ${stepNumber} positionals must contain only strings.`, |
| 95 | + ); |
| 96 | + } |
| 97 | + return value; |
| 98 | +} |
| 99 | + |
| 100 | +function readLegacyFlags(value: unknown, stepNumber: number): Record<string, unknown> | undefined { |
| 101 | + if (value === undefined) return undefined; |
| 102 | + if (!value || typeof value !== 'object' || Array.isArray(value)) { |
| 103 | + throw new AppError('INVALID_ARGS', `Batch step ${stepNumber} flags must be an object.`); |
| 104 | + } |
| 105 | + return value as Record<string, unknown>; |
| 106 | +} |
| 107 | + |
| 108 | +function cliFlagsFromBatchStep(flags: Record<string, unknown> | undefined): CliFlags { |
| 109 | + return { |
| 110 | + json: false, |
| 111 | + help: false, |
| 112 | + version: false, |
| 113 | + ...(flags as Partial<CliFlags> | undefined), |
| 114 | + }; |
| 115 | +} |
0 commit comments