fix(Settings): Path user config file with any missing default values
@@ -2,6 +2,7 @@ import { createStore, ObservableMap } from '@stencil/store'
import { app } from 'electron'
import fs from 'fs'
import path from 'path'
+import { UnprotectedStoreKeys } from '../../preload/stores'
import { AppConfigStore } from '../../preload/types'
const storeName = 'storeUnprotected.json'
@@ -26,6 +27,40 @@ export const readUnprotectedStore = (): AppConfigStore => {
return defaultConfigStore
}
+export const writeUnprotectedStore = (store: AppConfigStore): void => {
+ fs.writeFileSync(unprotectedStorePath, JSON.stringify(store))
+}
+
+export const resetUnprotectedStore = (): void => {
+ fs.writeFileSync(unprotectedStorePath, JSON.stringify(defaultConfigStore))
+}
+
+/**
+ * If there are any default configuration settings missing from the user settings
+ * file on disk, patch the settings file with the default values.
+ */
+const setMissingConfigValues = (currentConfig: AppConfigStore) => {
+ const defaultStoreKeys = Object.keys(
+ defaultConfigStore
+ ) as UnprotectedStoreKeys[]
+ const currentStoreKeys = Object.keys(currentConfig) as UnprotectedStoreKeys[]
+
+ const missingConfig = defaultStoreKeys.reduce(
+ (config: Partial<AppConfigStore>, key) =>
+ currentStoreKeys.includes(key)
+ ? config
+ : { ...config, [key]: defaultConfigStore[key] },
+ {}
+ )
+
+ if (Object.keys(missingConfig).length > 0) {
+ writeUnprotectedStore({
+ ...currentConfig,
+ ...missingConfig,
+ })
+ }
+}
+
const initAppConfigStore = () => {
let config = defaultConfigStore
if (fs.existsSync(unprotectedStorePath)) {
@@ -38,16 +73,10 @@ const initAppConfigStore = () => {
writeUnprotectedStore(store.state)
})
+ setMissingConfigValues(config)
+
return store
}
export let unprotectedStore: ObservableMap<AppConfigStore> =
initAppConfigStore()
-
-export const writeUnprotectedStore = (store: AppConfigStore): void => {
- fs.writeFileSync(unprotectedStorePath, JSON.stringify(store))
-}
-
-export const resetUnprotectedStore = (): void => {
- fs.writeFileSync(unprotectedStorePath, JSON.stringify(defaultConfigStore))
-}
