mirror of
https://github.com/YuzuZensai/VRC-Circle.git
synced 2026-09-14 03:10:01 +00:00
♻️ refactor: extract jsonFile helper for file-backed JSON stores
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { app } from "electron";
|
import { app } from "electron";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { copyFileSync, existsSync, readFileSync, rmSync } from "node:fs";
|
import { copyFileSync, existsSync, rmSync } from "node:fs";
|
||||||
import { writeFileAtomicSync } from "../lib/atomicFile";
|
import { jsonFile } from "../lib/jsonFile";
|
||||||
import type { Account, AccountsState } from "../../shared/types/auth";
|
import type { Account, AccountsState } from "../../shared/types/auth";
|
||||||
|
|
||||||
const dir = () => join(app.getPath("userData"), "sessions");
|
const dir = () => join(app.getPath("userData"), "sessions");
|
||||||
@@ -15,17 +15,9 @@ interface Registry {
|
|||||||
accounts: Account[];
|
accounts: Account[];
|
||||||
}
|
}
|
||||||
|
|
||||||
function read(): Registry {
|
const registry = jsonFile<Registry>(registryPath, () => ({ active: null, accounts: [] }));
|
||||||
try {
|
const read = registry.read;
|
||||||
return JSON.parse(readFileSync(registryPath(), "utf8")) as Registry;
|
const write = registry.write;
|
||||||
} catch {
|
|
||||||
return { active: null, accounts: [] };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function write(reg: Registry): void {
|
|
||||||
writeFileAtomicSync(registryPath(), JSON.stringify(reg, null, 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function listAccounts(): AccountsState {
|
export function listAccounts(): AccountsState {
|
||||||
const reg = read();
|
const reg = read();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { app } from "electron";
|
import { app } from "electron";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { existsSync, readFileSync } from "node:fs";
|
import { existsSync } from "node:fs";
|
||||||
import { writeFileAtomicSync } from "../lib/atomicFile";
|
import { jsonFile } from "../lib/jsonFile";
|
||||||
import type { AppConfig, PreferredRegion } from "../../shared/types/appConfig";
|
import type { AppConfig, PreferredRegion } from "../../shared/types/appConfig";
|
||||||
import { detectedGamePath } from "../game/steam";
|
import { detectedGamePath } from "../game/steam";
|
||||||
|
|
||||||
@@ -10,12 +10,15 @@ const path = () => join(app.getPath("userData"), "app-config.json");
|
|||||||
type StoredConfig = Omit<AppConfig, "version" | "detectedGamePath">;
|
type StoredConfig = Omit<AppConfig, "version" | "detectedGamePath">;
|
||||||
const DEFAULTS: StoredConfig = { gamePath: null, preferredRegion: "auto" };
|
const DEFAULTS: StoredConfig = { gamePath: null, preferredRegion: "auto" };
|
||||||
|
|
||||||
function readStored(): StoredConfig {
|
const configFile = jsonFile<StoredConfig>(
|
||||||
try {
|
path,
|
||||||
return { ...DEFAULTS, ...(JSON.parse(readFileSync(path(), "utf8")) as Partial<StoredConfig>) };
|
() => ({ ...DEFAULTS }),
|
||||||
} catch {
|
(raw) => ({ ...DEFAULTS, ...(raw as Partial<StoredConfig>) }),
|
||||||
return { ...DEFAULTS };
|
);
|
||||||
}
|
const readStored = configFile.read;
|
||||||
|
|
||||||
|
function writeStored(next: StoredConfig): void {
|
||||||
|
configFile.write(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
function withDerived(stored: StoredConfig): AppConfig {
|
function withDerived(stored: StoredConfig): AppConfig {
|
||||||
@@ -32,7 +35,7 @@ export function preferredRegion(): PreferredRegion {
|
|||||||
|
|
||||||
export function setPreferredRegion(region: PreferredRegion): AppConfig {
|
export function setPreferredRegion(region: PreferredRegion): AppConfig {
|
||||||
const next: StoredConfig = { ...readStored(), preferredRegion: region };
|
const next: StoredConfig = { ...readStored(), preferredRegion: region };
|
||||||
writeFileAtomicSync(path(), JSON.stringify(next, null, 2));
|
writeStored(next);
|
||||||
return withDerived(next);
|
return withDerived(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,6 +49,6 @@ export function setGamePath(gamePath: string | null): AppConfig {
|
|||||||
throw new Error("That path doesn't exist on disk.");
|
throw new Error("That path doesn't exist on disk.");
|
||||||
}
|
}
|
||||||
const next: StoredConfig = { ...readStored(), gamePath: trimmed };
|
const next: StoredConfig = { ...readStored(), gamePath: trimmed };
|
||||||
writeFileAtomicSync(path(), JSON.stringify(next, null, 2));
|
writeStored(next);
|
||||||
return withDerived(next);
|
return withDerived(next);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { app } from "electron";
|
import { app } from "electron";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { readFileSync } from "node:fs";
|
import { jsonFile } from "../lib/jsonFile";
|
||||||
import { writeFileAtomicSync } from "../lib/atomicFile";
|
|
||||||
import { logger } from "../debug/logger";
|
import { logger } from "../debug/logger";
|
||||||
import type {
|
import type {
|
||||||
EnhancementId,
|
EnhancementId,
|
||||||
@@ -17,17 +16,9 @@ const storePath = () => join(app.getPath("userData"), "enhancements.json");
|
|||||||
|
|
||||||
type Prefs = Partial<Record<EnhancementId, boolean>>;
|
type Prefs = Partial<Record<EnhancementId, boolean>>;
|
||||||
|
|
||||||
function readPrefs(): Prefs {
|
const prefsFile = jsonFile<Prefs>(storePath, () => ({}));
|
||||||
try {
|
const readPrefs = prefsFile.read;
|
||||||
return JSON.parse(readFileSync(storePath(), "utf8")) as Prefs;
|
const writePrefs = prefsFile.write;
|
||||||
} catch {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function writePrefs(prefs: Prefs): void {
|
|
||||||
writeFileAtomicSync(storePath(), JSON.stringify(prefs, null, 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
function screenshotState(): EnhancementState {
|
function screenshotState(): EnhancementState {
|
||||||
const s = screenshot.status();
|
const s = screenshot.status();
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { readFileSync } from "node:fs";
|
||||||
|
import { writeFileAtomicSync } from "./atomicFile";
|
||||||
|
|
||||||
|
export function jsonFile<T>(
|
||||||
|
path: () => string,
|
||||||
|
fallback: () => T,
|
||||||
|
parse: (raw: unknown) => T = (raw) => raw as T,
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
read(): T {
|
||||||
|
try {
|
||||||
|
return parse(JSON.parse(readFileSync(path(), "utf8")));
|
||||||
|
} catch {
|
||||||
|
return fallback();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
write(value: T): void {
|
||||||
|
writeFileAtomicSync(path(), JSON.stringify(value, null, 2));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
+4
-13
@@ -1,19 +1,13 @@
|
|||||||
import { app, screen, type BrowserWindow, type Rectangle } from "electron";
|
import { app, screen, type BrowserWindow, type Rectangle } from "electron";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { readFileSync } from "node:fs";
|
import { jsonFile } from "./lib/jsonFile";
|
||||||
import { writeFileAtomicSync } from "./lib/atomicFile";
|
|
||||||
|
|
||||||
type Bounds = Rectangle & { maximized: boolean };
|
type Bounds = Rectangle & { maximized: boolean };
|
||||||
|
|
||||||
const path = () => join(app.getPath("userData"), "window-state.json");
|
const path = () => join(app.getPath("userData"), "window-state.json");
|
||||||
|
|
||||||
function read(): Partial<Bounds> | null {
|
const stateFile = jsonFile<Partial<Bounds> | null>(path, () => null);
|
||||||
try {
|
const read = stateFile.read;
|
||||||
return JSON.parse(readFileSync(path(), "utf8")) as Partial<Bounds>;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function onScreen(b: Rectangle): boolean {
|
function onScreen(b: Rectangle): boolean {
|
||||||
return screen.getAllDisplays().some((d) => {
|
return screen.getAllDisplays().some((d) => {
|
||||||
@@ -47,10 +41,7 @@ export function trackBounds(win: BrowserWindow): void {
|
|||||||
|
|
||||||
const save = (): void => {
|
const save = (): void => {
|
||||||
if (win.isDestroyed()) return;
|
if (win.isDestroyed()) return;
|
||||||
writeFileAtomicSync(
|
stateFile.write({ ...normalBounds, maximized: win.isMaximized() });
|
||||||
path(),
|
|
||||||
JSON.stringify({ ...normalBounds, maximized: win.isMaximized() }, null, 2),
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const remember = (): void => {
|
const remember = (): void => {
|
||||||
|
|||||||
Reference in New Issue
Block a user