From 018b972aea5351c714b8b541453c67245ad07f4e Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Mon, 6 Jul 2020 16:07:16 +0800 Subject: [PATCH] feat: add ServerChecker Signed-off-by: MiaoWoo --- packages/api/src/interfaces/server/index.ts | 19 ++++++++++++++++++- packages/core/src/index.ts | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/api/src/interfaces/server/index.ts b/packages/api/src/interfaces/server/index.ts index dbbefc7d..7bc52dbe 100644 --- a/packages/api/src/interfaces/server/index.ts +++ b/packages/api/src/interfaces/server/index.ts @@ -1,5 +1,5 @@ import * as reflect from '@ccms/common/dist/reflect' -import { injectable } from '@ccms/container' +import { injectable, inject } from '@ccms/container' import { NativePluginManager } from './native_plugin' import { constants } from '../../constants' @@ -41,6 +41,23 @@ export namespace server { tabComplete?(sender: string | any, input: string, index?: number): string[] } @injectable() + export class ServerChecker { + @inject(ServerType) + private serverType: string + check(servers: string[]) { + // Not set servers -> allow + if (!servers || !servers.length) return true + // include !type -> deny + let denyServers = servers.filter(svr => svr.startsWith("!")) + if (denyServers.length !== 0) { + return !denyServers.includes(`!${this.serverType}`) + } else { + // only include -> allow + return servers.includes(this.serverType) + } + } + } + @injectable() export abstract class ReflectServer implements server.Server { protected pipeline: any protected rootLogger: any diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 382cac49..10acc024 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -97,6 +97,7 @@ function initialize() { let type = detectServer() console.i18n("ms.core.initialize.detect", { scope: global.scope, type }) container.bind(server.ServerType).toConstantValue(type) + container.bind(server.ServerChecker).toSelf().inSingletonScope() console.i18n("ms.core.package.initialize", { scope: global.scope, type }) require(`${global.scope}/${type}`).default(container) require(`${global.scope}/plugin`)