89e6479661
- Migrated all environment variable access from Node.js `process.env` to Bun runtime `Bun.env` API across the entire codebase. - Updated 71 files including source code, tests, documentation, and configuration to use Bun's native environment variable API. - Added comprehensive environment variables documentation in `packages/coding-agent/docs/environment-variables.md` covering 100+ environment variables organized by category. - Updated CHANGELOG entries to reflect migration from `process.env` to `Bun.env` and environment variable prefix changes (PI_* vs OMP_*). - Maintained identical behavior and logic across all changes - only the environment variable access method was updated.
67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import * as fs from "node:fs";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
|
|
/**
|
|
* Parses a .env file synchronously and extracts key-value string pairs.
|
|
* Ignores lines that are empty or start with '#'. Trims whitespace.
|
|
* Allows values to be quoted with single or double quotes.
|
|
* Returns an object of key-value pairs.
|
|
*/
|
|
function parseEnvFile(filePath: string): Record<string, string> {
|
|
const result: Record<string, string> = {};
|
|
try {
|
|
const content = fs.readFileSync(filePath, "utf-8");
|
|
for (const line of content.split("\n")) {
|
|
const trimmed = line.trim();
|
|
// Skip comments and blank lines
|
|
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
|
|
const eqIndex = trimmed.indexOf("=");
|
|
if (eqIndex === -1) continue;
|
|
|
|
const key = trimmed.slice(0, eqIndex).trim();
|
|
let value = trimmed.slice(eqIndex + 1).trim();
|
|
|
|
// Remove surrounding quotes (" or ')
|
|
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
value = value.slice(1, -1);
|
|
}
|
|
|
|
result[key] = value;
|
|
}
|
|
} catch {
|
|
// File doesn't exist or can't be read - return empty result
|
|
}
|
|
|
|
// OMP_ overrides PI_
|
|
for (const k in result) {
|
|
if (k.startsWith("OMP_")) {
|
|
result[`PI_${k.slice(4)}`] = result[k];
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// Eagerly parse the user's $HOME/.env and the current project's .env (from cwd)
|
|
const homeEnv = parseEnvFile(path.join(os.homedir(), ".env"));
|
|
const projectEnv = parseEnvFile(path.join(process.cwd(), ".env"));
|
|
|
|
for (const file of [projectEnv, homeEnv]) {
|
|
for (const [key, value] of Object.entries(file)) {
|
|
if (!Bun.env[key]) {
|
|
Bun.env[key] = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Intentional re-export of Bun.env.
|
|
*
|
|
* All users should import this env module (import { $env } from "@oh-my-pi/pi-utils")
|
|
* before using environment variables. This ensures that .env files have been loaded and
|
|
* overrides (project, home) have been applied, so $env always reflects the correct values.
|
|
*/
|
|
export const $env: Record<string, string> = Bun.env as Record<string, string>;
|