work with empty config file

Massimo Melina committed Feb 10, 2022 at 11:04 UTC 5fd955a4472c09357a2757cb68a42b72fac3c1f4
2 files changed +10 -3
package.json
+1 -1
@@ -14,7 +14,7 @@
14 "start-frontend": "cd frontend && npm run start",
15 "start-admin": "cd admin && npm run start",
16 "start": "node dist",
17 - "build": "npm install && rm -rf dist/src && tsc --target es2018 && cp -v -r package*.json READ* plugins tests dist && cp config.yaml dist/config-example.yaml && cp accounts.yaml dist/accounts-example.yaml && cd dist && npm ci --production && cd .. && node afterbuild",
17 + "build": "npm install && rm -rf dist/src && tsc --target es2018 && cp -v -r package*.json READ* plugins dist && cd dist && npm ci --production && cd .. && node afterbuild",
18 "build-all": "rm -rf dist && npm run build && npm run build-frontend && npm run build-admin && npm audit --production && echo COMPLETED",
19 "build-frontend": "cd frontend && npm install && npm run build",
20 "build-admin": "cd admin && npm install && npm run build",
src/config.ts
+9 -2
@@ -5,6 +5,8 @@ import fs from 'fs/promises'
5 import yaml from 'yaml'
6 import _ from 'lodash'
7 import { objSameKeys, onOffMap } from './misc'
8 +import { exists } from 'fs'
9 +import { promisify } from 'util'
10
11 export const CFG_ALLOW_CLEAR_TEXT_LOGIN = 'allow_clear_text_login'
12
@@ -67,6 +69,8 @@ export function getWholeConfig({ omit=[], only=[] }: { omit:string[], only:strin
69 }
70
71 export function setConfig(newCfg: Record<string,any>, partial=false) {
72 + if (!newCfg)
73 + newCfg = {}
74 for (const k in newCfg)
75 check(k)
76 const oldKeys = Object.keys(state)
@@ -97,8 +101,11 @@ export function setConfig(newCfg: Record<string,any>, partial=false) {
101
102 export const saveConfigAsap = _.debounce(async () => {
103 let txt = yaml.stringify(state)
100 - if (txt.trim() === '{}') // users wouldn't understand
101 - txt = ''
104 + if (txt.trim() === '{}') // most users wouldn't understand
105 + if (await promisify(exists)(path)) // if a file exists then empty it, else don't bother creating it
106 + txt = ''
107 + else
108 + return
109 fs.writeFile(path, txt)
110 .catch(err => console.error('Failed at saving config file, please ensure it is writable.', String(err)))
111 }, 100)