import { readFileSync } from 'fs'; import { resolve } from 'path'; export function load_config(file_path) { if (!file_path) { throw new Error('--config is required'); } let raw; try { raw = readFileSync(file_path, 'utf8'); } catch (err) { throw new Error(`Cannot read config file at ${file_path}: ${err.message}`); } let parsed; try { parsed = JSON.parse(raw); } catch (err) { throw new Error(`Config file is not valid JSON: ${err.message}`); } if (!parsed.secrets) { throw new Error("Config file must have a 'secrets' field pointing to the secrets file"); } // Resolve secrets path relative to the config file's directory const config_dir = resolve(file_path, '..'); const secrets_path = resolve(config_dir, parsed.secrets); let secrets_raw; try { secrets_raw = readFileSync(secrets_path, 'utf8'); } catch (err) { throw new Error(`Cannot read secrets file at ${secrets_path}: ${err.message}`); } let secrets; try { secrets = JSON.parse(secrets_raw); } catch (err) { throw new Error(`Secrets file is not valid JSON: ${err.message}`); } if (!secrets.users || typeof secrets.users !== 'object') { throw new Error("Secrets file must have a top-level 'users' object"); } const mail_perms_path = parsed.mail_perms ? resolve(config_dir, parsed.mail_perms) : null; return { users: secrets.users, smtp: parsed.smtp ?? null, mail_perms_path, bind: parsed.bind ?? null, port: parsed.port ?? null, }; }