diff options
Diffstat (limited to 'packages/main/src/infrastructure/config/impl/ConfigFile.ts')
-rw-r--r-- | packages/main/src/infrastructure/config/impl/ConfigFile.ts | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/packages/main/src/infrastructure/config/impl/ConfigFile.ts b/packages/main/src/infrastructure/config/impl/ConfigFile.ts index 684a827..4f0d4f0 100644 --- a/packages/main/src/infrastructure/config/impl/ConfigFile.ts +++ b/packages/main/src/infrastructure/config/impl/ConfigFile.ts | |||
@@ -32,6 +32,8 @@ import type { ReadConfigResult } from '../ConfigRepository.js'; | |||
32 | 32 | ||
33 | const log = getLogger('ConfigFile'); | 33 | const log = getLogger('ConfigFile'); |
34 | 34 | ||
35 | export const CONFIG_FILE_NAME = 'settings.json'; | ||
36 | |||
35 | export default class ConfigFile implements ConfigRepository { | 37 | export default class ConfigFile implements ConfigRepository { |
36 | private readonly configFilePath: string; | 38 | private readonly configFilePath: string; |
37 | 39 | ||
@@ -41,7 +43,7 @@ export default class ConfigFile implements ConfigRepository { | |||
41 | 43 | ||
42 | constructor( | 44 | constructor( |
43 | private readonly userDataDir: string, | 45 | private readonly userDataDir: string, |
44 | private readonly configFileName = 'settings.json', | 46 | private readonly configFileName = CONFIG_FILE_NAME, |
45 | ) { | 47 | ) { |
46 | this.configFilePath = path.join(userDataDir, configFileName); | 48 | this.configFilePath = path.join(userDataDir, configFileName); |
47 | } | 49 | } |
@@ -65,6 +67,9 @@ export default class ConfigFile implements ConfigRepository { | |||
65 | } | 67 | } |
66 | 68 | ||
67 | async writeConfig(contents: string): Promise<void> { | 69 | async writeConfig(contents: string): Promise<void> { |
70 | if (this.writingConfig) { | ||
71 | throw new Error('writeConfig cannot be called reentrantly'); | ||
72 | } | ||
68 | this.writingConfig = true; | 73 | this.writingConfig = true; |
69 | try { | 74 | try { |
70 | await writeFile(this.configFilePath, contents, 'utf8'); | 75 | await writeFile(this.configFilePath, contents, 'utf8'); |
@@ -95,7 +100,11 @@ export default class ConfigFile implements ConfigRepository { | |||
95 | ); | 100 | ); |
96 | return; | 101 | return; |
97 | } | 102 | } |
98 | throw error; | 103 | log.error( |
104 | 'Unexpected error while listening for config file changes', | ||
105 | error, | ||
106 | ); | ||
107 | return; | ||
99 | } | 108 | } |
100 | if ( | 109 | if ( |
101 | !this.writingConfig && | 110 | !this.writingConfig && |
@@ -111,20 +120,23 @@ export default class ConfigFile implements ConfigRepository { | |||
111 | } | 120 | } |
112 | }, throttleMs); | 121 | }, throttleMs); |
113 | 122 | ||
114 | const watcher = watch(this.userDataDir, { | 123 | const watcher = watch( |
115 | persistent: false, | 124 | this.userDataDir, |
116 | }); | 125 | { |
117 | 126 | persistent: false, | |
118 | watcher.on('change', (eventType, filename) => { | 127 | recursive: false, |
119 | if ( | 128 | }, |
120 | eventType === 'change' && | 129 | (_eventType, filename) => { |
121 | (filename === this.configFileName || filename === null) | 130 | if (filename === this.configFileName || filename === null) { |
122 | ) { | 131 | configChanged()?.catch((err) => { |
123 | configChanged()?.catch((err) => { | 132 | log.error( |
124 | log.error('Unhandled error while listening for config changes', err); | 133 | 'Unhandled error while listening for config changes', |
125 | }); | 134 | err, |
126 | } | 135 | ); |
127 | }); | 136 | }); |
137 | } | ||
138 | }, | ||
139 | ); | ||
128 | 140 | ||
129 | return () => { | 141 | return () => { |
130 | log.trace('Removing watcher for', this.configFilePath); | 142 | log.trace('Removing watcher for', this.configFilePath); |