feat: basic migration from legacy settings to settings/state V1
Signed-off-by: Sefa Eyeoglu <contact@scrumplex.net>
This commit is contained in:
parent
016c011223
commit
3643ecf7d6
2 changed files with 34 additions and 9 deletions
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
import { mkdirSync, readFileSync, writeFileSync } from "fs";
|
import { mkdirSync, readFileSync, writeFileSync } from "fs";
|
||||||
import { dirname, join } from "path";
|
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 { SettingsStore } from "shared/utils/SettingsStore";
|
||||||
|
|
||||||
import { DATA_DIR, VENCORD_SETTINGS_FILE } from "./constants";
|
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 SETTINGS_FILE = join(DATA_DIR, "settings.json");
|
||||||
const STATE_FILE = join(DATA_DIR, "state.json");
|
const STATE_FILE = join(DATA_DIR, "state.json");
|
||||||
|
|
||||||
function loadSettings<T extends object = any>(file: string, description: string) {
|
function readSettings(file: string, description: string) {
|
||||||
let settings = {} as T;
|
|
||||||
try {
|
try {
|
||||||
const content = readFileSync(file, "utf8");
|
const content = readFileSync(file, "utf8");
|
||||||
try {
|
try {
|
||||||
settings = JSON.parse(content);
|
return JSON.parse(content);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`Failed to parse ${description}:`, err);
|
console.error(`Failed to parse ${description}:`, err);
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createSettingsStore<T extends object = any>(file: string, settings: T) {
|
||||||
const store = new SettingsStore(settings);
|
const store = new SettingsStore(settings);
|
||||||
store.addGlobalChangeListener(o => {
|
store.addGlobalChangeListener(o => {
|
||||||
mkdirSync(dirname(file), { recursive: true });
|
mkdirSync(dirname(file), { recursive: true });
|
||||||
|
@ -34,6 +36,27 @@ function loadSettings<T extends object = any>(file: string, description: string)
|
||||||
return store;
|
return store;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Settings = loadSettings<TSettings>(SETTINGS_FILE, "Vesktop settings.json");
|
function loadVesktopSettings() {
|
||||||
export const State = loadSettings<TState>(STATE_FILE, "Vesktop state.json");
|
// settingsData is any to allow for migration below
|
||||||
export const VencordSettings = loadSettings<any>(VENCORD_SETTINGS_FILE, "Vencord settings.json");
|
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")
|
||||||
|
);
|
||||||
|
|
6
src/shared/settings.d.ts
vendored
6
src/shared/settings.d.ts
vendored
|
@ -6,7 +6,8 @@
|
||||||
|
|
||||||
import type { Rectangle } from "electron";
|
import type { Rectangle } from "electron";
|
||||||
|
|
||||||
export interface Settings {
|
export interface SettingsV1 {
|
||||||
|
formatVersion: 1;
|
||||||
discordBranch?: "stable" | "canary" | "ptb";
|
discordBranch?: "stable" | "canary" | "ptb";
|
||||||
vencordDir?: string;
|
vencordDir?: string;
|
||||||
transparencyOption?: "none" | "mica" | "tabbed" | "acrylic";
|
transparencyOption?: "none" | "mica" | "tabbed" | "acrylic";
|
||||||
|
@ -27,7 +28,8 @@ export interface Settings {
|
||||||
splashBackground?: string;
|
splashBackground?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface State {
|
export interface StateV1 {
|
||||||
|
formatVersion: 1;
|
||||||
maximized?: boolean;
|
maximized?: boolean;
|
||||||
minimized?: boolean;
|
minimized?: boolean;
|
||||||
windowBounds?: Rectangle;
|
windowBounds?: Rectangle;
|
||||||
|
|
Reference in a new issue