fix: admin/accounts: new accounts not saved if starting with none

Massimo Melina committed Apr 13, 2022 at 14:56 UTC d6bd59457f9bf2cf4572687191ccdeef9a0f2951
1 file changed +5 -7
server/src/config.ts
+5 -7
@@ -50,7 +50,7 @@ export function subscribeConfig<T>({ k, ...definition }:{ k:string } & Partial<C
50 if (started) {
51 let v = state[k]
52 if (v === undefined)
53 - v = _.cloneDeep(defaultValue)
53 + state[k] = v = _.cloneDeep(defaultValue)
54 if (v !== undefined)
55 cb(v)
56 }
@@ -109,11 +109,7 @@ export function setConfig(newCfg: Record<string,any>, save?: boolean) {
109 v = caster(v)
110 const j = JSON.stringify(v)
111 if (j === JSON.stringify(oldV)) return // no change
112 - if (newV === undefined // optimization: we know in this case it's equal to the default
113 - || j === JSON.stringify(defaultValue)) // if we move away from the default value and then come back, we restore the initial state (undefined)
114 - delete state[k]
115 - else
116 - state[k] = v
112 + state[k] = v
113 cfgEvents.emit('new.'+k, v, oldV)
114 if (save === undefined)
115 saveConfigAsap().then()
@@ -123,7 +119,9 @@ export function setConfig(newCfg: Record<string,any>, save?: boolean) {
119 export const saveConfigAsap = debounceAsync(async () => {
120 while (!started)
121 await wait(100)
126 - let txt = yaml.stringify(state, { lineWidth:1000 })
122 + const diff = objSameKeys(state, (v,k) =>
123 + JSON.stringify(v) === JSON.stringify(configProps[k].defaultValue) ? undefined : v)
124 + let txt = yaml.stringify(diff, { lineWidth:1000 })
125 if (txt.trim() === '{}') // most users wouldn't understand
126 if (await promisify(exists)(path)) // if a file exists then empty it, else don't bother creating it
127 txt = ''