Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
import * as fs from 'fs';
import * as path from 'path';
import * as dotenv from 'dotenv';
/**
* `data/.env.generated` — the dashboard-written env overlay, one KEY=value per line, merged into
* `process.env` on the next boot (after `process.env` and `.env`, all `override: false`).
*
* Resolved per call rather than captured at import: the location is relative to `process.cwd()`, and
* the controller has always re-derived it on each use.
*
* NOT shared with `database/load-cli-env.ts`, which resolves the same filename against an injected
* `cwd` so it stays testable without chdir. Folding that into this helper would remove its seam.
*/
export const generatedEnvPath = (): string => path.resolve(process.cwd(), 'data', '.env.generated');
/**
* Parsed contents, or `{}` when the file has never been written.
*
* Three reads share this — the built-in-flag fallback behind the Docker probe
* (InfraStatusController.readSavedBuiltinFlags), the config form's hydrate
* (InfraConfigController.getConfig), and the merge base the save path writes back over
* (InfraConfigController.saveConfig). Re-deriving the location at each of them made the file an
* undeclared dependency shared between reading infrastructure status, rendering the form, and
* persisting credentials: three concerns that otherwise touch none of the same state.
*/
export function readGeneratedEnv(): Record<string, string> {
const envPath = generatedEnvPath();
return fs.existsSync(envPath) ? dotenv.parse(fs.readFileSync(envPath, 'utf8')) : {};
}
/**
* `value` in the form dotenv reads back unchanged, or `undefined` when no form does. dotenv takes an
* unquoted `#` as a comment and strips outer quotes and whitespace, so such a value is quoted; any
* other value stays bare, which keeps existing files and scripts/lib-env.sh (plain lines only) as-is.
* A leading quote is never left bare: it reads back on its own line, but in the whole file it opens a
* quoted value that a later line holding the same quote character closes.
*/
export function encodeGeneratedEnvValue(key: string, value: string): string | undefined {
const quoted = [`'${value}'`, `"${value}"`, `\`${value}\``];
return (/^['"`]/.test(value) ? quoted : [value, ...quoted]).find(
form => dotenv.parse(`${key}=${form}`)[key] === value,
);
}