From 713a30ed725fe500bf6cea6b8faf5b476e76687e Mon Sep 17 00:00:00 2001 From: Frank Noirot Date: Thu, 22 Aug 2024 19:51:26 -0400 Subject: [PATCH] Fix initial default app settings behavior when the user has no settings yet (#3601) Fix initial default app settings behavior when the user has no settings yet. Co-authored-by: Jess Frazelle --- src/lib/desktop.ts | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/lib/desktop.ts b/src/lib/desktop.ts index 842db440b..0190935cd 100644 --- a/src/lib/desktop.ts +++ b/src/lib/desktop.ts @@ -441,28 +441,34 @@ export const readProjectSettingsFile = async ( return configObj } +/** + * Read the app settings file, or creates an initial one if it doesn't exist. + */ export const readAppSettingsFile = async () => { let settingsPath = await getAppSettingsFilePath() - try { - await window.electron.stat(settingsPath) - } catch (e) { - if (e === 'ENOENT') { - const config = defaultAppSettings() - if (err(config)) return Promise.reject(config) - if (!config.settings?.app) - return Promise.reject(new Error('config.app is falsey')) - config.settings.app.project_directory = await getInitialDefaultDir() - return config + // The file exists, read it and parse it. + if (window.electron.exists(settingsPath)) { + const configToml = await window.electron.readFile(settingsPath) + const configObj = parseAppSettings(configToml) + if (err(configObj)) { + return Promise.reject(configObj) } - } - const configToml = await window.electron.readFile(settingsPath) - const configObj = parseAppSettings(configToml) - if (err(configObj)) { - return Promise.reject(configObj) + + return configObj } - return configObj + // The file doesn't exist, create a new one. + // This defaultAppConfig is truly an empty object every time. + const defaultAppConfig = defaultAppSettings() + if (err(defaultAppConfig)) { + return Promise.reject(defaultAppConfig) + } + const initialDirConfig: DeepPartial = { + settings: { project: { directory: await getInitialDefaultDir() } }, + } + const config = Object.assign(defaultAppConfig, initialDirConfig) + return config } export const writeAppSettingsFile = async (tomlStr: string) => {