UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

1 lines 7.07 kB
{"version":3,"file":"parse.cjs","names":[],"sources":["../../src/cli/parse.ts"],"sourcesContent":["import type { CommandDescriptor } from \"./catalog\";\n\ntype SchemaLike = {\n _def?: {\n type?: string;\n innerType?: SchemaLike;\n shape?: Record<string, SchemaLike>;\n values?: Record<string, string> | string[];\n };\n parse: (value: unknown) => unknown;\n};\n\nfunction unwrap(schema: SchemaLike): SchemaLike {\n let current = schema;\n while (true) {\n const type = current._def?.type;\n if (type === \"default\" || type === \"optional\" || type === \"nullable\" || type === \"nullish\") {\n const inner = current._def?.innerType;\n if (!inner) break;\n current = inner;\n continue;\n }\n return current;\n }\n return current;\n}\n\nfunction isBooleanSchema(schema: SchemaLike): boolean {\n return unwrap(schema)._def?.type === \"boolean\";\n}\n\nfunction isArraySchema(schema: SchemaLike): boolean {\n return unwrap(schema)._def?.type === \"array\";\n}\n\nfunction coerceValue(raw: string, schema: SchemaLike): unknown {\n const inner = unwrap(schema);\n switch (inner._def?.type) {\n case \"boolean\":\n return raw === \"true\" || raw === \"1\" || raw === \"yes\";\n case \"number\": {\n const value = Number(raw);\n if (Number.isNaN(value)) throw new Error(`Invalid number: ${raw}`);\n return value;\n }\n case \"enum\":\n return raw;\n default:\n return raw;\n }\n}\n\nfunction toFlagName(field: string): string {\n return `--${field.replace(/([a-z0-9])([A-Z])/g, \"$1-$2\").toLowerCase()}`;\n}\n\nfunction getShape(schema: SchemaLike): Record<string, SchemaLike> {\n const inner = unwrap(schema);\n const shape = inner._def?.shape;\n if (!shape) return {};\n return shape;\n}\n\nexport function parseCommandInput(descriptor: CommandDescriptor, argv: string[]): unknown {\n const schema = (descriptor.procedure as any)[\"~orpc\"]?.inputSchema as SchemaLike | undefined;\n if (!schema) return {};\n\n const shape = getShape(schema);\n const fields = Object.entries(shape);\n const fieldByFlag = new Map<string, string>();\n const positionalFields: string[] = [];\n\n for (const [fieldName] of fields) {\n fieldByFlag.set(toFlagName(fieldName), fieldName);\n if (descriptor.meta.fields?.[fieldName]?.positional) {\n positionalFields.push(fieldName);\n }\n }\n\n const input: Record<string, unknown> = {};\n const positionals: string[] = [];\n\n for (let i = 0; i < argv.length; i += 1) {\n const token = argv[i];\n if (!token) continue;\n\n if (token.startsWith(\"--\")) {\n const [flag, inline] = token.split(\"=\", 2);\n const fieldName = fieldByFlag.get(flag);\n\n if (!fieldName && flag.startsWith(\"--no-\")) {\n const positiveFlagName = `--${flag.slice(5)}`;\n const positiveFieldName = fieldByFlag.get(positiveFlagName);\n if (!positiveFieldName) throw new Error(`Unknown flag: ${token}`);\n input[positiveFieldName] = false;\n continue;\n }\n\n if (!fieldName) throw new Error(`Unknown flag: ${token}`);\n\n const fieldSchema = shape[fieldName];\n if (isBooleanSchema(fieldSchema)) {\n input[fieldName] = inline ? coerceValue(inline, fieldSchema) : true;\n continue;\n }\n\n const next = inline ?? argv[i + 1];\n\n if (isArraySchema(fieldSchema)) {\n if (next === undefined || next.startsWith(\"--\")) {\n input[fieldName] = [];\n continue;\n }\n input[fieldName] = next\n .split(\",\")\n .map((s: string) => s.trim())\n .filter(Boolean);\n if (!inline) i += 1;\n continue;\n }\n\n if (next === undefined || next.startsWith(\"--\")) {\n throw new Error(`Missing value for ${flag}`);\n }\n input[fieldName] = coerceValue(next, fieldSchema);\n if (!inline) i += 1;\n continue;\n }\n\n positionals.push(token);\n }\n\n if (positionalFields.length > 0) {\n positionalFields.forEach((fieldName, index) => {\n const raw = positionals[index];\n if (raw !== undefined) {\n input[fieldName] = coerceValue(raw, shape[fieldName]);\n }\n });\n } else if (positionals.length > 0) {\n const candidate = fields.find(([, fieldSchema]) => !isBooleanSchema(fieldSchema));\n if (candidate) {\n const [fieldName, fieldSchema] = candidate;\n input[fieldName] = coerceValue(positionals[0], fieldSchema);\n }\n }\n\n return schema.parse(input);\n}\n"],"mappings":";;AAYA,SAAS,OAAO,QAAgC;CAC9C,IAAI,UAAU;CACd,OAAO,MAAM;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