feat: basic migration from legacy settings to settings/state V1

Signed-off-by: Sefa Eyeoglu <contact@scrumplex.net>
This commit is contained in:
Sefa Eyeoglu 2023-11-08 12:43:48 +01:00
parent 016c011223
commit 3643ecf7d6
No known key found for this signature in database
GPG key ID: E13DFD4B47127951
2 changed files with 34 additions and 9 deletions

View file

@ -6,7 +6,7 @@
import { mkdirSync, readFileSync, writeFileSync } from "fs";
import { dirname, join } from "path";
import type { Settings as TSettings, State as TState } from "shared/settings";
import type { SettingsV1, StateV1 } from "shared/settings";
import { SettingsStore } from "shared/utils/SettingsStore";
import { DATA_DIR, VENCORD_SETTINGS_FILE } from "./constants";
@ -14,17 +14,19 @@ import { DATA_DIR, VENCORD_SETTINGS_FILE } from "./constants";
const SETTINGS_FILE = join(DATA_DIR, "settings.json");
const STATE_FILE = join(DATA_DIR, "state.json");
function loadSettings<T extends object = any>(file: string, description: string) {
let settings = {} as T;
function readSettings(file: string, description: string) {
try {
const content = readFileSync(file, "utf8");
try {
settings = JSON.parse(content);
return JSON.parse(content);
} catch (err) {
console.error(`Failed to parse ${description}:`, err);
}
} catch {}
return {};
}
function createSettingsStore<T extends object = any>(file: string, settings: T) {
const store = new SettingsStore(settings);
store.addGlobalChangeListener(o => {
mkdirSync(dirname(file), { recursive: true });
@ -34,6 +36,27 @@ function loadSettings<T extends object = any>(file: string, description: string)
return store;
}
export const Settings = loadSettings<TSettings>(SETTINGS_FILE, "Vesktop settings.json");
export const State = loadSettings<TState>(STATE_FILE, "Vesktop state.json");
export const VencordSettings = loadSettings<any>(VENCORD_SETTINGS_FILE, "Vencord settings.json");
function loadVesktopSettings() {
// settingsData is any to allow for migration below
let settingsData = readSettings(SETTINGS_FILE, "Vesktop settings.json");
let stateData = readSettings(STATE_FILE, "Vesktop state.json") as StateV1;
// take stateDate from settings if we haven't migrated to settings v1 yet
if (settingsData.formatVersion ?? 0 < 1) {
stateData = settingsData as StateV1;
}
settingsData = settingsData as SettingsV1;
const Settings = createSettingsStore<SettingsV1>(SETTINGS_FILE, settingsData);
const State = createSettingsStore<StateV1>(STATE_FILE, stateData);
return {
Settings,
State
};
}
export const { Settings, State } = loadVesktopSettings();
export const VencordSettings = createSettingsStore<any>(
VENCORD_SETTINGS_FILE,
readSettings(VENCORD_SETTINGS_FILE, "Vencord settings.json")
);

View file

@ -6,7 +6,8 @@
import type { Rectangle } from "electron";
export interface Settings {
export interface SettingsV1 {
formatVersion: 1;
discordBranch?: "stable" | "canary" | "ptb";
vencordDir?: string;
transparencyOption?: "none" | "mica" | "tabbed" | "acrylic";
@ -27,7 +28,8 @@ export interface Settings {
splashBackground?: string;
}
export interface State {
export interface StateV1 {
formatVersion: 1;
maximized?: boolean;
minimized?: boolean;
windowBounds?: Rectangle;