@wocker/core
Version:
Core of the Wocker
65 lines (64 loc) • 2.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Route = void 0;
const env_1 = require("../env");
class Route {
constructor(type, method) {
this.type = type;
this.method = method;
this.args = [];
this.argsMeta = [];
this.designTypes = [];
this.commandNames = [];
this.completions = [];
const descriptor = Object.getOwnPropertyDescriptor(this.type.prototype, this.method);
if (!descriptor) {
return;
}
this.description = Reflect.getMetadata(env_1.DESCRIPTION_METADATA, descriptor.value) || "";
this.commandNames = Reflect.getMetadata(env_1.COMMAND_METADATA, descriptor.value) || [];
this.completions = Reflect.getMetadata(env_1.COMPLETION_METADATA, descriptor.value) || [];
const argsMeta = Reflect.getMetadata(env_1.ARGS_METADATA, this.type, this.method) || [], argsAliases = Reflect.getMetadata(env_1.ALIAS_METADATA, this.type, this.method) || {}, argsDescription = Reflect.getMetadata(env_1.DESCRIPTION_METADATA, this.type, method) || {}, designTypes = Reflect.getMetadata(env_1.PARAMTYPES_METADATA, this.type.prototype, this.method) || [];
this.argsMeta = argsMeta;
this.designTypes = designTypes;
for (let i = 0; i < argsMeta.length; i++) {
const argMeta = argsMeta[i];
if (argMeta.type === "param") {
this.args[argMeta.index] = {
type: argMeta.type,
name: argMeta.name,
params: {
description: argsDescription[argMeta.index] || ""
}
};
}
if (argMeta.type === "option") {
const { type, alias, description } = argMeta.params || {};
this.args[argMeta.index] = {
type: argMeta.type,
name: argMeta.name,
params: Object.assign(Object.assign({}, argMeta.params), { type: this.getArgType(argMeta.index) || type, alias: argsAliases[argMeta.index] || alias, description: argsDescription[argMeta.index] || description })
};
}
}
}
get isCommand() {
return this.commandNames.length > 0;
}
get isCompletion() {
return this.completions.length > 0;
}
getArgType(index) {
switch (this.designTypes[index]) {
case String:
return "string";
case Boolean:
return "boolean";
case Number:
return "number";
default:
return undefined;
}
}
}
exports.Route = Route;