fix: 修复关键逻辑错误

- 修复 store 初始化的异步问题:构造函数中的异步方法现在正确等待
- 修复配置切换时删除原文件的严重 bug:使用 copyFile 替代 rename 避免文件丢失
- 简化代码实现,移除过度设计的部分
This commit is contained in:
Jason
2025-08-10 20:36:17 +08:00
parent 5f0ef8a441
commit 9dac53b754
3 changed files with 23 additions and 26 deletions

View File

@@ -75,7 +75,7 @@ export async function fileExists(filePath: string): Promise<boolean> {
}
/**
* 切换供应商配置(基于文件重命名
* 切换供应商配置(基于文件复制
*/
export async function switchProvider(
provider: Provider,
@@ -104,21 +104,14 @@ export async function switchProvider(
currentProviderId,
currentProvider.name
);
await fs.rename(settingsPath, currentProviderPath);
// 使用复制而不是重命名,避免删除原文件
await fs.copyFile(settingsPath, currentProviderPath);
console.log(`已备份当前供应商配置: ${currentProvider.name}`);
} else {
// 如果没有当前供应商ID创建临时备份
const backupPath = path.join(
configDir,
`settings-backup-${Date.now()}.json`
);
await fs.rename(settingsPath, backupPath);
console.log(`已备份当前配置到: ${backupPath}`);
}
}
// 2. 将目标供应商配置重命名为settings.json
await fs.rename(newSettingsPath, settingsPath);
// 2. 复制新配置到settings.json(保留原文件)
await fs.copyFile(newSettingsPath, settingsPath);
console.log(`成功切换到供应商: ${provider.name}`);
return true;