()
| 86 | try { |
| 87 | await fs.mkdir(dataPath, { recursive: true }); |
| 88 | this.configPath = path.join(dataPath, "zpr_config.json"); |
| 89 | this.backupPath = path.join(dataPath, "zpr_config.backup.json"); |
| 90 | |
| 91 | // 尝试从备份恢复损坏的配置 |
| 92 | await this.validateAndRestore(); |
| 93 | |
| 94 | this.db = await JSONFilePreset<Record<string, any>>( |
| 95 | this.configPath, |
| 96 | { ...DEFAULT_CONFIG } |
| 97 | ); |
| 98 | this.initialized = true; |
| 99 | console.log("[zpr] 配置初始化成功"); |
| 100 | } catch (error) { |
| 101 | console.error("[zpr] 初始化配置失败:", error); |
| 102 | await this.handleInitError(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private static async validateAndRestore(): Promise<void> { |
| 107 | try { |
| 108 | const configExists = await fs.access(this.configPath).then(() => true).catch(() => false); |
| 109 | if (!configExists) return; |
| 110 | |
| 111 | const configContent = await fs.readFile(this.configPath, 'utf8'); |
no test coverage detected