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

Added Prefix option

parent 78f8c838
No related branches found
No related tags found
1 merge request!2Resolve "Add optional PREFIX for ENV Variables"
Pipeline #28636 failed
......@@ -9,7 +9,7 @@ Simple Config with ENV and Files Support.
## Usage
`const Config = require('@libs/config')`
`let config = new Config([basePath])`
`let config = new Config([basePath], [EnvPrefix])`
Then config is your config object. (Use it like config.setting)
......@@ -49,6 +49,9 @@ This makes *_show* a reserved keyword
If you give a basePath, the config-Files are used from there.
Else the main dir of the application will be used.
YOu can give a prefix for the enviroment variables.
e.g. if you give "PREFIX" as prefix, the setting "setting.deep.key" can be reached with *PREFIX_SETTING_DEEP_KEY*
### Values
You may use direct values like strings or numbers:
......@@ -90,6 +93,12 @@ You can also use files to read the value from. This makes the config compatible
`SETTING=overwritten-by-env node examples/env/index.js`
### enviroment variables with prefix
(Enviroment set by command to not pollute your machine)
`PREFIX_SETTING=overwritten-by-env node examples/env_prefix/index.js`
### use files to read from
(Enviroment set by command to not pollute your machine)
......
{
"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(undefined, 'PREFIX')
console.log(JSON.stringify(config, undefined, 2))
\ No newline at end of file
......@@ -2,9 +2,10 @@ var fs = require('fs')
var path = require('path')
var merge = require('lodash.merge')
module.exports = function(basePath = undefined) {
module.exports = function(basePath = undefined, envPrefix = undefined) {
let configDefaults
let configLocal
let envPrefixUpper = envPrefix ? envPrefix.toUpperCase() + "_" : ''
if (basePath) {
configDefaults = path.join(basePath, 'config.defaults.json')
configLocal = path.join(basePath, 'config.json')
......@@ -58,7 +59,7 @@ module.exports = function(basePath = undefined) {
let keys = objectDeepKeys(this)
for (let index = 0; index < keys.length; index++) {
const element = keys[index]
let env = process.env[element.toUpperCase().replace(/\./g, '_')]
let env = process.env[envPrefixUpper + element.toUpperCase().replace(/\./g, '_')]
if (env) {
env = (env == 'true') ? true : env
env = (env == 'false') ? false : env
......
......@@ -90,6 +90,29 @@ describe('config', function() {
expect(config.another.setting).toBe('avalue')
})
it('should have all values with preference to env and prefix', async function() {
await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
await fs.writeFile('config.json', JSON.stringify(jsonLocals))
process.env['P_SETTING'] = 'overwritten-by-env'
process.env['P_ANOTHER_MORE'] = 'false'
process.env['P_EVEN_DEEPER_KEY'] = 'true'
let config = new Config(undefined, 'p')
await fs.unlink('config.json')
await fs.unlink('config.defaults.json')
delete process.env['P_SETTING']
delete process.env['P_ANOTHER_MORE']
delete process.env['P_EVEN_DEEPER_KEY']
expect(config.setting).toBe('overwritten-by-env')
expect(config.another.more).toBe(false)
expect(config.even.deeper.key).toBe(true)
expect(config.another.setting).toBe('avalue')
})
it('should read in a file when given', async function() {
jsonLocals.setting = 'file:file.txt'
await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
......
{
"name": "@libs/config",
"version": "1.12.4",
"version": "1.13.0",
"description": "Simple Config with ENV Support",
"main": "index.js",
"scripts": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment