()
| 164 | await fs.access(CONFIG_FILE_PATH); |
| 165 | const data = await fs.readFile(CONFIG_FILE_PATH, "utf-8"); |
| 166 | this.config = JSON.parse(data); |
| 167 | |
| 168 | let needSave = false; |
| 169 | |
| 170 | // 检查是否需要版本迁移 |
| 171 | if (!this.config.configVersion || this.config.configVersion < CURRENT_CONFIG_VERSION) { |
| 172 | console.log("[autodelcmd] 检测到旧版本配置,正在迁移..."); |
| 173 | await this.migrateConfig(); |
| 174 | needSave = true; |
| 175 | } |
| 176 | |
| 177 | // 为没有 ID 的规则生成简单数字 ID |
| 178 | if (this.config.customRules) { |
| 179 | let nextId = getMaxRuleId(this.config.customRules) + 1; |
| 180 | |
| 181 | // 为没有ID的规则分配连续的数字ID |
| 182 | this.config.customRules.forEach(rule => { |
| 183 | if (!rule.id) { |
| 184 | rule.id = nextId.toString(); |
| 185 | nextId++; |
| 186 | needSave = true; |
| 187 | } |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | if (needSave) { |
| 192 | await this.saveConfig(); |
| 193 | } |
| 194 | } catch (error) { |
| 195 | console.log("[autodelcmd] 首次运行,正在创建默认配置文件..."); |
| 196 | // 首次运行时,将默认规则写入配置文件 |
| 197 | await this.initializeDefaultConfig(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | private async initializeDefaultConfig() { |
| 202 | const defaultRules = this.getDefaultRules(); |
| 203 | |
| 204 | // 为默认规则分配ID |
| 205 | defaultRules.forEach((rule, index) => { |
| 206 | rule.id = (index + 1).toString(); |
no test coverage detected