Line data Source code
1 : 2 1 : const { merge, defaultTo, toString } = require('lodash'); 3 1 : const yaml = require('js-yaml'); 4 1 : const fs = require('fs'); 5 1 : const path = require('path'); 6 1 : const debug = require('debug')('yaml:loadAll'); 7 : 8 : /** 9 : * @param {string} dir 10 : * @param {RegExp} [pattern=/^config.*.yml$/] [description] 11 : * @return {AppServer.Config|null} 12 : */ 13 : function loadAll(dir, pattern = /^config.*\.yml$/) { 14 2 : try { 15 : /** @type {string[]} */ 16 2 : const files = []; 17 2 : fs.readdirSync(dir).forEach((f) => { 18 20 : if ((f.endsWith('.json') || f.endsWith('.yml')) && 19 : (!pattern || pattern.test(f))) { 20 0 : files.push(f); 21 : } 22 : }); 23 1 : debug('Loading files: %o from "%s"', files, dir); 24 1 : return files.reduce((ret, f) => merge(ret, 25 : yaml.load(toString(fs.readFileSync(path.join(dir, f))))), null); 26 : } 27 : catch (e) { 28 1 : debug(/** @type {Error} */(e).message); 29 1 : return null; 30 : } 31 : } 32 : 33 : /** @type {AppServer.Config} */ 34 1 : const config = loadAll('/etc/app') || loadAll('./') || 35 : require('./config-stub'); /* eslint-disable-line global-require */ 36 : 37 1 : config.port = defaultTo(config.port, 8080); 38 1 : config.basePath = defaultTo(config.basePath, ''); 39 : 40 1 : module.exports = /** @type {AppServer.Config} */ (config);