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": { "dependencies": {
"@ms/api": "^0.0.0", "@ms/api": "^0.0.0",
"es6-map": "^0.1.5", "@ms/common": "^0.0.0",
"inversify": "^5.0.1" "@ms/container": "^0.0.0"
} }
} }

View File

@ -5,16 +5,15 @@ import { getPluginMetadatas } from './utils'
/** /**
* MiaoScript plugin * MiaoScript plugin
* @param name namespace name default is '/' * @param metadata PluginMetadata
* @param middleware middleware array
*/ */
export function plugin(currentMetadata: interfaces.PluginMetadata) { export function plugin(metadata: interfaces.PluginMetadata) {
return function(target: any) { return function(target: any) {
target.description = currentMetadata; target.description = metadata;
currentMetadata.target = target; metadata.target = target;
decorate(injectable(), target); decorate(injectable(), target);
Reflect.defineMetadata(METADATA_KEY.plugin, currentMetadata, target); Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target);
const previousMetadata: interfaces.PluginMetadata[] = getPluginMetadatas(); 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() @injectable()
export abstract class Plugin { export abstract class Plugin {
public description: PluginMetadata; public description: PluginMetadata;
protected logger: Console; public logger: Console;
@postConstruct()
private init() {
this.logger = global.console;
}
public load() { } public load() { }
public enable() { } 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 { injectable, inject, postConstruct, Container } from '@ms/container'
import * as fs from '@ms/common/dist/fs' import * as fs from '@ms/common/dist/fs'
@ -11,6 +11,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
private pluginInstance: any; private pluginInstance: any;
@inject(server.ServerType) @inject(server.ServerType)
private serverType: string; private serverType: string;
@inject(server.Console)
private Console: MiaoScriptConsole;
private pluginMap: Map<string, interfaces.Plugin>; private pluginMap: Map<string, interfaces.Plugin>;
@ -47,7 +49,6 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
private runCatch(pl: any, func: string) { private runCatch(pl: any, func: string) {
console.log(JSON.stringify(pl));
try { try {
pl[func].call(pl); pl[func].call(pl);
} catch (ex) { } catch (ex) {
@ -131,6 +132,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
this.pluginMap.set(metadata.name, container.getNamed(plugin.Plugin, metadata.name)); this.pluginMap.set(metadata.name, container.getNamed(plugin.Plugin, metadata.name));
let pluginInstance = this.pluginMap.get(metadata.name) let pluginInstance = this.pluginMap.get(metadata.name)
pluginInstance.description = metadata; pluginInstance.description = metadata;
// @ts-ignore
pluginInstance.logger = new this.Console(metadata.name);
} }
} }
} }