Commit 016e94bd authored by nanahira's avatar nanahira

add empty command

parent 15f13c1f
import {
CommandConfigExtended,
CommandDefinitionFun,
CommandPutConfig,
CommandPutConfigMap,
......@@ -57,19 +58,19 @@ export const All = (path: string) => UseHttpRoute({ path, method: 'all' });
export function UseCommand<D extends string>(
def: D,
config?: Command.Config,
config?: CommandConfigExtended,
): MethodDecorator;
export function UseCommand<D extends string>(
def: D,
desc: string,
config?: Command.Config,
config?: CommandConfigExtended,
): MethodDecorator;
export function UseCommand(
def: string,
...args: [Command.Config?] | [string, Command.Config?]
...args: [CommandConfigExtended?] | [string, CommandConfigExtended?]
): MethodDecorator {
const desc = typeof args[0] === 'string' ? (args.shift() as string) : '';
const config = args[0] as Command.Config;
const config = args[0] as CommandConfigExtended;
return (obj, key: string, des) => {
const putOptions: CommandPutConfig<keyof CommandPutConfigMap>[] =
Reflect.getMetadata(KoishiCommandPutDef, obj.constructor, key) ||
......
......@@ -119,10 +119,14 @@ export type DoRegisterConfig<
export interface CommandRegisterConfig<D extends string = string> {
def: D;
desc?: string;
config?: Command.Config;
config?: CommandConfigExtended;
putOptions?: CommandPutConfig<keyof CommandPutConfigMap>[];
}
export interface CommandConfigExtended extends Command.Config {
empty?: boolean;
}
export interface CommandOptionConfig {
name: string;
desc: string;
......
......@@ -269,20 +269,22 @@ export function KoishiPlugin<T = any>(
for (const commandDef of commandDefs) {
command = commandDef(command) || command;
}
if (!commandData.putOptions) {
command.action((argv: Argv, ...args: any[]) =>
this[methodKey](argv, ...args),
);
} else {
for (const _optionToRegister of commandData.putOptions) {
this._preRegisterCommandActionArg(_optionToRegister, command);
}
command.action((argv: Argv, ...args: any[]) => {
const params = commandData.putOptions.map((o) =>
this._getCommandActionArg(o, argv, args),
if (!commandData.config?.empty) {
if (!commandData.putOptions) {
command.action((argv: Argv, ...args: any[]) =>
this[methodKey](argv, ...args),
);
return this[methodKey](...params);
});
} else {
for (const _optionToRegister of commandData.putOptions) {
this._preRegisterCommandActionArg(_optionToRegister, command);
}
command.action((argv: Argv, ...args: any[]) => {
const params = commandData.putOptions.map((o) =>
this._getCommandActionArg(o, argv, args),
);
return this[methodKey](...params);
});
}
}
break;
case 'route':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment