export const MAX_DESCRIBE_ENTRIES = 5; export const DEFAULT_SEED_BATCH_SIZE = 20; export type SearchInput = { query: string; }; export type DescribeEntry = { name: string; description: string; }; export type DescribeInput = { entries: DescribeEntry[]; }; export type SeedExportInput = { directory: string; batch_size?: number; }; export type SeedEntryInput = { name: string; description?: string; }; export type SeedImportInput = { directory: string; }; export type Command = | { kind: "search"; input: SearchInput } | { kind: "describe"; input: DescribeInput } | { kind: "seed-export"; input: SeedExportInput } | { kind: "seed-import"; input: SeedImportInput } | { kind: "list" } | { kind: "scan" } | { kind: "help" }; type EmptyInput = Record; export function parse(argv: string[]): Command { const [verb, ...rest] = argv; switch (verb) { case "search": return { kind: "search", input: parseJsonPayload("search", rest) }; case "describe": return parseDescribe(rest); case "seed-export": return parseSeedExport(rest); case "seed-import": return parseSeedImport(rest); case "list": parseJsonPayload("list", rest); return { kind: "list" }; case "scan": parseJsonPayload("scan", rest); return { kind: "scan" }; case "help": parseJsonPayload("help", rest); return { kind: "help" }; case "--help": case "-h": case undefined: return { kind: "help" }; default: throw new CliError("invalid_command", `unknown command \`${verb}\``); } } function parseDescribe(argv: string[]): Command { const input = parseJsonPayload("describe", argv); if (input.entries.length === 0) { throw new CliError("empty_entries", "describe requires at least one entry"); } if (input.entries.length > MAX_DESCRIBE_ENTRIES) { throw new CliError( "too_many_entries", `describe accepts at most ${MAX_DESCRIBE_ENTRIES} entries, got ${input.entries.length}`, ); } return { kind: "describe", input }; } function parseSeedExport(argv: string[]): Command { const input = parseJsonPayload("seed-export", argv); if (!input.directory?.trim()) { throw new CliError("invalid_input", "seed-export requires a non-empty directory"); } if ( input.batch_size !== undefined && (!Number.isInteger(input.batch_size) || input.batch_size <= 0) ) { throw new CliError("invalid_input", "seed-export batch_size must be a positive integer"); } return { kind: "seed-export", input: { directory: input.directory.trim(), batch_size: input.batch_size ?? DEFAULT_SEED_BATCH_SIZE, }, }; } function parseSeedImport(argv: string[]): Command { const input = parseJsonPayload("seed-import", argv); if (!input.directory?.trim()) { throw new CliError("invalid_input", "seed-import requires a non-empty directory"); } return { kind: "seed-import", input: { directory: input.directory.trim() } }; } function parseJsonPayload(command: string, argv: string[]): T { const [payload, extra] = argv; if (!payload) { throw new CliError("invalid_input", `missing JSON payload for \`${command}\``); } if (extra) { throw new CliError("invalid_command", `unexpected argument \`${extra}\``); } try { return JSON.parse(payload) as T; } catch (error) { const message = error instanceof Error ? error.message : "unknown parse failure"; throw new CliError("invalid_input", `invalid JSON payload for \`${command}\`: ${message}`); } } export class CliError extends Error { constructor( readonly code: string, message: string, ) { super(message); this.name = "CliError"; } }