import fs from "node:fs"; import path from "node:path"; import { paperclipConfigSchema, type PaperclipConfig } from "./schema.js"; const DEFAULT_CONFIG_PATH = ".paperclip/config.json"; export function resolveConfigPath(overridePath?: string): string { if (overridePath) return path.resolve(overridePath); if (process.env.PAPERCLIP_CONFIG) return path.resolve(process.env.PAPERCLIP_CONFIG); return path.resolve(process.cwd(), DEFAULT_CONFIG_PATH); } export function readConfig(configPath?: string): PaperclipConfig | null { const filePath = resolveConfigPath(configPath); if (!fs.existsSync(filePath)) return null; const raw = JSON.parse(fs.readFileSync(filePath, "utf-8")); return paperclipConfigSchema.parse(raw); } export function writeConfig( config: PaperclipConfig, configPath?: string, ): void { const filePath = resolveConfigPath(configPath); const dir = path.dirname(filePath); fs.mkdirSync(dir, { recursive: true }); // Backup existing config before overwriting if (fs.existsSync(filePath)) { const backupPath = filePath + ".backup"; fs.copyFileSync(filePath, backupPath); fs.chmodSync(backupPath, 0o600); } fs.writeFileSync(filePath, JSON.stringify(config, null, 2) + "\n", { mode: 0o600, }); } export function configExists(configPath?: string): boolean { return fs.existsSync(resolveConfigPath(configPath)); }