Skip to content

Commit

Permalink
refactor: move json file functions to json file manager (#2383)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicole-obrien authored Apr 26, 2024
1 parent 6cb2343 commit a9092a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
29 changes: 29 additions & 0 deletions packages/desktop/lib/electron/managers/json-file.manager.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
35 changes: 6 additions & 29 deletions packages/desktop/lib/electron/processes/main.process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -652,7 +652,7 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt
}

function setBounds(): void {
const settings = <ISettings>loadJsonConfig(settingsFilename)
const settings = <ISettings>JsonFileManager.loadJsonFromFile(settingsFilename)

if (settings && settings.windowState && settings.windowState[windowName]) {
windowState = settings.windowState[windowName]
Expand All @@ -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 || <ISettings>{}
settings.windowState = settings.windowState || <IAppState>{}
settings.windowState[windowName] = windowState

saveJsonConfig(settingsFilename, settings)
JsonFileManager.saveJsonToFile(settingsFilename, settings)
}

function track(win: BrowserWindow): void {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a9092a1

Please sign in to comment.