-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
71 lines (61 loc) · 2.56 KB
/
Copy pathconfig.ts
File metadata and controls
71 lines (61 loc) · 2.56 KB
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
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { homedir } from 'node:os';
import { createInterface } from 'node:readline/promises';
import dotenv from 'dotenv';
export function getConfigPath(): string {
const base = process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
return join(base, 'fints-cli', 'config.env');
}
export function loadConfig(): void {
const path = getConfigPath();
if (existsSync(path)) {
dotenv.config({ path });
} else {
seedConfig();
}
}
function seedConfig(): void {
const configPath = getConfigPath();
const configDir = join(configPath, '..');
mkdirSync(configDir, { recursive: true, mode: 0o700 });
const content = CONFIG_KEYS.map((entry) => {
const def = 'default' in entry ? entry.default : '';
return `${entry.key}=${def}`;
}).join('\n') + '\n';
writeFileSync(configPath, content, { mode: 0o600 });
process.stderr.write(`Created config template at ${configPath}\nEdit it or run "fints-cli init" to configure.\n`);
}
const CONFIG_KEYS = [
{ key: 'FVB_BLZ', label: 'Bank code (BLZ)', default: '50190000' },
{ key: 'FVB_USER', label: 'Online banking user ID' },
{ key: 'FVB_PIN', label: 'Online banking PIN' },
{ key: 'FVB_URL', label: 'FinTS endpoint URL', default: 'https://hbci11.fiducia.de/cgi-bin/hbciservlet' },
{ key: 'FVB_PRODUCT_ID', label: 'FinTS product ID', default: '9FA6681DEC0CF3046BFC2F8A6' },
] as const;
export async function initConfig(): Promise<void> {
const rl = createInterface({ input: process.stdin, output: process.stderr });
const values: Record<string, string> = {};
try {
process.stderr.write('fints-cli configuration\n\n');
for (const entry of CONFIG_KEYS) {
const def = 'default' in entry ? entry.default : undefined;
const prompt = def ? `${entry.label} [${def}]: ` : `${entry.label}: `;
const answer = await rl.question(prompt);
values[entry.key] = answer || (def ?? '');
}
} finally {
rl.close();
}
const missing = CONFIG_KEYS.filter(k => !('default' in k) && !values[k.key]);
if (missing.length > 0) {
process.stderr.write(`\nMissing required values: ${missing.map(k => k.label).join(', ')}\n`);
process.exit(1);
}
const configPath = getConfigPath();
const configDir = join(configPath, '..');
mkdirSync(configDir, { recursive: true, mode: 0o700 });
const content = CONFIG_KEYS.map(({ key }) => `${key}=${values[key]}`).join('\n') + '\n';
writeFileSync(configPath, content, { mode: 0o600 });
process.stderr.write(`\nConfig written to ${configPath}\n`);
}