@tsed/cli-core
Version:
Build your CLI with TypeScript and Decorators
36 lines (35 loc) • 1.05 kB
JavaScript
import { isArray, isClass, Type } from "@tsed/core";
function mapValue(value, { type, itemType }) {
if (!value) {
return value;
}
switch (type) {
case String:
value = String(value);
break;
case Number:
value = parseFloat(value);
break;
case Boolean:
value = value === "true";
break;
case Array:
value = String(value)
.split(",")
.map((value) => mapValue(value, { type: itemType }));
break;
}
return value;
}
export function mapCommanderArgs(args, commandArgs) {
commandArgs = commandArgs.filter((arg) => !isClass(arg)).filter((arg) => !isArray(arg));
let index = 0;
return Object.entries(args).reduce((options, [arg, { defaultValue, type, itemType }]) => {
const value = commandArgs[index] || defaultValue;
index++;
return {
...options,
[arg]: mapValue(value, { type, itemType })
};
}, {});
}