synclounge/SettingsHelper.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-07-20 04:31:44 +00:00
2019-04-20 08:03:44 +00:00
const args = require('args-parser')(process.argv);
const settings = require(`./data/settings.json`);
2019-04-20 08:03:44 +00:00
2018-07-20 04:31:44 +00:00
module.exports = function () {
const fields = [
{
local: 'webroot',
env: 'WEB_ROOT',
default: ''
},
{
local: 'webapp_port',
env: 'WEB_PORT',
default: '8088'
},
{
local: 'accessUrl',
env: 'WEB_ACCESSURL',
default: ''
},
{
local: 'serverroot',
env: 'SERVER_ROOT',
default: ''
},
{
local: 'server_port',
env: 'SERVER_PORT',
default: '8089'
},
{
local: 'autoJoin',
env: 'AUTOJOIN_ENABLED',
default: 'false'
},
{
local: 'autoJoinServer',
env: 'AUTOJOIN_SERVERURL',
default: ''
},
{
local: 'autoJoinRoom',
env: 'AUTOJOIN_ROOM',
default: ''
},
{
local: 'autoJoinPassword',
env: 'AUTOJOIN_PASSWORD',
default: ''
}
2019-04-20 08:03:44 +00:00
];
2018-07-20 12:28:08 +00:00
// Load and export our settings in preference of ENV -> args
2019-04-20 08:03:44 +00:00
const output = {};
2018-07-20 04:31:44 +00:00
for (let i = 0; i < fields.length; i++) {
2019-04-20 08:03:44 +00:00
const setting = fields[i];
// console.log('Processing setting', setting);
// console.log(`Args: '${args[setting.env]}'; '${args[setting.local]}'`);
// console.log(`ENV: '${process.env[setting.env]}'; '${process.env[setting.local]}'`);
// console.log(`Settings: '${settings[setting.local]}'; '${setting.default}'`);
output[setting.local] = args[setting.env] || args[setting.local] || process.env[setting.env] || process.env[setting.local] || settings[setting.local] || setting.default;
// Remove trailing slashes, if they exist
if ((setting.local == 'webroot' || setting.local == 'accessUrl') && output[setting.local].endsWith("/")) {
output[setting.local] = output[setting.local].replace(/\/+$/, "");
}
// Add leading slash, if not provided
if (setting.local == 'webroot' && !output[setting.local].startsWith("/")) {
// Make sure it starts with one leading slash
output[setting.local] = `/${output[setting.local]}`;
}
process.env[setting.local] = output[setting.local];
2018-07-20 04:31:44 +00:00
}
//console.log('Our settings are', output)
2019-04-20 08:03:44 +00:00
return output;
};