2019-09-07 04:23:15 +00:00
|
|
|
import { interfaces } from './interfaces'
|
|
|
|
import { METADATA_KEY } from './constants'
|
|
|
|
import { injectable, decorate } from "inversify";
|
2019-09-20 01:09:29 +00:00
|
|
|
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata } from './utils'
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MiaoScript plugin
|
2019-09-10 09:22:00 +00:00
|
|
|
* @param metadata PluginMetadata
|
2019-09-07 04:23:15 +00:00
|
|
|
*/
|
2019-09-10 09:22:00 +00:00
|
|
|
export function plugin(metadata: interfaces.PluginMetadata) {
|
2019-09-07 04:23:15 +00:00
|
|
|
return function(target: any) {
|
2019-09-10 09:22:00 +00:00
|
|
|
target.description = metadata;
|
|
|
|
metadata.target = target;
|
2019-09-07 04:23:15 +00:00
|
|
|
decorate(injectable(), target);
|
2019-09-19 10:59:32 +00:00
|
|
|
Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target.constructor);
|
|
|
|
const previousMetadata: Map<string, interfaces.PluginMetadata> = getPluginMetadatas();
|
|
|
|
previousMetadata.set(metadata.name, metadata);
|
|
|
|
Reflect.defineMetadata(METADATA_KEY.plugin, previousMetadata, Reflect);
|
2019-09-07 04:23:15 +00:00
|
|
|
};
|
|
|
|
}
|
2019-09-19 10:59:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MiaoScript Command
|
|
|
|
* @param metadata CommandMetadata
|
|
|
|
*/
|
|
|
|
export function cmd(metadata: interfaces.CommandMetadata = {}) {
|
|
|
|
return function(target: any, key: string, value: any) {
|
|
|
|
metadata.name = metadata.name || key;
|
|
|
|
metadata.executor = key;
|
|
|
|
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
|
|
|
|
const previousMetadata: Map<string, interfaces.CommandMetadata> = getPluginCommandMetadata(target)
|
|
|
|
previousMetadata.set(metadata.name, metadata);
|
|
|
|
Reflect.defineMetadata(METADATA_KEY.cmd, previousMetadata, target.constructor);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MiaoScript Command
|
|
|
|
* @param metadata CommandMetadata
|
|
|
|
*/
|
|
|
|
export function tab(metadata: interfaces.TabCompleterMetadata = {}) {
|
|
|
|
return function(target: any, key: string, value: any) {
|
|
|
|
metadata.name = metadata.name || key;
|
|
|
|
metadata.executor = key;
|
|
|
|
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
|
2019-09-20 01:09:29 +00:00
|
|
|
const previousMetadata: Map<string, interfaces.TabCompleterMetadata> = getPluginTabCompleterMetadata(target)
|
2019-09-19 10:59:32 +00:00
|
|
|
previousMetadata.set(metadata.name, metadata)
|
|
|
|
Reflect.defineMetadata(METADATA_KEY.tab, previousMetadata, target.constructor);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MiaoScript Listener
|
|
|
|
* @param metadata ListenerMetadata
|
|
|
|
*/
|
|
|
|
export function listener(metadata: interfaces.ListenerMetadata = {}) {
|
|
|
|
return function(target: any, key: string, value: any) {
|
|
|
|
metadata.name = metadata.name || key;
|
|
|
|
metadata.executor = key;
|
|
|
|
const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target)
|
|
|
|
Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...previousMetadata], target.constructor);
|
|
|
|
};
|
|
|
|
}
|