-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathstorage.ts
More file actions
37 lines (33 loc) · 915 Bytes
/
Copy pathstorage.ts
File metadata and controls
37 lines (33 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { app } from 'electron';
import fs from 'node:fs';
import path from 'node:path';
import logger from '../logger';
const dataPath = app.getPath('userData');
const FILEPATH = path.join(dataPath, 'config.json');
export const writeData = (
key: string,
value: unknown,
filePath: string = FILEPATH
) => {
const contents = parseData(filePath);
contents[key] = value;
fs.writeFileSync(filePath, JSON.stringify(contents));
};
export const readData = <T>(
key: string,
filePath: string = FILEPATH
): T | undefined => {
const contents = parseData(filePath);
return contents[key] as T;
};
const parseData = (filePath: string) => {
const defaultData = {};
try {
const data = fs.readFileSync(filePath, 'utf8');
return JSON.parse(data);
} catch (error) {
logger.warn('Failed to read config file', error);
logger.warn('Creating new config file');
return defaultData;
}
};