UNPKG

xmppjs-chat-bot

Version:
47 lines 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HandlersDirectory = void 0; const abstract_1 = require("./handlers/abstract"); const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); let directory; class HandlersDirectory { constructor() { this.handlers = new Map(); } static singleton() { if (!directory) { directory = new HandlersDirectory(); } return directory; } register(key, handlerDHandlerDerivedClass) { if (this.handlers.has(key)) { throw new Error('Handler ' + key + ' already exists'); } this.handlers.set(key, handlerDHandlerDerivedClass); } async registerFromFile(filePath) { filePath = (0, node_path_1.resolve)(filePath); const stat = await node_fs_1.promises.stat(filePath); if (!stat) { throw new Error(`File "${filePath}" does not exist`); } if (!stat.isFile()) { throw new Error(`"${filePath}" is not a file`); } const lib = require(filePath); if (!('registerHandlers' in lib) || (typeof lib.registerHandlers !== 'function')) { throw new Error(`"${filePath} has no exported "registerHandlers" function`); } await lib.registerHandlers(this); } getClass(key) { return this.handlers.get(key); } get HandlerBaseClass() { return abstract_1.Handler; } } exports.HandlersDirectory = HandlersDirectory; //# sourceMappingURL=handlers_directory.js.map