everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
65 lines (63 loc) • 2.1 kB
JavaScript
import { commandOptionSchemas } from "../contract.mjs";
import { globalRegistry } from "zod";
//#region src/cli/flags.ts
/** The flag description is whatever `.describe()` attached along the
* wrapper chain — check each layer's registry entry. */
function describeOf(schema) {
let current = schema;
while (true) {
const registered = globalRegistry.get(current)?.description;
if (registered) return registered;
const inner = current.def?.innerType;
if (!inner) break;
current = inner;
}
}
function unwrap(schema) {
let current = schema;
while (true) {
const type = current.def?.type;
if (type === "default" || type === "optional" || type === "nullable" || type === "nullish") {
const inner = current.def?.innerType;
if (!inner) break;
current = inner;
continue;
}
return current;
}
return current;
}
function toFlagName(field) {
return `--${field.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase()}`;
}
function defaultValueOf(schema) {
let current = schema;
while (true) {
const value = current.def?.defaultValue;
if (value !== void 0) return value;
const inner = current.def?.innerType;
if (!inner) break;
current = inner;
}
}
/** Flag help derived from the contract schemas (ticket #426) — the schema is
* the single flag source; there is no parallel description table. */
function flagHelpLines(descriptor) {
const schema = commandOptionSchemas[descriptor.key];
const shape = unwrap(schema).def?.shape;
if (!shape) return [];
const lines = [];
for (const [fieldName, fieldSchema] of Object.entries(shape)) {
const parts = [toFlagName(fieldName)];
const description = describeOf(fieldSchema);
if (unwrap(fieldSchema).def?.type === "boolean") parts.push("(boolean)");
if (description) parts.push(description);
const defaultValue = defaultValueOf(fieldSchema);
if (defaultValue !== void 0 && typeof defaultValue !== "function") parts.push(`default: ${String(defaultValue)}`);
lines.push(` ${parts.join(" ")}`);
}
return lines;
}
//#endregion
export { describeOf, flagHelpLines };
//# sourceMappingURL=flags.mjs.map