重大改进:用原生 JSON 存储替换 electron-store
- 实现 SimpleStore 类,使用原生 fs 操作 JSON 文件 - 解决 "Cannot find module 'conf'" 的依赖问题 - 移除 electron-store 依赖,减少包体积 - 配置文件存储在 ~/.cc-switch/config.json - 数据格式透明,便于备份和调试 - 修复 TypeScript 类型问题 - 测试通过:构建、打包、运行正常
This commit is contained in:
@@ -30,7 +30,6 @@
|
|||||||
"vite": "^5.0.0"
|
"vite": "^5.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"electron-store": "^8.1.0",
|
|
||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
"react-dom": "^18.2.0"
|
"react-dom": "^18.2.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import { app, BrowserWindow, ipcMain, dialog, shell } from 'electron'
|
import { app, BrowserWindow, ipcMain, dialog, shell } from 'electron'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import Store from 'electron-store'
|
import { Provider } from '../shared/types'
|
||||||
import { Provider, AppConfig } from '../shared/types'
|
|
||||||
import { switchProvider, getClaudeCodeConfig } from './services'
|
import { switchProvider, getClaudeCodeConfig } from './services'
|
||||||
|
import { store } from './store'
|
||||||
const store = new Store<AppConfig>()
|
|
||||||
|
|
||||||
let mainWindow: BrowserWindow | null = null
|
let mainWindow: BrowserWindow | null = null
|
||||||
|
|
||||||
@@ -51,33 +49,33 @@ app.on('window-all-closed', () => {
|
|||||||
|
|
||||||
// IPC handlers
|
// IPC handlers
|
||||||
ipcMain.handle('getProviders', () => {
|
ipcMain.handle('getProviders', () => {
|
||||||
return store.get('providers', {})
|
return store.get('providers', {} as Record<string, Provider>)
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.handle('getCurrentProvider', () => {
|
ipcMain.handle('getCurrentProvider', () => {
|
||||||
return store.get('current', '')
|
return store.get('current', '')
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.handle('addProvider', (_, provider: Provider) => {
|
ipcMain.handle('addProvider', async (_, provider: Provider) => {
|
||||||
const providers = store.get('providers', {})
|
const providers = store.get('providers', {} as Record<string, Provider>)
|
||||||
providers[provider.id] = provider
|
providers[provider.id] = provider
|
||||||
store.set('providers', providers)
|
await store.set('providers', providers)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.handle('deleteProvider', (_, id: string) => {
|
ipcMain.handle('deleteProvider', async (_, id: string) => {
|
||||||
const providers = store.get('providers', {})
|
const providers = store.get('providers', {} as Record<string, Provider>)
|
||||||
delete providers[id]
|
delete providers[id]
|
||||||
store.set('providers', providers)
|
await store.set('providers', providers)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.handle('updateProvider', async (_, provider: Provider) => {
|
ipcMain.handle('updateProvider', async (_, provider: Provider) => {
|
||||||
const providers = store.get('providers', {})
|
const providers = store.get('providers', {} as Record<string, Provider>)
|
||||||
const currentProviderId = store.get('current', '')
|
const currentProviderId = store.get('current', '')
|
||||||
|
|
||||||
providers[provider.id] = provider
|
providers[provider.id] = provider
|
||||||
store.set('providers', providers)
|
await store.set('providers', providers)
|
||||||
|
|
||||||
// 如果编辑的是当前激活的供应商,同时更新Claude Code配置
|
// 如果编辑的是当前激活的供应商,同时更新Claude Code配置
|
||||||
if (provider.id === currentProviderId) {
|
if (provider.id === currentProviderId) {
|
||||||
@@ -92,12 +90,12 @@ ipcMain.handle('updateProvider', async (_, provider: Provider) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.handle('switchProvider', async (_, providerId: string) => {
|
ipcMain.handle('switchProvider', async (_, providerId: string) => {
|
||||||
const providers = store.get('providers', {})
|
const providers = store.get('providers', {} as Record<string, Provider>)
|
||||||
const provider = providers[providerId]
|
const provider = providers[providerId]
|
||||||
if (provider) {
|
if (provider) {
|
||||||
const success = await switchProvider(provider)
|
const success = await switchProvider(provider)
|
||||||
if (success) {
|
if (success) {
|
||||||
store.set('current', providerId)
|
await store.set('current', providerId)
|
||||||
}
|
}
|
||||||
return success
|
return success
|
||||||
}
|
}
|
||||||
|
|||||||
56
src/main/store.ts
Normal file
56
src/main/store.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import fs from 'fs/promises'
|
||||||
|
import path from 'path'
|
||||||
|
import os from 'os'
|
||||||
|
import { AppConfig } from '../shared/types'
|
||||||
|
|
||||||
|
export class SimpleStore {
|
||||||
|
private configPath: string
|
||||||
|
private configDir: string
|
||||||
|
private data: AppConfig = { providers: {}, current: '' }
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.configDir = path.join(os.homedir(), '.cc-switch')
|
||||||
|
this.configPath = path.join(this.configDir, 'config.json')
|
||||||
|
this.loadData()
|
||||||
|
}
|
||||||
|
|
||||||
|
private async loadData(): Promise<void> {
|
||||||
|
try {
|
||||||
|
const content = await fs.readFile(this.configPath, 'utf-8')
|
||||||
|
this.data = JSON.parse(content)
|
||||||
|
} catch (error) {
|
||||||
|
// 文件不存在或格式错误,使用默认数据
|
||||||
|
this.data = { providers: {}, current: '' }
|
||||||
|
await this.saveData()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async saveData(): Promise<void> {
|
||||||
|
try {
|
||||||
|
// 确保目录存在
|
||||||
|
await fs.mkdir(this.configDir, { recursive: true })
|
||||||
|
// 写入配置文件
|
||||||
|
await fs.writeFile(this.configPath, JSON.stringify(this.data, null, 2), 'utf-8')
|
||||||
|
} catch (error) {
|
||||||
|
console.error('保存配置失败:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get<T>(key: keyof AppConfig, defaultValue?: T): T {
|
||||||
|
const value = this.data[key] as T
|
||||||
|
return value !== undefined ? value : (defaultValue as T)
|
||||||
|
}
|
||||||
|
|
||||||
|
async set<T>(key: keyof AppConfig, value: T): Promise<void> {
|
||||||
|
this.data[key] = value as any
|
||||||
|
await this.saveData()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取配置文件路径,用于调试
|
||||||
|
getConfigPath(): string {
|
||||||
|
return this.configPath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建单例
|
||||||
|
export const store = new SimpleStore()
|
||||||
Reference in New Issue
Block a user