2019-12-18 14:48:05 +00:00
'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 ;
}
2021-03-25 12:52:17 +00:00
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 } . ` ) ;
2019-12-18 14:48:05 +00:00
2021-03-25 12:52:17 +00:00
global . entity = entity ;
2019-12-18 14:48:05 +00:00
const baseConfigFile = path . join ( configDirectory , 'config.json' ) ;
2021-03-25 12:52:17 +00:00
const envConfigFile = env ? entity ? path . join ( configDirectory , ` config. ${ env } . ${ bank } . ${ entity } .json ` ) : path . join ( configDirectory , ` config. ${ env } . ${ bank } .json ` ) : null ;
2019-12-18 14:48:05 +00:00
return {
... safeLoadJson ( baseConfigFile ) ,
... safeLoadJson ( envConfigFile ) ,
}
}
2019-12-30 07:37:19 +00:00
module . exports = loadConfig ;