UNPKG

@runejs/common

Version:

Common logging, networking, compression, and other miscellaneous functionality for RuneJS.

96 lines 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadConfigurationFiles = exports.getFiles = void 0; const tslib_1 = require("tslib"); const util_1 = (0, tslib_1.__importDefault)(require("util")); const fs_1 = (0, tslib_1.__importStar)(require("fs")); const path_1 = require("path"); const logger_1 = require("../logger"); const readdir = util_1.default.promisify(fs_1.default.readdir); const stat = util_1.default.promisify(fs_1.default.stat); /** * Searches for files within the given directory, using a FileFilter to filter the results if provided. * Returns an array of paths to the matching files so that they may be imported. * @param directory The directory to search for files. * @param filter [optional] The whitelist or blacklist filter to apply to the file search. */ function getFiles(directory, filter) { return (0, tslib_1.__asyncGenerator)(this, arguments, function* getFiles_1() { var e_1, _a; const files = yield (0, tslib_1.__await)(readdir(directory)); for (const file of files) { const path = (0, path_1.join)(directory, file); const statistics = yield (0, tslib_1.__await)(stat(path)); if (statistics.isDirectory()) { try { for (var _b = (e_1 = void 0, (0, tslib_1.__asyncValues)(getFiles(path, filter))), _c; _c = yield (0, tslib_1.__await)(_b.next()), !_c.done;) { const child = _c.value; yield yield (0, tslib_1.__await)(child); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) yield (0, tslib_1.__await)(_a.call(_b)); } finally { if (e_1) throw e_1.error; } } } else { if ((filter === null || filter === void 0 ? void 0 : filter.type) === 'blacklist') { // blacklist const invalid = filter.list.some(item => file === item); if (invalid) { continue; } } else if ((filter === null || filter === void 0 ? void 0 : filter.type) === 'whitelist') { // whitelist const invalid = !filter.list.some(item => file.endsWith(item)); if (invalid) { continue; } } yield yield (0, tslib_1.__await)(path); } } }); } exports.getFiles = getFiles; /** * Searches the given directory for configuration files and converts the results into a * JSON array of the type T. * @param configurationDir The directory to search for configuration files. * @param filter [optional] The file filter to apply to this search. */ function loadConfigurationFiles(configurationDir, filter) { var e_2, _a; return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { const files = []; try { for (var _b = (0, tslib_1.__asyncValues)(getFiles(configurationDir, filter)), _c; _c = yield _b.next(), !_c.done;) { const path = _c.value; try { const configContent = JSON.parse((0, fs_1.readFileSync)(path, 'utf8')); if (configContent) { files.push(configContent); } } catch (error) { logger_1.logger.error(`Error loading configuration file at ${path}:`); logger_1.logger.error(error); } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b); } finally { if (e_2) throw e_2.error; } } return files; }); } exports.loadConfigurationFiles = loadConfigurationFiles; //# sourceMappingURL=file-util.js.map