enigma-bbs/core/config.js

68 lines
2.2 KiB
JavaScript
Raw Normal View History

// ENiGMA½
const DefaultConfig = require('./config_default');
const ConfigLoader = require('./config_loader');
2014-10-17 02:21:06 +00:00
2020-06-08 04:20:34 +00:00
const _ = require('lodash');
// Global system configuration instance; see Config.create()
let systemConfigInstance;
exports.Config = class Config extends ConfigLoader {
constructor(options) {
super(options);
2020-06-08 04:20:34 +00:00
}
static create(baseConfigPath, cb) {
2020-06-11 01:15:49 +00:00
const replacePaths = [
'loginServers.ssh.algorithms.kex',
'loginServers.ssh.algorithms.cipher',
'loginServers.ssh.algorithms.hmac',
'loginServers.ssh.algorithms.compress',
];
2020-06-11 01:15:49 +00:00
const replaceKeys = [
'args', 'sendArgs', 'recvArgs', 'recvArgsNonBatch',
];
const options = {
defaultConfig : DefaultConfig,
defaultsCustomizer : (defaultVal, configVal, key, path) => {
if (Array.isArray(defaultVal) && Array.isArray(configVal)) {
if (replacePaths.includes(path) || replaceKeys.includes(key)) {
// full replacement using user config value
return configVal;
} else {
// merge user config & default config; keep only unique
_.uniq(defaultVal.concat(configVal));
}
}
},
onReload : err => {
if (!err) {
const Events = require('./events.js');
Events.emit(Events.getSystemEvents().ConfigChanged);
}
},
};
systemConfigInstance = new Config(options);
systemConfigInstance.init(baseConfigPath, err => {
if (err) {
console.stdout(`Configuration ${baseConfigPath} error: ${err.message}`); // eslint-disable-line no-console
return cb(err);
}
// late bind an exported get method to the global Config
// instance we just created
exports.get = systemConfigInstance.get.bind(systemConfigInstance);
return cb(err);
});
}
static getDefaultPath() {
// e.g. /enigma-bbs-install-path/config/
return './config/';
}
};