feat: complate plugin system

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-10 17:22:00 +08:00
parent 634dbdb16d
commit 6ff25c7164
4 changed files with 15 additions and 18 deletions

View File

@ -28,7 +28,7 @@
},
"dependencies": {
"@ms/api": "^0.0.0",
"es6-map": "^0.1.5",
"inversify": "^5.0.1"
"@ms/common": "^0.0.0",
"@ms/container": "^0.0.0"
}
}
}

View File

@ -5,16 +5,15 @@ import { getPluginMetadatas } from './utils'
/**
* MiaoScript plugin
* @param name namespace name default is '/'
* @param middleware middleware array
* @param metadata PluginMetadata
*/
export function plugin(currentMetadata: interfaces.PluginMetadata) {
export function plugin(metadata: interfaces.PluginMetadata) {
return function(target: any) {
target.description = currentMetadata;
currentMetadata.target = target;
target.description = metadata;
metadata.target = target;
decorate(injectable(), target);
Reflect.defineMetadata(METADATA_KEY.plugin, currentMetadata, target);
Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target);
const previousMetadata: interfaces.PluginMetadata[] = getPluginMetadatas();
Reflect.defineMetadata(METADATA_KEY.plugin, [currentMetadata, ...previousMetadata], Reflect);
Reflect.defineMetadata(METADATA_KEY.plugin, [metadata, ...previousMetadata], Reflect);
};
}

View File

@ -5,12 +5,7 @@ export namespace interfaces {
@injectable()
export abstract class Plugin {
public description: PluginMetadata;
protected logger: Console;
@postConstruct()
private init() {
this.logger = global.console;
}
public logger: Console;
public load() { }
public enable() { }

View File

@ -1,4 +1,4 @@
import { plugin, server } from '@ms/api'
import { plugin, server, MiaoScriptConsole } from '@ms/api'
import { injectable, inject, postConstruct, Container } from '@ms/container'
import * as fs from '@ms/common/dist/fs'
@ -11,6 +11,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
private pluginInstance: any;
@inject(server.ServerType)
private serverType: string;
@inject(server.Console)
private Console: MiaoScriptConsole;
private pluginMap: Map<string, interfaces.Plugin>;
@ -47,7 +49,6 @@ export class PluginManagerImpl implements plugin.PluginManager {
}
private runCatch(pl: any, func: string) {
console.log(JSON.stringify(pl));
try {
pl[func].call(pl);
} catch (ex) {
@ -131,6 +132,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
this.pluginMap.set(metadata.name, container.getNamed(plugin.Plugin, metadata.name));
let pluginInstance = this.pluginMap.get(metadata.name)
pluginInstance.description = metadata;
// @ts-ignore
pluginInstance.logger = new this.Console(metadata.name);
}
}
}