UNPKG

@ones-open/node-host

Version:
215 lines (214 loc) 8.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Plugin = void 0; const path_1 = __importDefault(require("path")); const lodash_1 = require("lodash"); const logger_1 = require("../logger"); const utils_1 = require("../utils"); const config_1 = require("../config"); const vm_1 = require("./vm"); const env_1 = require("./env"); const semver_1 = require("semver"); class Plugin extends vm_1.PluginVM { constructor() { super(); const semverParsed = (0, semver_1.parse)(config_1.config.plugin.app_version); this.plugin = { Application: { ApplicationID: config_1.config.plugin.app_id, Name: config_1.config.plugin.app_name, ApplicationVersion: { Major: semverParsed?.major, Minor: semverParsed?.minor, Revision: semverParsed?.patch, }, }, InstanceID: config_1.config.plugin.instance_id, OrganizationUUID: config_1.config.plugin.organization_uuid, TeamUUID: config_1.config.plugin.team_uuid, RuntimeWorkSpacePath: config_1.config.plugin.workspace_path, Scope: config_1.config.plugin.scope, ScopeUUID: config_1.config.plugin.scope_uuid, }; this.logger = (0, logger_1.createPluginLogger)(this.plugin); } async InitVM(messageHandler) { const { code, path: packageDir } = (0, utils_1.getCodeInfo)(); const packagePath = path_1.default.join(packageDir, config_1.config.plugin.backend_node_modules); const env = this.getEnv(messageHandler, this.logger); this.run({ code, packagePath, env }); } InstanceId() { return this.plugin.InstanceID || ''; } async Enable(request) { await this.runHookMethod('Enable', request); } async SaasEnable(request, teamUUIDList) { await this.runHookMethod('Enable', request, teamUUIDList); } async OrgEnable(request, teamUUIDList) { return this.runHookMethod('OrgEnable', request, teamUUIDList); } async SaasOrgEnable(request, orgUUID, teamUUIDList) { return this.runHookMethod('OrgEnable', request, orgUUID, teamUUIDList); } async Start(request) { await this.runHookMethod('Start', request); } async Stop(request, forced) { try { await this.runHookMethod('Stop', request); } catch (err) { if (forced) { this.logger.info('Force stop, ignore the [Stop] call error'); } else { throw err; } } } async Disable(request, forced) { try { await this.runHookMethod('Disable', request); } catch (err) { if (forced) { this.logger.info(`[Disable] call error:`, err); this.logger.info(`Force Disable, ignore the [Disable] call error`); } else { throw err; } } } async SaasDisable(request, teamUUIDList, forced) { try { await this.runHookMethod('Disable', request, teamUUIDList); } catch (err) { if (forced) { this.logger.info(`[Disable] call error:`, err); this.logger.info(`Force Disable, ignore the [Disable] call error`); } else { throw err; } } } async OrgDisable(request, teamUUIDList, forced) { try { return (await this.runHookMethod('OrgDisable', request, teamUUIDList)); } catch (err) { if (forced) { this.logger.info(`[OrgDisable] call error:`, err); this.logger.info(`Force Disable, ignore the [OrgDisable] call error`); } else { throw err; } } return []; } async SaasOrgDisable(request, orgUUID, teamUUIDList, forced) { try { return (await this.runHookMethod('OrgDisable', request, orgUUID, teamUUIDList)); } catch (err) { if (forced) { this.logger.info(`[OrgDisable] call error:`, err); this.logger.info(`Force Disable, ignore the [OrgDisable] call error`); } else { throw err; } } return []; } async Install(request) { const json = (0, utils_1.getHashJSON)(); if (json.enableDebugLog) { logger_1.logger.info(`${new URLSearchParams((0, lodash_1.mapValues)(json, String))}`); } await this.runHookMethod('Install', request); } // 私有部署模式下,组织插件安装钩子函数 async OrgInstall(request, teamUUIDList, firstInstall) { await this.runHookMethod('OrgInstall', request, teamUUIDList, firstInstall); } // saas模式下,组织插件安装钩子函数 async SaasOrgInstall(request) { await this.runHookMethod('OrgInstall', request); } async Uninstall(request) { await this.runHookMethod('UnInstall', request); } async OrgUninstall(request, teamUUIDList) { await this.runHookMethod('OrgUnInstall', request, teamUUIDList); } async SaasOrgUninstall(request) { await this.runHookMethod('OrgUnInstall', request); } async Upgrade(oldPluginInfo, request) { await this.runHookMethod('Upgrade', oldPluginInfo, request); } async OrgUpgrade(oldPluginInfo, request, teamUUIDList) { await this.runHookMethod('OrgUpgrade', oldPluginInfo, request, teamUUIDList); } async SaasOrgUpgrade(oldPluginInfo, request) { await this.runHookMethod('OrgUpgrade', oldPluginInfo, request); } async runHookMethod(method, ...args) { const logVar = `Call Plugin Method:[${method}]`; try { logger_1.logger.info(logVar + ' ...'); const result = await this.runMethod(method, ...args); logger_1.logger.info(logVar + ' success!'); return result; } catch (err) { if (this.isMethodNotFoundError(err)) return; this.logger.error(logVar + ' failed:', err); throw err; // 如果是逻辑失败,则抛出错误 } } getEnv(messageHandler, logger) { // 简单类型信息拍平传入,不要以对象传入,否则需要以getSync(key)才能拿到对象内数据 const env = { ...(0, env_1.createEnv)(this.plugin, messageHandler), _Logger: logger, instanceId: this.plugin.InstanceID, applicationId: this.plugin.Application?.ApplicationID, applicationName: this.plugin.Application?.Name, teamUUID: this.plugin.TeamUUID, organizationUUID: this.plugin.OrganizationUUID, scope: this.plugin.Scope, scopeUUID: this.plugin.ScopeUUID, env: config_1.config.cli.debug_mode ? 'Development' : 'Production', openapiServiceAddress: config_1.config.cli.debug_mode ? config_1.config.platform.baseURL : config_1.config.host.openapi_service_address, serveMode: config_1.config.host.serve_mode ? config_1.config.host.serve_mode : config_1.serveModeStandalone, }; if (config_1.config.cli.debug_mode) { env.pluginWorkspacePath = config_1.config.plugin.workspace_path; env.fileInLocal = config_1.config.local.file_in_local; env.mysqlInLocal = config_1.config.local.mysql_in_local; env.mysqlHost = config_1.config.local.mysql_host; env.mysqlUserName = config_1.config.local.mysql_user_name; env.mysqlUserPassword = config_1.config.local.mysql_user_password; env.mysqlDatabaseName = config_1.config.local.mysql_database_name; } else { env.pluginWorkspacePath = this.plugin.RuntimeWorkSpacePath; //P8124 非debug_mode模式,也需要获取该路径 } return env; } } exports.Plugin = Plugin;