node-ebics-client/examples/loadConfig.js
Herman van Hazendonk de92265c95 Examples: Improve config implementation for multibank.
When using the software with multiple banks, the current config file solution wasn't very flexible. We had some different local implementation at our end. In order to use upstream software directly without any changes, suggesting to merge these changes. For me locally it would mean I can get rid of a lot of local example code which are currently bank and even entity specific and can be made more generic.
This will also allow multiple EBICS connections with the same bank for different entities as well, in case this is needed. In our case we have multiple EBICS connections with the same bank.

Signed-off-by: Herman van Hazendonk <github.com@herrie.org>
2021-03-26 19:57:54 +01:00

47 lines
1.3 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const safeLoadJson = (file) => {
if (!file)
return {};
try {
return JSON.parse(fs.readFileSync(file));
} catch (e) {
console.warn(`Couldn't load ${file} config file.`);
return {};
}
}
const getDefaultEnv = () => {
const [,,parArg] = process.argv;
return parArg || process.env.NODE_ENV;
}
const getBankIdentifier = () => {
const [,,,parArg] = process.argv;
return parArg || "testbank";
}
const getEntityIdentifier = () => {
const [,,,,parArg] = process.argv;
return parArg || ""
}
const loadConfig = (configDirectory = path.join(__dirname, './config'), env = getDefaultEnv(), bank = getBankIdentifier(), entity = getEntityIdentifier()) => {
entity ? console.log(`Loading config from ${configDirectory} with env set to ${env}, bank set to ${bank} and entity set to ${entity}.`) : console.log(`Loading config from ${configDirectory} with env set to ${env} and bank set to ${bank}.`);
global.entity = entity;
const baseConfigFile = path.join(configDirectory, 'config.json');
const envConfigFile = env ? entity ? path.join(configDirectory, `config.${env}.${bank}.${entity}.json`) : path.join(configDirectory, `config.${env}.${bank}.json`) : null;
return {
...safeLoadJson(baseConfigFile),
...safeLoadJson(envConfigFile),
}
}
module.exports = loadConfig;