From 3643ecf7d6e80d35b703942b6e197bb2fcadfdca Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Wed, 8 Nov 2023 12:43:48 +0100 Subject: [PATCH] feat: basic migration from legacy settings to settings/state V1 Signed-off-by: Sefa Eyeoglu --- src/main/settings.ts | 37 ++++++++++++++++++++++++++++++------- src/shared/settings.d.ts | 6 ++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/main/settings.ts b/src/main/settings.ts index d59d851..cc2a2d9 100644 --- a/src/main/settings.ts +++ b/src/main/settings.ts @@ -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(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(file: string, settings: T) { const store = new SettingsStore(settings); store.addGlobalChangeListener(o => { mkdirSync(dirname(file), { recursive: true }); @@ -34,6 +36,27 @@ function loadSettings(file: string, description: string) return store; } -export const Settings = loadSettings(SETTINGS_FILE, "Vesktop settings.json"); -export const State = loadSettings(STATE_FILE, "Vesktop state.json"); -export const VencordSettings = loadSettings(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(SETTINGS_FILE, settingsData); + const State = createSettingsStore(STATE_FILE, stateData); + return { + Settings, + State + }; +} + +export const { Settings, State } = loadVesktopSettings(); +export const VencordSettings = createSettingsStore( + VENCORD_SETTINGS_FILE, + readSettings(VENCORD_SETTINGS_FILE, "Vencord settings.json") +); diff --git a/src/shared/settings.d.ts b/src/shared/settings.d.ts index 39914cf..efc41cb 100644 --- a/src/shared/settings.d.ts +++ b/src/shared/settings.d.ts @@ -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;