@ -1,7 +1,7 @@
|
||||
import { injectable, decorate } from "@ms/container";
|
||||
import { interfaces } from './interfaces'
|
||||
import { METADATA_KEY } from './constants'
|
||||
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata } from './utils'
|
||||
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata, getPluginConfigMetadata } from './utils'
|
||||
|
||||
/**
|
||||
* MiaoScript plugin
|
||||
@ -24,6 +24,7 @@ export function plugin(metadata: interfaces.PluginMetadata) {
|
||||
*/
|
||||
export function cmd(metadata: interfaces.CommandMetadata = {}) {
|
||||
return function(target: any, key: string, value: any) {
|
||||
checkFunction("command", target[key]);
|
||||
metadata.name = metadata.name || key;
|
||||
metadata.executor = key;
|
||||
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
|
||||
@ -39,6 +40,7 @@ export function cmd(metadata: interfaces.CommandMetadata = {}) {
|
||||
*/
|
||||
export function tab(metadata: interfaces.TabCompleterMetadata = {}) {
|
||||
return function(target: any, key: string, value: any) {
|
||||
checkFunction("tab", target[key]);
|
||||
metadata.name = metadata.name || (key.startsWith('tab') ? key.split('tab', 2)[1] : key);
|
||||
if (!metadata.name) { return; }
|
||||
metadata.executor = key;
|
||||
@ -55,9 +57,28 @@ export function tab(metadata: interfaces.TabCompleterMetadata = {}) {
|
||||
*/
|
||||
export function listener(metadata: interfaces.ListenerMetadata = {}) {
|
||||
return function(target: any, key: string, value: any) {
|
||||
checkFunction("listener", target[key]);
|
||||
metadata.name = metadata.name || key;
|
||||
metadata.executor = key;
|
||||
const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target)
|
||||
Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...previousMetadata], target.constructor);
|
||||
};
|
||||
}
|
||||
|
||||
export function config(metadata: interfaces.ConfigMetadata = { version: 1 }) {
|
||||
return function(target: any, key: string, value: any) {
|
||||
if (typeof value === "function") {
|
||||
throw new Error(`decorate config must config at prototype but is a ${typeof value}!`)
|
||||
}
|
||||
metadata.name = metadata.name || key;
|
||||
const previousMetadata: Map<string, interfaces.ConfigMetadata> = getPluginConfigMetadata(target)
|
||||
previousMetadata.set(metadata.name, metadata);
|
||||
Reflect.defineMetadata(METADATA_KEY.config, previousMetadata, target.constructor);
|
||||
}
|
||||
}
|
||||
|
||||
function checkFunction(type: string, value: any) {
|
||||
if (typeof value !== "function") {
|
||||
throw new Error(`decorate ${type} must config at function but is a ${typeof value}!`)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user