import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const here = path.dirname(fileURLToPath(import.meta.url)); /** * Liest mobile/.env (ohne zusätzliche Abhängigkeit). Mit KUNDE= stattdessen * mobile/kunden//.env – so lassen sich Apps für mehrere Einrichtungen bauen. */ export function loadEnv() { const kunde = process.env.KUNDE ?? ''; if (kunde && !/^[a-z0-9][a-z0-9-]*$/.test(kunde)) throw new Error(`KUNDE „${kunde}“: nur Kleinbuchstaben, Ziffern und Bindestrich.`); const dir = kunde ? path.join(here, 'kunden', kunde) : here; const file = path.join(dir, '.env'); if (kunde && !fs.existsSync(file)) throw new Error(`${file} fehlt – siehe README, Abschnitt „Mehrere Einrichtungen“.`); const env = {}; if (fs.existsSync(file)) { for (const line of fs.readFileSync(file, 'utf8').split('\n')) { const m = line.match(/^\s*([A-Z_]+)\s*=\s*(.*?)\s*$/); if (m) env[m[1]] = m[2].replace(/^["']|["']$/g, ''); } } const get = (k, d) => process.env[k] || env[k] || d; return { kunde, /** Ordner mit .env und ggf. Firebase-Dateien der Einrichtung */ dir, appUrl: get('APP_URL', 'https://app.example.org').replace(/\/+$/, ''), appId: get('APP_ID', 'org.example.werkstattapp'), appName: get('APP_NAME', 'Werkstatt-App'), brandColor: get('BRAND_COLOR', '#2e2672') }; }