* Find the most recent backup file for a given config file. * Checks ~/.claude/backups/ first, then falls back to the legacy location * (next to the config file) for backwards compatibility. * Returns the full path to the most recent backup, or null if none exist.
(file: string)
| 1370 | * Returns the full path to the most recent backup, or null if none exist. |
| 1371 | */ |
| 1372 | function findMostRecentBackup(file: string): string | null { |
| 1373 | const fs = getFsImplementation() |
| 1374 | const fileBase = basename(file) |
| 1375 | const backupDir = getConfigBackupDir() |
| 1376 | |
| 1377 | // Check the new backup directory first |
| 1378 | try { |
| 1379 | const backups = fs |
| 1380 | .readdirStringSync(backupDir) |
| 1381 | .filter(f => f.startsWith(`${fileBase}.backup.`)) |
| 1382 | .sort() |
| 1383 | |
| 1384 | const mostRecent = backups.at(-1) // Timestamps sort lexicographically |
| 1385 | if (mostRecent) { |
| 1386 | return join(backupDir, mostRecent) |
| 1387 | } |
| 1388 | } catch { |
| 1389 | // Backup dir doesn't exist yet |
| 1390 | } |
| 1391 | |
| 1392 | // Fall back to legacy location (next to the config file) |
| 1393 | const fileDir = dirname(file) |
| 1394 | |
| 1395 | try { |
| 1396 | const backups = fs |
| 1397 | .readdirStringSync(fileDir) |
| 1398 | .filter(f => f.startsWith(`${fileBase}.backup.`)) |
| 1399 | .sort() |
| 1400 | |
| 1401 | const mostRecent = backups.at(-1) // Timestamps sort lexicographically |
| 1402 | if (mostRecent) { |
| 1403 | return join(fileDir, mostRecent) |
| 1404 | } |
| 1405 | |
| 1406 | // Check for legacy backup file (no timestamp) |
| 1407 | const legacyBackup = `${file}.backup` |
| 1408 | try { |
| 1409 | fs.statSync(legacyBackup) |
| 1410 | return legacyBackup |
| 1411 | } catch { |
| 1412 | // Legacy backup doesn't exist |
| 1413 | } |
| 1414 | } catch { |
| 1415 | // Ignore errors reading directory |
| 1416 | } |
| 1417 | |
| 1418 | return null |
| 1419 | } |
| 1420 | |
| 1421 | function getConfig<A>( |
| 1422 | file: string, |
no test coverage detected