UNPKG

xmppjs-chat-bot

Version:
73 lines 2.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.listenRoomConfDir = void 0; const logger_1 = require("../logger"); const read_1 = require("./read"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); async function listenRoomConfDir(logger, dir, callback) { logger = (0, logger_1.wrapLogger)('listenRoomConfDir', logger); const delays = new Map(); if (!fs_1.default.existsSync(dir)) { logger.error('The directory we want to listen for does not exists'); return null; } const stat = await fs_1.default.promises.stat(dir); if (!stat.isDirectory()) { logger.error('The path we want to listen for room configurations is not a directory'); return null; } const loadRoomConfFile = async (filepath) => { if (!filepath.endsWith('.json')) { logger.debug('Ignoring file ' + filepath + ', is not a .json file.'); return; } const stat = await fs_1.default.promises.stat(filepath); if (stat.isDirectory()) { logger.error(filepath + ' is a directory, can`t load as file'); return; } const conf = await (0, read_1.readRoomConf)(filepath, logger); if (conf) { logger.debug('Conf readed, we can call the callback'); await callback(conf); } else { logger.error('Can\'t load ' + filepath + ' , seems not valid'); } }; const w = fs_1.default.watch(dir, { persistent: false, recursive: false }, (event, filename) => { logger.debug('Change ' + event + ' on ' + filename); if (delays.has(filename)) { return; } delays.set(filename, setTimeout(() => { delays.delete(filename); logger.debug('Handling change on ' + filename); const filepath = path_1.default.resolve(dir, filename); loadRoomConfFile(filepath).then(() => { logger.debug('New Conf loaded'); }, (err) => { logger.error(err ?? 'Failed loading new conf'); }); }, 100)); }); logger.debug('Loading all existing conf files...'); const files = await fs_1.default.promises.readdir(dir); for (const file of files) { const fp = path_1.default.resolve(dir, file); logger.debug('Loading file ' + fp); await loadRoomConfFile(fp); } return () => { w.close(); }; } exports.listenRoomConfDir = listenRoomConfDir; //# sourceMappingURL=listen.js.map