/** * CLI configuration utilities * Stores credentials at ~/.config/reviq/credentials.json */ import { mkdir, readFile, unlink, writeFile } from "node:fs/promises"; import { homedir } from "node:os"; import { join } from "node:path"; export interface Config { apiUrl: string; token: string; email: string; } const CONFIG_DIR = join(homedir(), ".config", "reviq"); const CONFIG_FILE = join(CONFIG_DIR, "credentials.json"); /** * Get the path to the config file */ export function getConfigPath(): string { return CONFIG_FILE; } /** * Read the config file * Returns null if the file doesn't exist or is invalid */ export async function readConfig(): Promise { try { const data = await readFile(CONFIG_FILE, "utf-8"); return JSON.parse(data) as Config; } catch { return null; } } /** * Write the config file * Creates the config directory if it doesn't exist */ export async function writeConfig(config: Config): Promise { await mkdir(CONFIG_DIR, { recursive: true, mode: 0o700 }); await writeFile(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600, }); } /** * Delete the config file * Ignores errors if the file doesn't exist */ export async function deleteConfig(): Promise { try { await unlink(CONFIG_FILE); } catch { // Ignore if doesn't exist } }