project-client/src/main/settings.ts

35 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-04-10 05:49:50 +09:00
/*
* SPDX-License-Identifier: GPL-3.0
* Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
2023-04-04 11:17:38 +09:00
import { readFileSync, writeFileSync } from "fs";
import { join } from "path";
2023-04-09 07:49:47 +09:00
import type { Settings as TSettings } from "shared/settings";
2023-04-10 08:16:44 +09:00
import { SettingsStore } from "shared/utils/SettingsStore";
2023-04-10 05:49:50 +09:00
import { DATA_DIR, VENCORD_SETTINGS_FILE } from "./constants";
2023-04-04 11:17:38 +09:00
const SETTINGS_FILE = join(DATA_DIR, "settings.json");
function loadSettings<T extends object = any>(file: string, name: string) {
let settings = {} as T;
2023-04-04 11:17:38 +09:00
try {
const content = readFileSync(file, "utf8");
try {
settings = JSON.parse(content);
} catch (err) {
console.error(`Failed to parse ${name} settings.json:`, err);
}
2023-04-10 05:49:50 +09:00
} catch {}
2023-04-04 11:17:38 +09:00
2023-04-10 08:04:41 +09:00
const store = new SettingsStore(settings);
store.addGlobalChangeListener(o => writeFileSync(file, JSON.stringify(o, null, 4)));
2023-04-10 08:04:41 +09:00
return store;
}
2023-04-10 08:04:41 +09:00
export const Settings = loadSettings<TSettings>(SETTINGS_FILE, "Vencord Desktop");
export const VencordSettings = loadSettings<any>(VENCORD_SETTINGS_FILE, "Vencord");