Do not always require JSON parsing from env vars

This commit is contained in:
Jerko Steiner 2019-11-18 23:37:05 -03:00
parent 714b8f55db
commit 1ef92f6b19
2 changed files with 12 additions and 3 deletions

View File

@ -198,11 +198,13 @@ describe('readConfig', () => {
const config = readConfig({
PEERCALLS__TEST_VALUE__SUB_VALUE_1: '1',
PEERCALLS__TEST_VALUE__SUB_VALUE_2: JSON.stringify({a: 2}),
PEERCALLS__TEST_VALUE__SUB_VALUE_3: 'string',
}, '/tmp', '/tmp')
expect(config.value()).toEqual({
testValue: {
subValue1: 1,
subValue2: {a: 2},
subValue3: 'string',
},
})
})

View File

@ -4,7 +4,7 @@ import { resolve, join } from 'path'
import { safeLoad } from 'js-yaml'
import _debug from 'debug'
const debug = _debug('peercalls')
const debug = _debug('peercalls:config')
const isObject = (value: unknown) => value !== null && typeof value === 'object'
@ -175,8 +175,15 @@ export function readConfig(
.forEach(shortKey => {
cfg = cfg[shortKey] = cfg[shortKey] || {}
})
cfg[lastKey] = JSON.parse(value)
try {
cfg[lastKey] = JSON.parse(value)
} catch (err) {
cfg[lastKey] = value
}
})
return new ReadConfig(mergeConfig(envConfig, config))
const configWithEnv = mergeConfig(envConfig, config)
debug('Read configuration: %j', configWithEnv)
return new ReadConfig(configWithEnv)
}