skyline/config/utils.js
Jingwei.Zhang 0213d8c6d6 feat: optimize configuration reading
Add config/config.yaml to set default configurations. Use the config/local_config.yaml to set the custom configurations

Change-Id: I22049e478b6440c765751c8f17663f36f33c277a
2023-05-31 11:41:19 +08:00

38 lines
803 B
JavaScript

const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const { merge } = require('lodash');
const root = (dir) =>
`${path.resolve(__dirname, './')}/${dir}`.replace(/(\/+)/g, '/');
const loadYaml = (filePath) => {
try {
return yaml.load(fs.readFileSync(filePath), 'utf8');
} catch (e) {
return false;
}
};
const getServerConfig = (key) => {
// parse config yaml
const config = loadYaml(root('./config.yaml')) || {};
const tryFile = root('./local_config.yaml');
if (fs.existsSync(tryFile)) {
// merge local_config
const local_config = loadYaml(tryFile);
if (typeof local_config === 'object') {
merge(config, local_config);
}
}
return key ? config[key] : config;
};
module.exports = {
getServerConfig,
root,
};