fix: backup existing config files before overwriting (#119)
Before writing config.toml (Codex) or opencode.json (OpenCode), the CLI attempts to create a timestamped backup of any existing config file. This prevents accidental data loss when users have customized configs. Backup is best-effort - if it fails (e.g., unusual permissions), the install continues without blocking. Backup files are named: config.toml.bak.2026-01-23T21-16-40-065Z
This commit is contained in:
@@ -1,6 +1,19 @@
|
||||
import { promises as fs } from "fs"
|
||||
import path from "path"
|
||||
|
||||
export async function backupFile(filePath: string): Promise<string | null> {
|
||||
if (!(await pathExists(filePath))) return null
|
||||
|
||||
try {
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, "-")
|
||||
const backupPath = `${filePath}.bak.${timestamp}`
|
||||
await fs.copyFile(filePath, backupPath)
|
||||
return backupPath
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function pathExists(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
await fs.access(filePath)
|
||||
|
||||
Reference in New Issue
Block a user