UNPKG

@runejs/common

Version:

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

66 lines 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseServerConfig = exports.sanitizeConfigOptions = exports.defaults = void 0; const tslib_1 = require("tslib"); const path_1 = (0, tslib_1.__importDefault)(require("path")); const fs = (0, tslib_1.__importStar)(require("fs")); const js_yaml_1 = require("js-yaml"); const logger_1 = require("../logger"); exports.defaults = { configDir: path_1.default.join('.', 'config'), cacheDir: path_1.default.join('.', 'cache'), filestoreDir: path_1.default.join('.', 'filestore'), configFileName: 'server-config' }; const sanitizeConfigOptions = (options) => { if (!options) { options = { useDefault: false }; } const keys = Object.keys(exports.defaults); for (const propName of keys) { if (!options[propName]) { options[propName] = exports.defaults[propName]; } } return options; }; exports.sanitizeConfigOptions = sanitizeConfigOptions; function parseServerConfig(options) { options = (0, exports.sanitizeConfigOptions)(options); let filePath = path_1.default.join(options.configDir, options.configFileName); if (options.useDefault) { filePath += '.example'; } let fileType; if (fs.existsSync(`${filePath}.json`)) { fileType = 'json'; } else if (fs.existsSync(`${filePath}.yaml`)) { fileType = 'yaml'; } else { if (!options.useDefault) { logger_1.logger.warn(`Server config not provided, using default...`); return parseServerConfig({ useDefault: true }); } else { throw new Error(`Unable to load server configuration: Default (.example) server configuration file not found.`); } } filePath += `.${fileType}`; const configFileContent = fs.readFileSync(filePath, 'utf-8'); if (!configFileContent) { throw new Error(`Syntax error encountered while loading server configuration file.`); } if (fileType === 'json') { options = JSON.parse(configFileContent); } else if (fileType === 'yaml') { options = (0, js_yaml_1.safeLoad)(configFileContent, { schema: js_yaml_1.JSON_SCHEMA }); } return (0, exports.sanitizeConfigOptions)(options); } exports.parseServerConfig = parseServerConfig; //# sourceMappingURL=server-config.js.map