From 2dda9bd0fcbfdf95b7a02a487439fb12ecec7367 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Thu, 7 May 2020 18:33:10 +0800 Subject: [PATCH] fix: reload & download error Signed-off-by: MiaoWoo --- package.json | 1 - packages/common/src/fs.ts | 6 ++---- packages/plugin/src/decorators.ts | 3 +-- packages/plugin/src/interfaces.ts | 12 +++++++++--- packages/plugin/src/manager.ts | 8 ++++++-- packages/plugin/src/utils.ts | 4 +++- packages/plugins/src/MiaoScriptPackageManager.ts | 15 ++++++++++----- 7 files changed, 31 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 069e2580..e155c756 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,6 @@ "watch": "lerna run watch --parallel", "build": "lerna run build --scope=\"@ccms/!(plugins)\"", "build:plugins": "lerna run build --scope=\"@ccms/plugins\"", - "publish": "yarn lerna exec \"npm publish --access=public\" --scope=\"@ccms/!(plugins|client|types)\"", "ug": "yarn upgrade-interactive --latest", "lp": "lerna publish" }, diff --git a/packages/common/src/fs.ts b/packages/common/src/fs.ts index b8f1923d..16b281e1 100644 --- a/packages/common/src/fs.ts +++ b/packages/common/src/fs.ts @@ -23,9 +23,7 @@ export function concat(...args: string[]) { * @returns {*} */ export function file(...opts: any[]): any { - if (!opts[0]) { - console.warn("文件名称不得为 undefined 或者 null !"); - } + if (!opts[0]) { throw new Error("文件名称不得为 undefined 或者 null !") } switch (opts.length) { case 1: var f = opts[0]; @@ -143,7 +141,7 @@ export function del(file) { return; } if (file.isDirectory()) { - Files.list(file.toPath()).collect(Collector.toList()).forEach(function(f) { + Files.list(file.toPath()).collect(Collector.toList()).forEach(function (f) { del(f); }) } diff --git a/packages/plugin/src/decorators.ts b/packages/plugin/src/decorators.ts index 3f10741e..6287034e 100644 --- a/packages/plugin/src/decorators.ts +++ b/packages/plugin/src/decorators.ts @@ -10,14 +10,13 @@ import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata export function plugin(metadata: interfaces.PluginMetadata) { return function (target: any) { metadata.target = target; - metadata.source = metadata.source + ''; decorate(injectable(), target); Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target); const previousMetadata: Map = getPluginMetadatas(); previousMetadata.set(metadata.name, metadata); Reflect.defineMetadata(METADATA_KEY.plugin, previousMetadata, Reflect); const previousSources: Map = getPluginSources(); - previousSources.set(metadata.source, metadata); + previousSources.set(metadata.source.toString(), metadata); Reflect.defineMetadata(METADATA_KEY.souece, previousSources, Reflect); }; } diff --git a/packages/plugin/src/interfaces.ts b/packages/plugin/src/interfaces.ts index ad5cff50..4ca8d5e8 100644 --- a/packages/plugin/src/interfaces.ts +++ b/packages/plugin/src/interfaces.ts @@ -1,5 +1,5 @@ import { server, MiaoScriptConsole, event } from "@ccms/api"; -import { injectable, inject } from "@ccms/container"; +import { injectable, inject, postConstruct } from "@ccms/container"; import { getPluginMetadata } from "./utils"; export namespace interfaces { @@ -7,11 +7,17 @@ export namespace interfaces { export abstract class Plugin { public description: PluginMetadata; public logger: Console; + @inject(server.Console) + private Console: MiaoScriptConsole; - constructor(@inject(server.Console) Console: MiaoScriptConsole) { + constructor() { this.description = getPluginMetadata(this) + } + + @postConstruct() + private initialize() { // @ts-ignore - this.logger = new Console(this.description.prefix || this.description.name) + this.logger = new this.Console(this.description.prefix || this.description.name) } public load() { } diff --git a/packages/plugin/src/manager.ts b/packages/plugin/src/manager.ts index 70a5da56..e5f70256 100644 --- a/packages/plugin/src/manager.ts +++ b/packages/plugin/src/manager.ts @@ -35,7 +35,7 @@ export class PluginManagerImpl implements plugin.PluginManager { this.pluginMap = new Map() console.i18n('ms.plugin.event.map', { count: this.EventManager.mapEventName().toFixed(0), type: this.serverType }); this.initialized = true; - this.plugnMappings = getPluginSources() + this.plugnMappings = getPluginSources(); } } @@ -62,9 +62,13 @@ export class PluginManagerImpl implements plugin.PluginManager { this.execPluginStage(plugin, stage) } + /** + * 从文件加载插件 + * @param file java.io.File + */ loadFromFile(file: string): interfaces.Plugin { this.loadPlugin(file) - let plugin = this.buildPlugin(this.plugnMappings.get(file)) + let plugin = this.buildPlugin(this.plugnMappings.get(file.toString())) this.load(plugin) this.enable(plugin) return plugin; diff --git a/packages/plugin/src/utils.ts b/packages/plugin/src/utils.ts index d56476e9..3c444583 100644 --- a/packages/plugin/src/utils.ts +++ b/packages/plugin/src/utils.ts @@ -1,6 +1,8 @@ import { interfaces } from './interfaces' import { METADATA_KEY } from './constants' +const pluginSourceCache = new Map(); + function getPlugins() { return [...getPluginMetadatas().values()].map((target) => target.target); } @@ -13,7 +15,7 @@ function getPluginSources() { let pluginSources: Map = Reflect.getMetadata( METADATA_KEY.souece, Reflect - ) || new Map(); + ) || pluginSourceCache; return pluginSources; } diff --git a/packages/plugins/src/MiaoScriptPackageManager.ts b/packages/plugins/src/MiaoScriptPackageManager.ts index cb220ff8..e3b09555 100644 --- a/packages/plugins/src/MiaoScriptPackageManager.ts +++ b/packages/plugins/src/MiaoScriptPackageManager.ts @@ -34,6 +34,7 @@ let langMap = { 'download.url': '§6插件下载地址: §b{url}', 'download.finish': '§6插件 §b{name} §a下载完毕 开始加载 ...', 'install.finish': '§6插件 §b{name} §a安装成功!', + 'update.finish': '§6插件 §b{name} §a更新成功!', } let fallbackMap = langMap @@ -56,6 +57,8 @@ export class MiaoScriptPackageManager extends interfaces.Plugin { private translate: Translate; + private subCommnadNameCache: string[]; + load() { this.translate = new Translate({ langMap, @@ -118,6 +121,7 @@ export class MiaoScriptPackageManager extends interfaces.Plugin { } cmdreload(sender: any, name: string) { + name = name || this.description.name if (this.checkPlugin(sender, name)) { this.pluginManager.reload(name); this.i18n(sender, 'plugin.reload.finish', { name }) @@ -179,13 +183,13 @@ export class MiaoScriptPackageManager extends interfaces.Plugin { update(sender: any, name: string) { if (this.checkCloudPlugin(sender, name)) { - + this.download(sender, name, true); } } @tab() tabmpm(sender: any, command: any, args: string | any[]) { - if (args.length === 1) return ['list', 'install', 'update', 'upgrade', 'reload', 'restart', 'run', 'help', 'create']; + if (args.length === 1) { return ['list', 'install', 'update', 'upgrade', 'reload', 'restart', 'run', 'help', 'create', 'deploy'] } if (args.length > 1) { switch (args[0]) { case "list": @@ -197,6 +201,7 @@ export class MiaoScriptPackageManager extends interfaces.Plugin { case "load": case "unload": case "reload": + case "deploy": return [...this.pluginManager.getPlugins().keys()]; } } @@ -211,15 +216,15 @@ export class MiaoScriptPackageManager extends interfaces.Plugin { }).async().submit(); } - download(sender: any, name: string) { + download(sender: any, name: string, update: boolean = false) { this.taskManager.create(() => { this.i18n(sender, 'download.start', { name }) this.i18n(sender, 'download.url', { url: this.packageCache[name].url }) - let pluginFile = fs.concat(this.pluginFolder, name + '.js') + let pluginFile = update ? fs.concat(this.pluginFolder, 'update', name + '.js') : fs.concat(this.pluginFolder, name + '.js') http.download(this.packageCache[name].url, pluginFile) this.i18n(sender, 'download.finish', { name }) this.pluginManager.loadFromFile(pluginFile) - this.i18n(sender, 'install.finish', { name }) + this.i18n(sender, update ? 'update.finish' : 'install.finish', { name }) }).async().submit() } }