import { join } from "node:path" import { homedir } from "node:os" import { mkdirSync, existsSync, readFileSync, writeFileSync } from "node:fs" export const CONFIG_DIR = join(homedir(), ".config", "sb1-actual") export const CONFIG_PATH = join(CONFIG_DIR, "config.json") export const TOKENS_PATH = join(CONFIG_DIR, "tokens.json") export type AccountMapping = { sb1Id: string actualId: string label?: string } export type Config = { sb1: { clientId: string clientSecret: string finInst: string } actual: { host: string password: string fileId: string } mappings: AccountMapping[] } export function loadConfig(): Config { if (!existsSync(CONFIG_PATH)) { throw new Error(`No config found at ${CONFIG_PATH}\n\nRun \`sb1-actual init\` to create it.`) } return JSON.parse(readFileSync(CONFIG_PATH, "utf8")) } export function saveConfig(config: Config): void { mkdirSync(CONFIG_DIR, { recursive: true }) writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2)) } const exampleConfig: Config = { sb1: { clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET", finInst: "YOUR_FIN_INST" }, actual: { host: "http://localhost:5006", password: "your-password", fileId: "your-budget-file-id" }, mappings: [] }