* 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)
| 1390 | * Returns the full path to the most recent backup, or null if none exist. |
| 1391 | */ |
| 1392 | function findMostRecentBackup(file: string): string | null { |
| 1393 | const fs = getFsImplementation() |
| 1394 | const fileBase = basename(file) |
| 1395 | const backupDir = getConfigBackupDir() |
| 1396 | |
| 1397 | // Check the new backup directory first |
| 1398 | try { |
| 1399 | const backups = fs |
| 1400 | .readdirStringSync(backupDir) |
| 1401 | .filter(f => f.startsWith(`${fileBase}.backup.`)) |
| 1402 | .sort() |
| 1403 | |
| 1404 | const mostRecent = backups.at(-1) // Timestamps sort lexicographically |
| 1405 | if (mostRecent) { |
| 1406 | return join(backupDir, mostRecent) |
| 1407 | } |
| 1408 | } catch { |
| 1409 | // Backup dir doesn't exist yet |
| 1410 | } |
| 1411 | |
| 1412 | // Fall back to legacy location (next to the config file) |
| 1413 | const fileDir = dirname(file) |
| 1414 | |
| 1415 | try { |
| 1416 | const backups = fs |
| 1417 | .readdirStringSync(fileDir) |
| 1418 | .filter(f => f.startsWith(`${fileBase}.backup.`)) |
| 1419 | .sort() |
| 1420 | |
| 1421 | const mostRecent = backups.at(-1) // Timestamps sort lexicographically |
| 1422 | if (mostRecent) { |
| 1423 | return join(fileDir, mostRecent) |
| 1424 | } |
| 1425 | |
| 1426 | // Check for legacy backup file (no timestamp) |
| 1427 | const legacyBackup = `${file}.backup` |
| 1428 | try { |
| 1429 | fs.statSync(legacyBackup) |
| 1430 | return legacyBackup |
| 1431 | } catch { |
| 1432 | // Legacy backup doesn't exist |
| 1433 | } |
| 1434 | } catch { |
| 1435 | // Ignore errors reading directory |
| 1436 | } |
| 1437 | |
| 1438 | return null |
| 1439 | } |
| 1440 | |
| 1441 | function getConfig<A>( |
| 1442 | file: string, |
no test coverage detected