-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move json file functions to json file manager (#2383)
- Loading branch information
1 parent
6cb2343
commit a9092a1
Showing
2 changed files
with
35 additions
and
29 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
packages/desktop/lib/electron/managers/json-file.manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters