diff options
Diffstat (limited to 'cli/src/commands/init.ts')
| -rw-r--r-- | cli/src/commands/init.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/cli/src/commands/init.ts b/cli/src/commands/init.ts new file mode 100644 index 0000000..64b85e0 --- /dev/null +++ b/cli/src/commands/init.ts @@ -0,0 +1,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) +} |
