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:
Zac Williams
2026-02-08 16:58:51 -06:00
committed by GitHub
parent 895d340dd4
commit c69c47fe9b
5 changed files with 90 additions and 3 deletions

View File

@@ -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)