config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // electron/modules/config.ts - 配置管理模块
  2. import * as fs from 'fs/promises'
  3. import * as fsSync from 'fs'
  4. import * as path from 'path'
  5. import { app } from 'electron'
  6. import type { InstallHistoryItem, GitMirrorType, NodejsMirrorType } from './types'
  7. import { DEFAULT_GIT_MIRROR, DEFAULT_NODEJS_MIRROR } from './constants'
  8. import logger from './logger'
  9. interface WindowBounds {
  10. x: number
  11. y: number
  12. width: number
  13. height: number
  14. }
  15. interface AppConfig {
  16. installHistory: InstallHistoryItem[]
  17. theme: 'light' | 'dark' | 'system'
  18. language: 'zh-CN' | 'en-US'
  19. windowBounds?: WindowBounds
  20. gitMirror: GitMirrorType
  21. nodejsMirror: NodejsMirrorType
  22. }
  23. const DEFAULT_CONFIG: AppConfig = {
  24. installHistory: [],
  25. theme: 'system',
  26. language: 'zh-CN',
  27. gitMirror: DEFAULT_GIT_MIRROR,
  28. nodejsMirror: DEFAULT_NODEJS_MIRROR
  29. }
  30. let configPath: string = ''
  31. let currentConfig: AppConfig = { ...DEFAULT_CONFIG }
  32. /**
  33. * 初始化配置
  34. */
  35. export async function initConfig(): Promise<void> {
  36. configPath = path.join(app.getPath('userData'), 'config.json')
  37. await loadConfig()
  38. }
  39. /**
  40. * 加载配置
  41. */
  42. async function loadConfig(): Promise<void> {
  43. try {
  44. if (fsSync.existsSync(configPath)) {
  45. const data = await fs.readFile(configPath, 'utf-8')
  46. const loaded = JSON.parse(data) as Partial<AppConfig>
  47. currentConfig = {
  48. ...DEFAULT_CONFIG,
  49. ...loaded
  50. }
  51. logger.info('配置加载成功')
  52. }
  53. } catch (error) {
  54. logger.error('加载配置失败,使用默认配置', error)
  55. currentConfig = { ...DEFAULT_CONFIG }
  56. }
  57. }
  58. /**
  59. * 保存配置(异步,不阻塞主进程)
  60. */
  61. function saveConfig(): void {
  62. fs.writeFile(configPath, JSON.stringify(currentConfig, null, 2), 'utf-8')
  63. .then(() => logger.debug('配置保存成功'))
  64. .catch((error) => logger.error('保存配置失败', error))
  65. }
  66. /**
  67. * 获取主题配置
  68. */
  69. export function getThemeConfig(): 'light' | 'dark' | 'system' {
  70. return currentConfig.theme
  71. }
  72. /**
  73. * 保存主题配置
  74. */
  75. export function saveThemeConfig(theme: 'light' | 'dark' | 'system'): void {
  76. currentConfig.theme = theme
  77. saveConfig()
  78. }
  79. /**
  80. * 获取语言配置
  81. */
  82. export function getLanguageConfig(): 'zh-CN' | 'en-US' {
  83. return currentConfig.language
  84. }
  85. /**
  86. * 保存语言配置
  87. */
  88. export function saveLanguageConfig(language: 'zh-CN' | 'en-US'): void {
  89. currentConfig.language = language
  90. saveConfig()
  91. }
  92. /**
  93. * 添加安装历史
  94. */
  95. export function addInstallHistory(item: Omit<InstallHistoryItem, 'timestamp'>): void {
  96. const historyItem: InstallHistoryItem = {
  97. ...item,
  98. timestamp: Date.now()
  99. }
  100. currentConfig.installHistory.unshift(historyItem)
  101. // 只保留最近 100 条记录
  102. if (currentConfig.installHistory.length > 100) {
  103. currentConfig.installHistory = currentConfig.installHistory.slice(0, 100)
  104. }
  105. saveConfig()
  106. }
  107. /**
  108. * 获取安装历史
  109. */
  110. export function getInstallHistory(limit = 20): InstallHistoryItem[] {
  111. return currentConfig.installHistory.slice(0, limit)
  112. }
  113. /**
  114. * 清除安装历史
  115. */
  116. export function clearInstallHistory(): void {
  117. currentConfig.installHistory = []
  118. saveConfig()
  119. }
  120. /**
  121. * 获取窗口位置配置
  122. */
  123. export function getWindowBounds(): WindowBounds | undefined {
  124. return currentConfig.windowBounds
  125. }
  126. /**
  127. * 保存窗口位置配置
  128. */
  129. export function saveWindowBounds(bounds: WindowBounds): void {
  130. currentConfig.windowBounds = bounds
  131. saveConfig()
  132. }
  133. /**
  134. * 获取 Git 镜像配置
  135. */
  136. export function getGitMirrorFromConfig(): GitMirrorType {
  137. return currentConfig.gitMirror
  138. }
  139. /**
  140. * 保存 Git 镜像配置
  141. */
  142. export function saveGitMirrorConfig(mirror: GitMirrorType): void {
  143. currentConfig.gitMirror = mirror
  144. saveConfig()
  145. }
  146. /**
  147. * 获取 Node.js 镜像配置
  148. */
  149. export function getNodejsMirrorFromConfig(): NodejsMirrorType {
  150. return currentConfig.nodejsMirror
  151. }
  152. /**
  153. * 保存 Node.js 镜像配置
  154. */
  155. export function saveNodejsMirrorConfig(mirror: NodejsMirrorType): void {
  156. currentConfig.nodejsMirror = mirror
  157. saveConfig()
  158. }