aboutsummaryrefslogtreecommitdiffstats
path: root/cli/src/config.ts
blob: a4c68a4bdf0bfd0b2cfb62a502007fdb5d0d8eb4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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: []
}