2020-06-29 06:05:07 +00:00
|
|
|
const nconf = require('nconf');
|
2020-07-21 21:28:48 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
|
2020-06-29 06:05:07 +00:00
|
|
|
const defaults = require('./defaults');
|
|
|
|
|
2020-08-24 00:00:26 +00:00
|
|
|
const omit = (keys, obj) => keys.reduce((a, e) => {
|
|
|
|
const { [e]: no, ...rest } = a;
|
|
|
|
return rest;
|
|
|
|
}, obj);
|
|
|
|
|
|
|
|
// Doesn't return the keys specified in the blockList
|
|
|
|
const get = (file, blockList = []) => {
|
|
|
|
// Clear out nconf memory in case another dependency used it before
|
|
|
|
nconf.reset();
|
|
|
|
|
2020-07-21 21:28:48 +00:00
|
|
|
nconf
|
|
|
|
.argv({
|
|
|
|
separator: '__',
|
|
|
|
parseValues: true,
|
|
|
|
})
|
|
|
|
.env({
|
|
|
|
separator: '__',
|
|
|
|
lowerCase: true,
|
|
|
|
parseValues: true,
|
|
|
|
whitelist: Object.keys(defaults).concat([
|
|
|
|
'autojoin__server',
|
|
|
|
'autojoin__room',
|
2020-09-28 19:23:52 +00:00
|
|
|
|
2020-07-21 21:28:48 +00:00
|
|
|
'authentication__mechanism',
|
|
|
|
'authentication__type',
|
|
|
|
'authentication__authorized',
|
|
|
|
|
|
|
|
'custom_server__name',
|
|
|
|
'custom_server__location',
|
|
|
|
'custom_server__url',
|
|
|
|
'custom_server__image',
|
|
|
|
|
|
|
|
'default_slplayer_quality',
|
|
|
|
]),
|
2020-08-31 22:55:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const configFile = nconf.get('config_file') || file;
|
|
|
|
|
|
|
|
if (configFile) {
|
|
|
|
nconf.file({ file: configFile });
|
|
|
|
}
|
|
|
|
|
|
|
|
nconf.defaults(defaults);
|
2020-07-21 21:28:48 +00:00
|
|
|
|
|
|
|
// Filter out the weird stuff
|
|
|
|
const {
|
2020-08-31 22:55:14 +00:00
|
|
|
config_file: no, type, $0: firstArg, _: command, modern, ...config
|
2020-07-21 21:28:48 +00:00
|
|
|
} = nconf.get();
|
|
|
|
|
2020-08-24 00:00:26 +00:00
|
|
|
// Remove blockList items
|
|
|
|
const filteredConfig = omit(blockList, config);
|
|
|
|
return filteredConfig;
|
|
|
|
};
|
2020-07-21 21:28:48 +00:00
|
|
|
|
2020-08-24 00:00:26 +00:00
|
|
|
// Saves the give config json to the specified file
|
|
|
|
const save = (config, file) => {
|
|
|
|
fs.writeFileSync(file, JSON.stringify(config));
|
2020-07-21 21:28:48 +00:00
|
|
|
};
|
|
|
|
|
2020-08-24 00:00:26 +00:00
|
|
|
module.exports = {
|
|
|
|
get,
|
|
|
|
save,
|
|
|
|
};
|