@samitouri / QOSami-HFS / commits / f791c98f

keep a backup of config.yaml

Massimo Melina committed Jun 5, 2023 at 00:11 UTC f791c98fe24f22dfbca01b1af812a39f2836a30f
1 file changed +8 -1
src/config.ts
+8 -1
@@ -1,7 +1,7 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 import EventEmitter from 'events'
4 -import { argv, ORIGINAL_CWD, VERSION } from './const'
4 +import { argv, DAY, ORIGINAL_CWD, VERSION } from './const'
5 import { watchLoad } from './watchLoad'
6 import yaml from 'yaml'
7 import _ from 'lodash'
@@ -10,6 +10,7 @@ import { copyFileSync, existsSync, renameSync, statSync } from 'fs'
10 import { join, resolve } from 'path'
11 import events from './events'
12 import { homedir } from 'os'
13 +import { copyFile, stat } from 'fs/promises'
14
15 const FILE = 'config.yaml'
16
@@ -180,6 +181,12 @@ function setConfig1(k: string, newV: unknown, saveChanges=true, valueVersion?: V
181 const saveDebounced = debounceAsync(async () => {
182 while (!started)
183 await wait(100)
184 + // keep backup
185 + const bak = filePath + '.bak'
186 + const aWeekAgo = Date.now() - DAY * 7
187 + if (await stat(bak).then(x => aWeekAgo > Number(x.mtime || x.ctime), () => true))
188 + await copyFile(filePath, bak).catch(() => {}) // ignore errors
189 +
190 let txt = yaml.stringify({ ...state, version: VERSION }, { lineWidth:1000 })
191 if (txt.trim() === '{}') // most users wouldn't understand
192 txt = ''