skyline/config/utils.js
Jingwei.Zhang b36d5593e6 feat: support custom locale language
support custom locale language and custom support languages.

Change-Id: Ia235cb83b65c8530449f52d9ea575c3d6f603f04
2023-08-09 09:25:11 +08:00

65 lines
1.5 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const { merge, extend, has } = 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 localConfig = loadYaml(tryFile);
if (typeof localConfig === 'object') {
merge(config, localConfig);
}
}
return key ? config[key] : config;
};
const getObjectConfig = (key) => {
// parse config yaml
const config = loadYaml(root('./config.yaml')) || {};
if (!has(config, key)) {
return {};
}
const defaultConfig = config[key];
const result = defaultConfig;
const tryFile = root('./local_config.yaml');
if (fs.existsSync(tryFile)) {
// merge local_config
const localConfig = loadYaml(tryFile);
extend(result, localConfig[key] || {});
}
return result;
};
const getGlobalVariables = () => {
const variables = getObjectConfig('globalVariables') || {};
// eslint-disable-next-line no-console
console.log('globalVariables', variables, JSON.stringify(variables));
return JSON.stringify(variables);
};
module.exports = {
getServerConfig,
root,
getGlobalVariables,
};