- Extracted tab width resolution from pi-natives to pi-utils with EditorConfig caching support. - Made tabWidth parameter required in native text functions (visibleWidth, truncateToWidth, wrapTextWithAnsi, sliceWithWidth, extractSegments). - Removed process-wide tab width state management from pi-natives text module. - Consolidated wrapper node promotion logic in chunk classification with abbreviated string representations. - Removed path-clean and pathdiff dependencies from pi-natives. - Updated all consuming packages to import tab width functions from pi-utils and pass explicit tabWidth parameters.
109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
import * as fs from "node:fs";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { getAgentDir, getConfigRootDir } from "./dirs";
|
|
|
|
/**
|
|
* 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 piEnv = parseEnvFile(path.join(getConfigRootDir(), ".env"));
|
|
const agentEnv = parseEnvFile(path.join(getAgentDir(), ".env"));
|
|
const projectEnv = parseEnvFile(path.join(process.cwd(), ".env"));
|
|
|
|
for (const file of [projectEnv, agentEnv, piEnv, 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>;
|
|
|
|
/**
|
|
* Resolve the first environment variable value from the given keys.
|
|
* @param keys - The keys to resolve.
|
|
* @returns The first environment variable value, or undefined if no value is found.
|
|
*/
|
|
export function $pickenv(...keys: string[]): string | undefined {
|
|
for (const key of keys) {
|
|
const value = Bun.env[key]?.trim();
|
|
if (value) {
|
|
return value;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* Parses a positive decimal integer from `$env[name]`.
|
|
* Empty, invalid, NaN, zero, or negative values return `defaultValue`.
|
|
*/
|
|
export function $envpos(name: string, defaultValue: number): number {
|
|
const raw = $env[name];
|
|
if (!raw) return defaultValue;
|
|
const parsed = Number.parseInt(raw, 10);
|
|
if (Number.isNaN(parsed) || parsed <= 0) return defaultValue;
|
|
return parsed;
|
|
}
|
|
|
|
/** True when `BUN_ENV` or `NODE_ENV` is the string `test`. */
|
|
export function isBunTestRuntime(): boolean {
|
|
return Bun.env.BUN_ENV === "test" || Bun.env.NODE_ENV === "test";
|
|
}
|
|
|
|
const TRUTHY: Dict<boolean> = { "1": true, Y: true, TRUE: true, YES: true, ON: true };
|
|
export function $flag(name: string, def: boolean = false): boolean {
|
|
const value = $env[name];
|
|
if (!value) return def;
|
|
return TRUTHY[value] === true;
|
|
}
|