@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
158 lines • 4.49 kB
JavaScript
import { z } from 'zod';
function parseEffect(def, _options, _currentOption) {
return def.schema._def;
}
function parseIntersection(def, _options, _currentOption) {
if (def.left._def.typeName !== z.ZodFirstPartyTypeKind.ZodAny) {
return def.left._def;
}
if (def.right._def.typeName !== z.ZodFirstPartyTypeKind.ZodAny) {
return def.right._def;
}
return;
}
function parseObject(def, options, _currentOption) {
const properties = def.shape();
for (const key in properties) {
const property = properties[key];
const option = {
name: key,
long: key,
short: property._def.alias,
required: true,
type: 'string'
};
parseDef(property._def, options, option);
options.push(option);
}
return;
}
function parseString(_def, _options, currentOption) {
if (currentOption) {
currentOption.type = 'string';
}
return;
}
function parseNumber(_def, _options, currentOption) {
if (currentOption) {
currentOption.type = 'number';
}
return;
}
function parseBoolean(_def, _options, currentOption) {
if (currentOption) {
currentOption.type = 'boolean';
}
return;
}
function parseOptional(def, _options, currentOption) {
if (currentOption) {
currentOption.required = false;
}
return def.innerType._def;
}
function parseDefault(def, _options, currentOption) {
if (currentOption) {
currentOption.required = false;
}
return def.innerType._def;
}
function parseEnum(def, _options, currentOption) {
if (currentOption) {
currentOption.type = 'string';
currentOption.autocomplete = [...def.values];
}
return;
}
function parseNativeEnum(def, _options, currentOption) {
if (currentOption) {
currentOption.type = 'string';
currentOption.autocomplete = Object.values(def.values).map(v => String(v));
}
return;
}
function getParseFn(typeName) {
switch (typeName) {
case z.ZodFirstPartyTypeKind.ZodEffects:
return parseEffect;
case z.ZodFirstPartyTypeKind.ZodObject:
return parseObject;
case z.ZodFirstPartyTypeKind.ZodOptional:
return parseOptional;
case z.ZodFirstPartyTypeKind.ZodString:
return parseString;
case z.ZodFirstPartyTypeKind.ZodNumber:
return parseNumber;
case z.ZodFirstPartyTypeKind.ZodBoolean:
return parseBoolean;
case z.ZodFirstPartyTypeKind.ZodEnum:
return parseEnum;
case z.ZodFirstPartyTypeKind.ZodNativeEnum:
return parseNativeEnum;
case z.ZodFirstPartyTypeKind.ZodDefault:
return parseDefault;
case z.ZodFirstPartyTypeKind.ZodIntersection:
return parseIntersection;
default:
return;
}
}
function parseDef(def, options, currentOption) {
let parsedDef = def;
do {
const parse = getParseFn(parsedDef.typeName);
if (!parse) {
break;
}
parsedDef = parse(parsedDef, options, currentOption);
if (!parsedDef) {
break;
}
} while (parsedDef);
}
function optionToString(optionInfo) {
let s = '';
if (optionInfo.short) {
s += `-${optionInfo.short}, `;
}
s += `--${optionInfo.long}`;
if (optionInfo.type !== 'boolean') {
s += ' ';
s += optionInfo.required ? '<' : '[';
s += optionInfo.long;
s += optionInfo.required ? '>' : ']';
}
return s;
}
;
export const zod = {
alias(alias, type) {
type._def.alias = alias;
return type;
},
schemaToOptionInfo(schema) {
const options = [];
parseDef(schema._def, options);
return options;
},
schemaToOptions(schema) {
const optionsInfo = this.schemaToOptionInfo(schema);
const options = optionsInfo.map(option => {
return {
option: optionToString(option),
autocomplete: option.autocomplete
};
});
return options;
},
coercedEnum: (e) => z.preprocess(val => {
const target = String(val)?.toLowerCase();
for (const k of Object.values(e)) {
if (String(k)?.toLowerCase() === target) {
return k;
}
}
return null;
}, z.nativeEnum(e))
};
//# sourceMappingURL=zod.js.map