fix: missing config file was preventing HFS from starting correctly
Massimo Melina committed
Jan 17, 2022 at 20:16 UTC
413a01cbcba25fec7b715180f21576089be81fb2
2 files changed
+14
-6
src/config.ts
+3
-2
@@ -9,10 +9,11 @@ const PATH = 'config.yaml'
9
let started = false
10
let state:Record<string,any> = {}
11
const emitter = new EventEmitter()
12
-watchLoad(argv.config || process.env.hfs_config || PATH, data => {
12
+const path = argv.config || process.env.hfs_config || PATH
13
+watchLoad(path, data => {
14
started = true
15
setConfig(data)
15
-})
16
+}, { failOnFirstAttempt:()=> setConfig({}) })
17
18
const configProps:Record<string, ConfigProps> = {}
19
src/watchLoad.ts
+11
-4
@@ -5,15 +5,18 @@ import { readFileBusy } from './misc'
5
6
export type WatchLoadCanceller = () => void
7
8
-export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>): WatchLoadCanceller {
8
+interface Options { failOnFirstAttempt?: ()=>void }
9
+
10
+export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>, { failOnFirstAttempt }:Options={}): WatchLoadCanceller {
11
let doing = false
12
+ let first = true
13
let watcher: FSWatcher
14
const debounced = _.debounce(load, 500)
15
+ debounced()
16
const timer = setInterval(()=>{
17
try {
18
watcher = watch(path, debounced)
19
debounced()
16
- clearInterval(timer)
20
}
21
catch(e){
22
}
@@ -29,17 +32,21 @@ export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>): W
32
async function load(){
33
if (doing) return
34
doing = true
32
- console.debug('loading', path)
35
let data: any
36
try {
37
data = await readFileBusy(path)
38
+ console.debug('loaded', path)
39
if (path.endsWith('.yaml'))
40
data = yaml.parse(data)
41
} catch (e) {
42
+ if (first)
43
+ failOnFirstAttempt?.()
44
doing = false
40
- console.warn('cannot read', path, String(e))
45
+ console.debug('cannot read', path, String(e))
46
return
47
}
48
+ first = false
49
+ clearInterval(timer)
50
await parser(data)
51
doing = false
52
}