hub-setup.mjs
58 lines 2.1 KB
Raw
1 /**
2 * Hub Setup: read/write data/hub_setup.yaml (vault_path and vault.git overrides).
3 * Used by the Setup wizard in the Hub UI. Merged by loadConfig() in config.mjs.
4 */
5
6 import fs from 'fs';
7 import path from 'path';
8 import yaml from 'js-yaml';
9
10 /**
11 * Read current hub_setup overrides (for GET /api/v1/setup).
12 * @param {string} dataDir - Resolved data_dir path
13 * @returns {{ vault_path?: string, vault?: { git?: { enabled?: boolean, remote?: string } } } | null}
14 */
15 export function readHubSetup(dataDir) {
16 const p = path.join(dataDir, 'hub_setup.yaml');
17 if (!fs.existsSync(p)) return null;
18 try {
19 const raw = fs.readFileSync(p, 'utf8');
20 return yaml.load(raw) || null;
21 } catch (_) {
22 return null;
23 }
24 }
25
26 /**
27 * Write hub_setup.yaml. Only writes vault_path and vault.git; other keys are preserved if present.
28 * @param {string} dataDir - Resolved data_dir path
29 * @param {{ vault_path?: string, vault?: { git?: { enabled?: boolean, remote?: string } } }} payload
30 * @throws if payload is invalid or write fails
31 */
32 export function writeHubSetup(dataDir, payload) {
33 if (!dataDir || typeof dataDir !== 'string') {
34 throw new Error('data_dir is required');
35 }
36 const p = path.join(dataDir, 'hub_setup.yaml');
37 const existing = readHubSetup(dataDir) || {};
38 const updated = { ...existing };
39
40 if (payload.vault_path !== undefined) {
41 const v = typeof payload.vault_path === 'string' ? payload.vault_path.trim() : '';
42 if (!v) throw new Error('vault_path cannot be empty');
43 updated.vault_path = v;
44 }
45 if (payload.vault?.git !== undefined) {
46 updated.vault = updated.vault || {};
47 updated.vault.git = { ...(updated.vault.git || {}), ...payload.vault.git };
48 if (payload.vault.git.enabled !== undefined) updated.vault.git.enabled = !!payload.vault.git.enabled;
49 if (payload.vault.git.remote !== undefined) {
50 const r = typeof payload.vault.git.remote === 'string' ? payload.vault.git.remote.trim() : '';
51 updated.vault.git.remote = r || undefined;
52 }
53 }
54
55 const out = yaml.dump(updated, { lineWidth: 120 });
56 fs.mkdirSync(dataDir, { recursive: true });
57 fs.writeFileSync(p, out, 'utf8');
58 }
File History 1 commit