aboutsummaryrefslogtreecommitdiffstats
path: root/cli/src/commands/init.ts
blob: 64b85e013c870d8ef1035ace55d74aa4f1de2fb2 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as p from "@clack/prompts"
import { loadConfig, saveConfig, CONFIG_PATH } from "../config"
import type { Config } from "../config"

export async function init() {
    let existing: Partial<Config> = {}
    try {
        existing = loadConfig()
    } catch {}

    p.intro(existing.sb1 ? `Editing config at ${CONFIG_PATH}` : "Setting up sb1-actual")

    const sb1 = await p.group({
        clientId: () => p.text({
            message: "SB1 client ID",
            initialValue: existing.sb1?.clientId,
            validate: v => v.trim() ? undefined : "Required"
        }),
        clientSecret: () => p.text({
            message: "SB1 client secret",
            initialValue: existing.sb1?.clientSecret,
            validate: v => v.trim() ? undefined : "Required"
        }),
        finInst: async () => {
            const known = [
                { value: "fid-ostlandet", label: "SpareBank 1 Østlandet (fid-ostlandet)" },
                { value: "custom",        label: "Other (enter manually)" },
            ]
            const current = existing.sb1?.finInst
            const selection = await p.select({
                message: "SB1 financial institution",
                options: known,
                initialValue: known.find(o => o.value === current) ? current : "custom",
            })
            if (p.isCancel(selection)) onCancel()
            if (selection !== "custom") return selection as string
            return p.text({
                message: "Enter finInst value",
                initialValue: current,
                validate: v => v.trim() ? undefined : "Required"
            }) as Promise<string>
        },
        redirectUri: () => {
            const uri = "http://localhost:3123/callback"
            p.note(uri, "Redirect URI — register this in the SB1 developer portal")
            return Promise.resolve(uri)
        },
    }, { onCancel })

    const actual = await p.group({
        host: () => p.text({
            message: "Actual server URL",
            initialValue: existing.actual?.host,
            placeholder: "http://localhost:5006",
        }),
        password: () => p.password({
            message: "Actual password",
        }),
        fileId: () => p.text({
            message: "Actual file ID",
            initialValue: existing.actual?.fileId,
            validate: v => v.trim() ? undefined : "Required"
        }),
    }, { onCancel })

    const config: Config = { sb1, actual, mappings: existing.mappings ?? [] }

    saveConfig(config)

    p.outro(`Config saved. Run \`sb1-actual auth\` to authenticate with Sparebanken 1.`)
}

function onCancel() {
    p.cancel("Cancelled.")
    process.exit(0)
}