codex-proxy / src /utils /yaml-mutate.ts
icebear0828
refactor: remove dead code, unused config, and fix type violations
50ae780
raw
history blame
766 Bytes
/**
* Structured YAML file mutation — parse, mutate, write back.
*
* Avoids fragile regex-based replacements.
* Note: js-yaml.dump() does not preserve comments.
*/
import { readFileSync, writeFileSync, renameSync } from "fs";
import yaml from "js-yaml";
/**
* Load a YAML file, apply a mutator function, and atomically write it back.
* Uses tmp-file + rename for crash safety.
*/
export function mutateYaml(filePath: string, mutator: (data: Record<string, unknown>) => void): void {
const raw = readFileSync(filePath, "utf-8");
const data = yaml.load(raw) as Record<string, unknown>;
mutator(data);
const tmp = filePath + ".tmp";
writeFileSync(tmp, yaml.dump(data, { lineWidth: -1, quotingType: '"' }), "utf-8");
renameSync(tmp, filePath);
}