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,5 +1,5 @@
|
||||
import path from "path"
|
||||
import { copyDir, ensureDir, writeText } from "../utils/files"
|
||||
import { backupFile, copyDir, ensureDir, writeText } from "../utils/files"
|
||||
import type { CodexBundle } from "../types/codex"
|
||||
import type { ClaudeMcpServer } from "../types/claude"
|
||||
|
||||
@@ -30,7 +30,12 @@ export async function writeCodexBundle(outputRoot: string, bundle: CodexBundle):
|
||||
|
||||
const config = renderCodexConfig(bundle.mcpServers)
|
||||
if (config) {
|
||||
await writeText(path.join(codexRoot, "config.toml"), config)
|
||||
const configPath = path.join(codexRoot, "config.toml")
|
||||
const backupPath = await backupFile(configPath)
|
||||
if (backupPath) {
|
||||
console.log(`Backed up existing config to ${backupPath}`)
|
||||
}
|
||||
await writeText(configPath, config)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
import path from "path"
|
||||
import { copyDir, ensureDir, writeJson, writeText } from "../utils/files"
|
||||
import { backupFile, copyDir, ensureDir, writeJson, writeText } from "../utils/files"
|
||||
import type { OpenCodeBundle } from "../types/opencode"
|
||||
|
||||
export async function writeOpenCodeBundle(outputRoot: string, bundle: OpenCodeBundle): Promise<void> {
|
||||
const paths = resolveOpenCodePaths(outputRoot)
|
||||
await ensureDir(paths.root)
|
||||
|
||||
const backupPath = await backupFile(paths.configPath)
|
||||
if (backupPath) {
|
||||
console.log(`Backed up existing config to ${backupPath}`)
|
||||
}
|
||||
await writeJson(paths.configPath, bundle.config)
|
||||
|
||||
const agentsDir = paths.agentsDir
|
||||
|
||||
@@ -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