From a9092a1adcf0868e869b1d7cf5f99a93106f843b Mon Sep 17 00:00:00 2001 From: Nicole O'Brien Date: Fri, 26 Apr 2024 18:26:46 +0100 Subject: [PATCH] refactor: move json file functions to json file manager (#2383) --- .../electron/managers/json-file.manager.ts | 29 +++++++++++++++ .../lib/electron/processes/main.process.ts | 35 ++++--------------- 2 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 packages/desktop/lib/electron/managers/json-file.manager.ts diff --git a/packages/desktop/lib/electron/managers/json-file.manager.ts b/packages/desktop/lib/electron/managers/json-file.manager.ts new file mode 100644 index 0000000000..746cd42dc9 --- /dev/null +++ b/packages/desktop/lib/electron/managers/json-file.manager.ts @@ -0,0 +1,29 @@ +import { IError } from '@core/error/interfaces' +import { app } from 'electron' +import fs from 'fs' +import path from 'path' + +export class JsonFileManager { + public static saveJsonToFile(filename: string, data: object): void { + try { + fs.writeFileSync(JsonFileManager.getFilePath(filename), JSON.stringify(data)) + } catch (err) { + console.error(err) + } + } + + public static loadJsonFromFile(filename: string): object | undefined { + try { + return JSON.parse(fs.readFileSync(JsonFileManager.getFilePath(filename)).toString()) + } catch (err) { + if (!(err as IError).message?.includes('ENOENT')) { + console.error(err) + } + } + } + + private static getFilePath(filename: string): string { + const userDataPath = app.getPath('userData') + return path.join(userDataPath, filename) + } +} diff --git a/packages/desktop/lib/electron/processes/main.process.ts b/packages/desktop/lib/electron/processes/main.process.ts index 5ed9625924..dcc90c83fe 100644 --- a/packages/desktop/lib/electron/processes/main.process.ts +++ b/packages/desktop/lib/electron/processes/main.process.ts @@ -36,7 +36,7 @@ import { getMachineId } from '../utils/os.utils' import { registerPowerMonitorListeners } from '../listeners' import ThirdPartyAppManager from '../managers/third-party-profiles.manager' import { ITransakWindowData } from '@core/app/interfaces' -import { IError } from '@core/error' +import { JsonFileManager } from '../managers/json-file.manager' export let appIsReady = false @@ -652,7 +652,7 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt } function setBounds(): void { - const settings = loadJsonConfig(settingsFilename) + const settings = JsonFileManager.loadJsonFromFile(settingsFilename) if (settings && settings.windowState && settings.windowState[windowName]) { windowState = settings.windowState[windowName] @@ -666,13 +666,13 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt windowState = window.getBounds() as IAppState } - let settings = loadJsonConfig(settingsFilename) as ISettings + let settings = JsonFileManager.loadJsonFromFile(settingsFilename) as ISettings settings = settings || {} settings.windowState = settings.windowState || {} settings.windowState[windowName] = windowState - saveJsonConfig(settingsFilename, settings) + JsonFileManager.saveJsonToFile(settingsFilename, settings) } function track(win: BrowserWindow): void { @@ -712,37 +712,14 @@ interface IAppState { function updateSettings(data: object): void { const filename = 'settings.json' - const config = loadJsonConfig(filename) + const config = JsonFileManager.loadJsonFromFile(filename) /** * CAUTION: We must be careful saving properties to this file, as * once we decide to save it there then it will be there forever * even if the name changes later. */ - saveJsonConfig(filename, { ...config, ...data }) -} - -function saveJsonConfig(filename: string, data: object): void { - try { - fs.writeFileSync(getJsonConfig(filename), JSON.stringify(data)) - } catch (err) { - console.error(err) - } -} - -function loadJsonConfig(filename: string): object | undefined { - try { - return JSON.parse(fs.readFileSync(getJsonConfig(filename)).toString()) - } catch (err) { - if (!(err as IError).message?.includes('ENOENT')) { - console.error(err) - } - } -} - -function getJsonConfig(filename: string): string { - const userDataPath = app.getPath('userData') - return path.join(userDataPath, filename) + JsonFileManager.saveJsonToFile(filename, { ...config, ...data }) } export function updateAppVersionDetails(details: object): void {