|
| 1 | +/** |
| 2 | + * Persistent user config storage |
| 3 | + * Reads/writes config.json in the platform-appropriate userData directory |
| 4 | + */ |
| 5 | + |
| 6 | +import { app } from 'electron'; |
| 7 | +import { readFileSync, writeFileSync, mkdirSync } from 'fs'; |
| 8 | +import { join } from 'path'; |
| 9 | +import { createLogger } from '../../utils/logger'; |
| 10 | + |
| 11 | +const logger = createLogger('ConfigStore'); |
| 12 | + |
| 13 | +export interface UserConfig { |
| 14 | + outputPath: string | null; |
| 15 | +} |
| 16 | + |
| 17 | +const defaults: UserConfig = { |
| 18 | + outputPath: null, |
| 19 | +}; |
| 20 | + |
| 21 | +function configPath(): string { |
| 22 | + return join(app.getPath('userData'), 'config.json'); |
| 23 | +} |
| 24 | + |
| 25 | +export function loadConfig(): UserConfig { |
| 26 | + try { |
| 27 | + const raw = readFileSync(configPath(), 'utf-8'); |
| 28 | + const parsed = JSON.parse(raw); |
| 29 | + return { ...defaults, ...parsed }; |
| 30 | + } catch { |
| 31 | + return { ...defaults }; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export function saveConfig(config: UserConfig): void { |
| 36 | + try { |
| 37 | + const dir = app.getPath('userData'); |
| 38 | + mkdirSync(dir, { recursive: true }); |
| 39 | + writeFileSync(configPath(), JSON.stringify(config, null, 2), 'utf-8'); |
| 40 | + } catch (error) { |
| 41 | + logger.error('Failed to save config:', error); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export function getConfigValue<K extends keyof UserConfig>(key: K): UserConfig[K] { |
| 46 | + return loadConfig()[key]; |
| 47 | +} |
| 48 | + |
| 49 | +export function setConfigValue<K extends keyof UserConfig>(key: K, value: UserConfig[K]): void { |
| 50 | + const config = loadConfig(); |
| 51 | + config[key] = value; |
| 52 | + saveConfig(config); |
| 53 | +} |
0 commit comments