@webfaas/webfaas-core
Version:
WebFaaS Framework - Core
90 lines • 2.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginManager = void 0;
const fs = require("fs");
const path = require("path");
const Core_1 = require("../Core");
const ILog_1 = require("../Log/ILog");
class PluginManager {
constructor(core) {
this.listPlugin = new Array();
this.core = core;
}
/**
* start plugins
*/
async start() {
for (let i = 0; i < this.listPlugin.length; i++) {
let plugin = this.listPlugin[i];
await plugin.startPlugin(this.core);
}
}
/**
* stop plugins
*/
async stop() {
for (let i = 0; i < this.listPlugin.length; i++) {
let plugin = this.listPlugin[i];
await plugin.stopPlugin(this.core);
}
}
/**
* add plugin
* @param plugin plugin
*/
addPlugin(plugin) {
this.listPlugin.push(plugin);
}
/**
* build instance plugin
* @param pluginClassOrFunction class or function
*/
instancePluginBuild(pluginFunctionFactory) {
let newPlugin;
if (pluginFunctionFactory.default) {
newPlugin = pluginFunctionFactory.default(this.core);
}
else {
newPlugin = pluginFunctionFactory(this.core);
}
return newPlugin;
}
/**
* load plugins by folder
*/
loadPluginsByFolder(scanFolderName) {
let file = "";
let filePath = "";
try {
let files = fs.readdirSync(scanFolderName);
for (let i = 0; i < files.length; i++) {
file = files[i];
filePath = path.join(scanFolderName, file);
if (file.substring(0, 1) === "@") {
this.loadPluginsByFolder(filePath);
}
else {
if (file.indexOf("webfaas-plugin-") === 0) {
if (fs.statSync(filePath).isDirectory()) {
let pluginFunctionFactory = require(filePath);
let newPlugin = this.instancePluginBuild(pluginFunctionFactory);
this.core.getLog().write(Core_1.LogLevelEnum.INFO, "loadPluginsByFolder", ILog_1.LogCodeEnum.PROCESS.toString(), "plugin loaded", { file: file }, __filename);
this.addPlugin(newPlugin);
}
}
}
}
}
catch (errTry) {
if (errTry.code === "ENOENT") {
return;
}
else {
this.core.getLog().writeError("loadPluginsByFolder", errTry, { scanFolderName: scanFolderName, file: file }, __filename);
throw errTry;
}
}
}
}
exports.PluginManager = PluginManager;
//# sourceMappingURL=PluginManager.js.map