fix: configuration passed at command line was not reflected in admin/config

Massimo Melina committed Mar 16, 2022 at 10:39 UTC c7a3fc46bc327816b74cb07c72f0e6642c41a0e4
1 file changed +19 -15
server/src/config.ts
+19 -15
@@ -30,25 +30,25 @@ const { save } = watchLoad(path, values => setConfig(values||{}, false), {
30
31 interface ConfigProps<T> {
32 defaultValue?: T,
33 - caster?:(argV:string)=> T
33 + arg?: T,
34 + caster: (argV:string)=> T
35 }
35 -export function defineConfig<T>(k: string, definition: ConfigProps<T>) {
36 - if (definition.defaultValue !== undefined)
37 - definition.defaultValue = _.cloneDeep(definition.defaultValue)
38 - configProps[k] = definition
39 - if (!definition.caster)
40 - if (typeof definition.defaultValue === 'number')
41 - // @ts-ignore
42 - definition.caster = Number
36 +export function defineConfig<T>(k: string, definition: Partial<ConfigProps<T>>) {
37 + const { caster = _.identity } = definition
38 + configProps[k] = {
39 + caster,
40 + arg: caster(argv[k]),
41 + ...definition,
42 + defaultValue: _.cloneDeep(definition.defaultValue),
43 + }
44 }
45
45 -export function subscribeConfig<T>({ k, ...definition }:{ k:string } & ConfigProps<T>, cb:(v:T, was?:T)=>void) {
46 +export function subscribeConfig<T>({ k, ...definition }:{ k:string } & Partial<ConfigProps<T>>, cb:(v:T, was?:T)=>void) {
47 if (definition)
48 defineConfig(k, definition)
48 - const { caster, defaultValue } = configProps[k] ?? {}
49 - const a = argv[k]
50 - if (a !== undefined)
51 - return cb(caster ? caster(a) : a)
49 + const { defaultValue, arg } = configProps[k] ?? {}
50 + if (arg !== undefined) // it was passed at command line, and it will never change
51 + return cb(arg)
52 const eventName = 'new.'+k
53 if (started) {
54 let v = state[k]
@@ -65,7 +65,11 @@ export function getConfig(k:string) {
65 }
66
67 export function getWholeConfig({ omit=[], only=[] }: { omit:string[], only:string[] }) {
68 - let copy = Object.assign( objSameKeys(configProps, x => x.defaultValue), state )
68 + let copy = Object.assign(
69 + objSameKeys(configProps, x => x.defaultValue),
70 + state,
71 + _.pickBy(objSameKeys(configProps, x => x.arg), x => x !== undefined),
72 + )
73 copy = _.omit(copy, omit)
74 if (only.length)
75 copy = _.pick(copy, only)