diff options
Diffstat (limited to 'packages/main/src/infrastructure')
-rw-r--r-- | packages/main/src/infrastructure/config/impl/ConfigFile.ts | 64 |
1 files changed, 29 insertions, 35 deletions
diff --git a/packages/main/src/infrastructure/config/impl/ConfigFile.ts b/packages/main/src/infrastructure/config/impl/ConfigFile.ts index a817717..c4e6d22 100644 --- a/packages/main/src/infrastructure/config/impl/ConfigFile.ts +++ b/packages/main/src/infrastructure/config/impl/ConfigFile.ts | |||
@@ -22,7 +22,6 @@ import { watch } from 'node:fs'; | |||
22 | import { readFile, stat, writeFile } from 'node:fs/promises'; | 22 | import { readFile, stat, writeFile } from 'node:fs/promises'; |
23 | import path from 'node:path'; | 23 | import path from 'node:path'; |
24 | 24 | ||
25 | import JSON5 from 'json5'; | ||
26 | import { throttle } from 'lodash-es'; | 25 | import { throttle } from 'lodash-es'; |
27 | 26 | ||
28 | import type Config from '../../../stores/config/Config'; | 27 | import type Config from '../../../stores/config/Config'; |
@@ -35,70 +34,65 @@ import type { ReadConfigResult } from '../ConfigRepository'; | |||
35 | const log = getLogger('ConfigFile'); | 34 | const log = getLogger('ConfigFile'); |
36 | 35 | ||
37 | export default class ConfigFile implements ConfigRepository { | 36 | export default class ConfigFile implements ConfigRepository { |
38 | readonly #userDataDir: string; | 37 | private readonly configFilePath: string; |
39 | 38 | ||
40 | readonly #configFileName: string; | 39 | private writingConfig = false; |
41 | 40 | ||
42 | readonly #configFilePath: string; | 41 | private timeLastWritten: Date | undefined; |
43 | 42 | ||
44 | #writingConfig = false; | 43 | constructor( |
45 | 44 | private readonly userDataDir: string, | |
46 | #timeLastWritten: Date | undefined; | 45 | private readonly configFileName = 'settings.json', |
47 | 46 | ) { | |
48 | constructor(userDataDir: string, configFileName = 'config.json5') { | 47 | this.configFilePath = path.join(userDataDir, configFileName); |
49 | this.#userDataDir = userDataDir; | ||
50 | this.#configFileName = configFileName; | ||
51 | this.#configFilePath = path.join(userDataDir, configFileName); | ||
52 | } | 48 | } |
53 | 49 | ||
54 | async readConfig(): Promise<ReadConfigResult> { | 50 | async readConfig(): Promise<ReadConfigResult> { |
55 | let configStr: string; | 51 | let configStr: string; |
56 | try { | 52 | try { |
57 | configStr = await readFile(this.#configFilePath, 'utf8'); | 53 | configStr = await readFile(this.configFilePath, 'utf8'); |
58 | } catch (error) { | 54 | } catch (error) { |
59 | if (isErrno(error, 'ENOENT')) { | 55 | if (isErrno(error, 'ENOENT')) { |
60 | log.debug('Config file', this.#configFilePath, 'was not found'); | 56 | log.debug('Config file', this.configFilePath, 'was not found'); |
61 | return { found: false }; | 57 | return { found: false }; |
62 | } | 58 | } |
63 | throw error; | 59 | throw error; |
64 | } | 60 | } |
65 | log.info('Read config file', this.#configFilePath); | 61 | log.info('Read config file', this.configFilePath); |
66 | return { | 62 | return { |
67 | found: true, | 63 | found: true, |
68 | data: JSON5.parse(configStr), | 64 | data: JSON.parse(configStr), |
69 | }; | 65 | }; |
70 | } | 66 | } |
71 | 67 | ||
72 | async writeConfig(configSnapshot: Config): Promise<void> { | 68 | async writeConfig(configSnapshot: Config): Promise<void> { |
73 | const configJson = JSON5.stringify(configSnapshot, { | 69 | const configJson = JSON.stringify(configSnapshot, undefined, 2); |
74 | space: 2, | 70 | this.writingConfig = true; |
75 | }); | ||
76 | this.#writingConfig = true; | ||
77 | try { | 71 | try { |
78 | await writeFile(this.#configFilePath, configJson, 'utf8'); | 72 | await writeFile(this.configFilePath, configJson, 'utf8'); |
79 | const { mtime } = await stat(this.#configFilePath); | 73 | const { mtime } = await stat(this.configFilePath); |
80 | log.trace('Config file', this.#configFilePath, 'last written at', mtime); | 74 | log.trace('Config file', this.configFilePath, 'last written at', mtime); |
81 | this.#timeLastWritten = mtime; | 75 | this.timeLastWritten = mtime; |
82 | } finally { | 76 | } finally { |
83 | this.#writingConfig = false; | 77 | this.writingConfig = false; |
84 | } | 78 | } |
85 | log.debug('Wrote config file', this.#configFilePath); | 79 | log.debug('Wrote config file', this.configFilePath); |
86 | } | 80 | } |
87 | 81 | ||
88 | watchConfig(callback: () => Promise<void>, throttleMs: number): Disposer { | 82 | watchConfig(callback: () => Promise<void>, throttleMs: number): Disposer { |
89 | log.debug('Installing watcher for', this.#userDataDir); | 83 | log.debug('Installing watcher for', this.userDataDir); |
90 | 84 | ||
91 | const configChanged = throttle(async () => { | 85 | const configChanged = throttle(async () => { |
92 | let mtime: Date; | 86 | let mtime: Date; |
93 | try { | 87 | try { |
94 | const stats = await stat(this.#configFilePath); | 88 | const stats = await stat(this.configFilePath); |
95 | mtime = stats.mtime; | 89 | mtime = stats.mtime; |
96 | log.trace('Config file last modified at', mtime); | 90 | log.trace('Config file last modified at', mtime); |
97 | } catch (error) { | 91 | } catch (error) { |
98 | if (isErrno(error, 'ENOENT')) { | 92 | if (isErrno(error, 'ENOENT')) { |
99 | log.debug( | 93 | log.debug( |
100 | 'Config file', | 94 | 'Config file', |
101 | this.#configFilePath, | 95 | this.configFilePath, |
102 | 'was deleted after being changed', | 96 | 'was deleted after being changed', |
103 | ); | 97 | ); |
104 | return; | 98 | return; |
@@ -106,27 +100,27 @@ export default class ConfigFile implements ConfigRepository { | |||
106 | throw error; | 100 | throw error; |
107 | } | 101 | } |
108 | if ( | 102 | if ( |
109 | !this.#writingConfig && | 103 | !this.writingConfig && |
110 | (this.#timeLastWritten === undefined || mtime > this.#timeLastWritten) | 104 | (this.timeLastWritten === undefined || mtime > this.timeLastWritten) |
111 | ) { | 105 | ) { |
112 | log.debug( | 106 | log.debug( |
113 | 'Found a config file modified at', | 107 | 'Found a config file modified at', |
114 | mtime, | 108 | mtime, |
115 | 'which is newer than last written', | 109 | 'which is newer than last written', |
116 | this.#timeLastWritten, | 110 | this.timeLastWritten, |
117 | ); | 111 | ); |
118 | await callback(); | 112 | await callback(); |
119 | } | 113 | } |
120 | }, throttleMs); | 114 | }, throttleMs); |
121 | 115 | ||
122 | const watcher = watch(this.#userDataDir, { | 116 | const watcher = watch(this.userDataDir, { |
123 | persistent: false, | 117 | persistent: false, |
124 | }); | 118 | }); |
125 | 119 | ||
126 | watcher.on('change', (eventType, filename) => { | 120 | watcher.on('change', (eventType, filename) => { |
127 | if ( | 121 | if ( |
128 | eventType === 'change' && | 122 | eventType === 'change' && |
129 | (filename === this.#configFileName || filename === null) | 123 | (filename === this.configFileName || filename === null) |
130 | ) { | 124 | ) { |
131 | configChanged()?.catch((err) => { | 125 | configChanged()?.catch((err) => { |
132 | log.error('Unhandled error while listening for config changes', err); | 126 | log.error('Unhandled error while listening for config changes', err); |
@@ -135,7 +129,7 @@ export default class ConfigFile implements ConfigRepository { | |||
135 | }); | 129 | }); |
136 | 130 | ||
137 | return () => { | 131 | return () => { |
138 | log.trace('Removing watcher for', this.#configFilePath); | 132 | log.trace('Removing watcher for', this.configFilePath); |
139 | watcher.close(); | 133 | watcher.close(); |
140 | }; | 134 | }; |
141 | } | 135 | } |