diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/main/src/index.ts | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index a79a6e2..605c17c 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts | |||
@@ -25,8 +25,13 @@ import { | |||
25 | ipcMain, | 25 | ipcMain, |
26 | nativeTheme, | 26 | nativeTheme, |
27 | } from 'electron'; | 27 | } from 'electron'; |
28 | import { readFileSync, watchFile } from 'fs'; | 28 | import { readFileSync } from 'fs'; |
29 | import { readFile, stat, writeFile } from 'fs/promises'; | 29 | import { |
30 | readFile, | ||
31 | stat, | ||
32 | watch, | ||
33 | writeFile, | ||
34 | } from 'fs/promises'; | ||
30 | import { autorun } from 'mobx'; | 35 | import { autorun } from 'mobx'; |
31 | import { | 36 | import { |
32 | applySnapshot, | 37 | applySnapshot, |
@@ -121,7 +126,9 @@ nativeTheme.on('updated', () => { | |||
121 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); | 126 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); |
122 | }); | 127 | }); |
123 | 128 | ||
124 | const configPath = join(app.getPath('userData'), 'config.json'); | 129 | const userDataDir = app.getPath('userData'); |
130 | const configFileName = 'config.json'; | ||
131 | const configPath = join(userDataDir, configFileName); | ||
125 | let loadingConfig = false; | 132 | let loadingConfig = false; |
126 | let savingConfig = false; | 133 | let savingConfig = false; |
127 | let configMtime: Date | null = null; | 134 | let configMtime: Date | null = null; |
@@ -175,21 +182,37 @@ onSnapshot(store.config, (snapshot) => { | |||
175 | } | 182 | } |
176 | }); | 183 | }); |
177 | 184 | ||
178 | async function initializeConfig(): Promise<void> { | 185 | async function watchConfig(): Promise<void> { |
179 | await loadConfig(); | 186 | const configWatcher = watch(userDataDir, { |
180 | watchFile(configPath, ({ mtime }) => { | 187 | persistent: false, |
188 | }); | ||
189 | for await (const { eventType, filename } of configWatcher) { | ||
190 | if (eventType !== 'change' | ||
191 | && (filename !== configFileName || filename !== null)) { | ||
192 | continue; | ||
193 | } | ||
194 | let mtime: Date; | ||
195 | try { | ||
196 | const stats = await stat(configPath); | ||
197 | mtime = stats.mtime; | ||
198 | } catch (err) { | ||
199 | if ((err as NodeJS.ErrnoException).code === 'ENOENT') { | ||
200 | continue; | ||
201 | } | ||
202 | throw err; | ||
203 | } | ||
181 | if (!savingConfig && (configMtime === null || mtime > configMtime)) { | 204 | if (!savingConfig && (configMtime === null || mtime > configMtime)) { |
182 | loadConfig().catch((err) => { | 205 | await loadConfig(); |
183 | console.error('Failed to reload config', configPath, err); | ||
184 | }); | ||
185 | configMtime = mtime; | 206 | configMtime = mtime; |
186 | console.log('Reloaded config', configPath); | 207 | console.log('Reloaded config', configPath); |
187 | } | 208 | } |
188 | }); | 209 | } |
189 | } | 210 | } |
190 | 211 | ||
191 | initializeConfig().catch((err) => { | 212 | loadConfig().catch((err) => { |
192 | console.error('Failed to load config', configPath, err); | 213 | console.error('Failed to load config', configPath, err); |
214 | }).then(watchConfig).catch((err) => { | ||
215 | console.error('Error when watching for config changes', configPath, err); | ||
193 | }); | 216 | }); |
194 | 217 | ||
195 | const rendererBaseUrl = getResourceUrl('../renderer/'); | 218 | const rendererBaseUrl = getResourceUrl('../renderer/'); |