// electron/modules/config.ts - 配置管理模块 import * as fs from 'fs/promises' import * as fsSync from 'fs' import * as path from 'path' import { app } from 'electron' import type { InstallHistoryItem, GitMirrorType, NodejsMirrorType } from './types' import { DEFAULT_GIT_MIRROR, DEFAULT_NODEJS_MIRROR } from './constants' import logger from './logger' interface WindowBounds { x: number y: number width: number height: number } interface AppConfig { installHistory: InstallHistoryItem[] theme: 'light' | 'dark' | 'system' language: 'zh-CN' | 'en-US' windowBounds?: WindowBounds gitMirror: GitMirrorType nodejsMirror: NodejsMirrorType } const DEFAULT_CONFIG: AppConfig = { installHistory: [], theme: 'system', language: 'zh-CN', gitMirror: DEFAULT_GIT_MIRROR, nodejsMirror: DEFAULT_NODEJS_MIRROR } let configPath: string = '' let currentConfig: AppConfig = { ...DEFAULT_CONFIG } /** * 初始化配置 */ export async function initConfig(): Promise { configPath = path.join(app.getPath('userData'), 'config.json') await loadConfig() } /** * 加载配置 */ async function loadConfig(): Promise { try { if (fsSync.existsSync(configPath)) { const data = await fs.readFile(configPath, 'utf-8') const loaded = JSON.parse(data) as Partial currentConfig = { ...DEFAULT_CONFIG, ...loaded } logger.info('配置加载成功') } } catch (error) { logger.error('加载配置失败,使用默认配置', error) currentConfig = { ...DEFAULT_CONFIG } } } /** * 保存配置(异步,不阻塞主进程) */ function saveConfig(): void { fs.writeFile(configPath, JSON.stringify(currentConfig, null, 2), 'utf-8') .then(() => logger.debug('配置保存成功')) .catch((error) => logger.error('保存配置失败', error)) } /** * 获取主题配置 */ export function getThemeConfig(): 'light' | 'dark' | 'system' { return currentConfig.theme } /** * 保存主题配置 */ export function saveThemeConfig(theme: 'light' | 'dark' | 'system'): void { currentConfig.theme = theme saveConfig() } /** * 获取语言配置 */ export function getLanguageConfig(): 'zh-CN' | 'en-US' { return currentConfig.language } /** * 保存语言配置 */ export function saveLanguageConfig(language: 'zh-CN' | 'en-US'): void { currentConfig.language = language saveConfig() } /** * 添加安装历史 */ export function addInstallHistory(item: Omit): void { const historyItem: InstallHistoryItem = { ...item, timestamp: Date.now() } currentConfig.installHistory.unshift(historyItem) // 只保留最近 100 条记录 if (currentConfig.installHistory.length > 100) { currentConfig.installHistory = currentConfig.installHistory.slice(0, 100) } saveConfig() } /** * 获取安装历史 */ export function getInstallHistory(limit = 20): InstallHistoryItem[] { return currentConfig.installHistory.slice(0, limit) } /** * 清除安装历史 */ export function clearInstallHistory(): void { currentConfig.installHistory = [] saveConfig() } /** * 获取窗口位置配置 */ export function getWindowBounds(): WindowBounds | undefined { return currentConfig.windowBounds } /** * 保存窗口位置配置 */ export function saveWindowBounds(bounds: WindowBounds): void { currentConfig.windowBounds = bounds saveConfig() } /** * 获取 Git 镜像配置 */ export function getGitMirrorFromConfig(): GitMirrorType { return currentConfig.gitMirror } /** * 保存 Git 镜像配置 */ export function saveGitMirrorConfig(mirror: GitMirrorType): void { currentConfig.gitMirror = mirror saveConfig() } /** * 获取 Node.js 镜像配置 */ export function getNodejsMirrorFromConfig(): NodejsMirrorType { return currentConfig.nodejsMirror } /** * 保存 Node.js 镜像配置 */ export function saveNodejsMirrorConfig(mirror: NodejsMirrorType): void { currentConfig.nodejsMirror = mirror saveConfig() }