feat: add config decorator bate
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
bdecc96fcf
commit
9f64f89222
@ -3,4 +3,5 @@ export const METADATA_KEY = {
|
|||||||
cmd: "@ms/plugin:cmd",
|
cmd: "@ms/plugin:cmd",
|
||||||
tab: "@ms/plugin:tab",
|
tab: "@ms/plugin:tab",
|
||||||
listener: "@ms/plugin:listener",
|
listener: "@ms/plugin:listener",
|
||||||
|
config: "@ms/plugin:config",
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { injectable, decorate } from "@ms/container";
|
import { injectable, decorate } from "@ms/container";
|
||||||
import { interfaces } from './interfaces'
|
import { interfaces } from './interfaces'
|
||||||
import { METADATA_KEY } from './constants'
|
import { METADATA_KEY } from './constants'
|
||||||
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata } from './utils'
|
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata, getPluginConfigMetadata } from './utils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MiaoScript plugin
|
* MiaoScript plugin
|
||||||
@ -24,6 +24,7 @@ export function plugin(metadata: interfaces.PluginMetadata) {
|
|||||||
*/
|
*/
|
||||||
export function cmd(metadata: interfaces.CommandMetadata = {}) {
|
export function cmd(metadata: interfaces.CommandMetadata = {}) {
|
||||||
return function(target: any, key: string, value: any) {
|
return function(target: any, key: string, value: any) {
|
||||||
|
checkFunction("command", target[key]);
|
||||||
metadata.name = metadata.name || key;
|
metadata.name = metadata.name || key;
|
||||||
metadata.executor = key;
|
metadata.executor = key;
|
||||||
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, 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 = {}) {
|
export function tab(metadata: interfaces.TabCompleterMetadata = {}) {
|
||||||
return function(target: any, key: string, value: any) {
|
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);
|
metadata.name = metadata.name || (key.startsWith('tab') ? key.split('tab', 2)[1] : key);
|
||||||
if (!metadata.name) { return; }
|
if (!metadata.name) { return; }
|
||||||
metadata.executor = key;
|
metadata.executor = key;
|
||||||
@ -55,9 +57,28 @@ export function tab(metadata: interfaces.TabCompleterMetadata = {}) {
|
|||||||
*/
|
*/
|
||||||
export function listener(metadata: interfaces.ListenerMetadata = {}) {
|
export function listener(metadata: interfaces.ListenerMetadata = {}) {
|
||||||
return function(target: any, key: string, value: any) {
|
return function(target: any, key: string, value: any) {
|
||||||
|
checkFunction("listener", target[key]);
|
||||||
metadata.name = metadata.name || key;
|
metadata.name = metadata.name || key;
|
||||||
metadata.executor = key;
|
metadata.executor = key;
|
||||||
const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target)
|
const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target)
|
||||||
Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...previousMetadata], target.constructor);
|
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}!`)
|
||||||
|
}
|
||||||
|
}
|
@ -60,5 +60,8 @@ export namespace interfaces {
|
|||||||
}
|
}
|
||||||
export interface ListenerMetadata extends ExecMetadata {
|
export interface ListenerMetadata extends ExecMetadata {
|
||||||
}
|
}
|
||||||
|
export interface ConfigMetadata extends BaseMetadata {
|
||||||
|
version?: number;
|
||||||
|
}
|
||||||
export type PluginLike = Plugin | string;
|
export type PluginLike = Plugin | string;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import { plugin, server, command, event } from '@ms/api'
|
|||||||
import { injectable, inject, postConstruct, Container, ContainerInstance } from '@ms/container'
|
import { injectable, inject, postConstruct, Container, ContainerInstance } from '@ms/container'
|
||||||
import * as fs from '@ms/common/dist/fs'
|
import * as fs from '@ms/common/dist/fs'
|
||||||
|
|
||||||
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPlugin, getPluginTabCompleterMetadata } from './utils'
|
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPlugin, getPluginTabCompleterMetadata, getPluginConfigMetadata } from './utils'
|
||||||
import { interfaces } from './interfaces'
|
import { interfaces } from './interfaces'
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
@ -53,6 +53,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
this.logStage(plugin, "Loading")
|
this.logStage(plugin, "Loading")
|
||||||
this.runCatch(plugin, 'load')
|
this.runCatch(plugin, 'load')
|
||||||
this.runCatch(plugin, `${this.serverType}load`)
|
this.runCatch(plugin, `${this.serverType}load`)
|
||||||
|
this.loadConfig(plugin)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +111,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
var files = []
|
var files = []
|
||||||
console.info(`Scanning Plugins in ${plugin} ...`)
|
console.info(`Scanning Plugins in ${plugin} ...`)
|
||||||
this.checkUpdateFolder(plugin)
|
this.checkUpdateFolder(plugin)
|
||||||
fs.list(plugin).forEach((file: any) => files.push(file.toFile()))
|
// must check file is exist maybe is a illegal symbolic link file
|
||||||
|
fs.list(plugin).forEach((file: any) => file.toFile().exists() ? files.push(file.toFile()) : void 0)
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,6 +197,11 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private loadConfig(pluginInstance: interfaces.Plugin) {
|
||||||
|
let configs = getPluginConfigMetadata(pluginInstance);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private registryCommand(pluginInstance: interfaces.Plugin) {
|
private registryCommand(pluginInstance: interfaces.Plugin) {
|
||||||
let cmds = getPluginCommandMetadata(pluginInstance)
|
let cmds = getPluginCommandMetadata(pluginInstance)
|
||||||
let tabs = getPluginTabCompleterMetadata(pluginInstance)
|
let tabs = getPluginTabCompleterMetadata(pluginInstance)
|
||||||
|
@ -49,6 +49,14 @@ function getPluginListenerMetadata(target: any) {
|
|||||||
return listnerMetadata;
|
return listnerMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPluginConfigMetadata(target: any) {
|
||||||
|
let configMetadata: Map<string, interfaces.ConfigMetadata> = Reflect.getMetadata(
|
||||||
|
METADATA_KEY.config,
|
||||||
|
target.constructor
|
||||||
|
) || new Map<string, interfaces.ConfigMetadata>();
|
||||||
|
return configMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
getPlugin,
|
getPlugin,
|
||||||
getPlugins,
|
getPlugins,
|
||||||
@ -57,4 +65,5 @@ export {
|
|||||||
getPluginCommandMetadata,
|
getPluginCommandMetadata,
|
||||||
getPluginTabCompleterMetadata,
|
getPluginTabCompleterMetadata,
|
||||||
getPluginListenerMetadata,
|
getPluginListenerMetadata,
|
||||||
|
getPluginConfigMetadata
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user