diff options
author | 2021-12-26 13:16:55 +0100 | |
---|---|---|
committer | 2021-12-26 13:16:55 +0100 | |
commit | ff9eb579ee93432660182ecde52d753bfcd5e4a1 (patch) | |
tree | c687e88f9cd10d3ccd2570d7363901111a5c71c0 /packages/main/src/index.ts | |
parent | refactor: Improved config reloading (diff) | |
download | sophie-ff9eb579ee93432660182ecde52d753bfcd5e4a1.tar.gz sophie-ff9eb579ee93432660182ecde52d753bfcd5e4a1.tar.zst sophie-ff9eb579ee93432660182ecde52d753bfcd5e4a1.zip |
feat: Switch to json5 config format
Diffstat (limited to 'packages/main/src/index.ts')
-rw-r--r-- | packages/main/src/index.ts | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index 605c17c..8055ce2 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts | |||
@@ -32,6 +32,7 @@ import { | |||
32 | watch, | 32 | watch, |
33 | writeFile, | 33 | writeFile, |
34 | } from 'fs/promises'; | 34 | } from 'fs/promises'; |
35 | import JSON5 from 'json5'; | ||
35 | import { autorun } from 'mobx'; | 36 | import { autorun } from 'mobx'; |
36 | import { | 37 | import { |
37 | applySnapshot, | 38 | applySnapshot, |
@@ -127,7 +128,7 @@ nativeTheme.on('updated', () => { | |||
127 | }); | 128 | }); |
128 | 129 | ||
129 | const userDataDir = app.getPath('userData'); | 130 | const userDataDir = app.getPath('userData'); |
130 | const configFileName = 'config.json'; | 131 | const configFileName = 'config.json5'; |
131 | const configPath = join(userDataDir, configFileName); | 132 | const configPath = join(userDataDir, configFileName); |
132 | let loadingConfig = false; | 133 | let loadingConfig = false; |
133 | let savingConfig = false; | 134 | let savingConfig = false; |
@@ -146,7 +147,7 @@ async function loadConfig(): Promise<void> { | |||
146 | } | 147 | } |
147 | let configSnapshot: unknown; | 148 | let configSnapshot: unknown; |
148 | try { | 149 | try { |
149 | configSnapshot = JSON.parse(configStr); | 150 | configSnapshot = JSON5.parse(configStr); |
150 | } catch (err) { | 151 | } catch (err) { |
151 | console.error('Invalid config file', configPath, err); | 152 | console.error('Invalid config file', configPath, err); |
152 | return; | 153 | return; |
@@ -162,7 +163,9 @@ async function loadConfig(): Promise<void> { | |||
162 | } | 163 | } |
163 | 164 | ||
164 | async function saveConfig(configSnapshot: ConfigSnapshotOut): Promise<void> { | 165 | async function saveConfig(configSnapshot: ConfigSnapshotOut): Promise<void> { |
165 | const configJson = JSON.stringify(configSnapshot, null, 2); | 166 | const configJson = JSON5.stringify(configSnapshot, { |
167 | space: 2, | ||
168 | }); | ||
166 | savingConfig = true; | 169 | savingConfig = true; |
167 | try { | 170 | try { |
168 | await writeFile(configPath, configJson, 'utf8'); | 171 | await writeFile(configPath, configJson, 'utf8'); |