Skip to content
Snippets Groups Projects
Commit efc6475c authored by Sigmund, Dominik's avatar Sigmund, Dominik
Browse files

Added Reload option

parent 4fbcc219
No related branches found
No related tags found
1 merge request!1Master
...@@ -23,6 +23,11 @@ It reads from the following sources, performing a deep merge: ...@@ -23,6 +23,11 @@ It reads from the following sources, performing a deep merge:
Enviroment Variables can target deep nested settings: Enviroment Variables can target deep nested settings:
The Setting _setting.deep.key_ can be reached with *SETTING_DEEP_KEY* The Setting _setting.deep.key_ can be reached with *SETTING_DEEP_KEY*
You may use the function _reload()_ to reload the config from all sources.
`config.reload()``
This makes *reload* a reserved keyword
## Examples ## Examples
### Only config.defaults.json ### Only config.defaults.json
......
{
"setting":"value",
"another": {
"setting":"avalue"
}
}
\ No newline at end of file
{
"setting":"overwritten",
"another": {
"more":"settings"
}
}
\ No newline at end of file
const Config = require('../../index')
let config = new Config()
console.log(JSON.stringify(config, undefined, 2))
process.env.SETTING = 'anotherone'
config.reload()
console.log(JSON.stringify(config, undefined, 2))
...@@ -47,5 +47,28 @@ module.exports = function() { ...@@ -47,5 +47,28 @@ module.exports = function() {
} }
} }
config.reload = function () {
try {
fs.accessSync(configDefaults)
config = merge(this, JSON.parse(fs.readFileSync(configDefaults, 'utf8')))
} catch(error) {
console.log('No File ' + configDefaults)
}
try {
fs.accessSync(configLocal)
config = merge(this, JSON.parse(fs.readFileSync(configLocal, 'utf8')))
} catch (error) {
console.log('No File ' + configLocal)
}
let keys = objectDeepKeys(this)
for (let index = 0; index < keys.length; index++) {
const element = keys[index]
const env = process.env[element.toUpperCase().replace('.', '_')]
if (env) {
set(this, element, env)
}
}
}
return config return config
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment