project-client/src/main/settings.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-04-10 05:49:50 +09:00
/*
* SPDX-License-Identifier: GPL-3.0
2023-07-14 02:03:13 +09:00
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
2023-04-10 05:49:50 +09:00
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
2023-06-22 00:24:50 +09:00
import { mkdirSync, readFileSync, writeFileSync } from "fs";
import { dirname, 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);
2023-06-22 00:24:50 +09:00
store.addGlobalChangeListener(o => {
mkdirSync(dirname(file), { recursive: true });
writeFileSync(file, JSON.stringify(o, null, 4));
});
2023-04-10 08:04:41 +09:00
return store;
}
2023-07-14 02:03:13 +09:00
export const Settings = loadSettings<TSettings>(SETTINGS_FILE, "Vesktop");
2023-04-10 08:04:41 +09:00
export const VencordSettings = loadSettings<any>(VENCORD_SETTINGS_FILE, "Vencord");