|
| 1 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 2 | +import { homedir } from "node:os"; |
| 3 | +import { dirname, join } from "node:path"; |
| 4 | +import { CliError } from "./error.ts"; |
| 5 | + |
| 6 | +export interface Config { |
| 7 | + apiKey?: string; |
| 8 | +} |
| 9 | + |
| 10 | +/** Maps CLI key names (e.g. "api-key") to Config property names (e.g. "apiKey"). */ |
| 11 | +export const CONFIG_KEY_MAP: Record<string, keyof Config> = { |
| 12 | + "api-key": "apiKey", |
| 13 | +}; |
| 14 | + |
| 15 | +function errorMessage(err: unknown): string { |
| 16 | + return err instanceof Error ? err.message : String(err); |
| 17 | +} |
| 18 | + |
| 19 | +export function getConfigFilePath(): string { |
| 20 | + const xdgConfigHome = process.env["XDG_CONFIG_HOME"]; |
| 21 | + const base = |
| 22 | + xdgConfigHome != null && xdgConfigHome !== "" |
| 23 | + ? xdgConfigHome |
| 24 | + : join(homedir(), ".config"); |
| 25 | + return join(base, "codize", "config.json"); |
| 26 | +} |
| 27 | + |
| 28 | +export function readConfig(): Config { |
| 29 | + const filePath = getConfigFilePath(); |
| 30 | + if (!existsSync(filePath)) { |
| 31 | + return {}; |
| 32 | + } |
| 33 | + let raw: string; |
| 34 | + try { |
| 35 | + raw = readFileSync(filePath, "utf-8"); |
| 36 | + } catch (err) { |
| 37 | + throw new CliError( |
| 38 | + `Cannot read config file '${filePath}': ${errorMessage(err)}`, |
| 39 | + ); |
| 40 | + } |
| 41 | + let parsed: unknown; |
| 42 | + try { |
| 43 | + parsed = JSON.parse(raw); |
| 44 | + } catch { |
| 45 | + throw new CliError( |
| 46 | + `Config file '${filePath}' contains invalid JSON. Fix or delete it to continue.`, |
| 47 | + ); |
| 48 | + } |
| 49 | + if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) { |
| 50 | + throw new CliError( |
| 51 | + `Config file '${filePath}' must be a JSON object. Fix or delete it to continue.`, |
| 52 | + ); |
| 53 | + } |
| 54 | + const obj = parsed as Record<string, unknown>; |
| 55 | + const config: Config = {}; |
| 56 | + if ("apiKey" in obj && typeof obj["apiKey"] !== "string") { |
| 57 | + throw new CliError( |
| 58 | + `Config file '${filePath}': 'apiKey' must be a string, got ${typeof obj["apiKey"]}. Fix or delete it to continue.`, |
| 59 | + ); |
| 60 | + } |
| 61 | + if (typeof obj["apiKey"] === "string") { |
| 62 | + config.apiKey = obj["apiKey"]; |
| 63 | + } |
| 64 | + return config; |
| 65 | +} |
| 66 | + |
| 67 | +export function writeConfig(config: Config): void { |
| 68 | + const filePath = getConfigFilePath(); |
| 69 | + const dir = dirname(filePath); |
| 70 | + try { |
| 71 | + mkdirSync(dir, { recursive: true }); |
| 72 | + } catch (err) { |
| 73 | + throw new CliError( |
| 74 | + `Cannot create config directory '${dir}': ${errorMessage(err)}`, |
| 75 | + ); |
| 76 | + } |
| 77 | + const json = JSON.stringify(config, null, 2) + "\n"; |
| 78 | + try { |
| 79 | + writeFileSync(filePath, json, "utf-8"); |
| 80 | + } catch (err) { |
| 81 | + throw new CliError( |
| 82 | + `Cannot write config file '${filePath}': ${errorMessage(err)}`, |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
0 commit comments