SPB Git forge

spb/rareindex

Public
54commits 1branches 0releases
7.1 MBsize
maindefault branch
10 days agolast push
TypeScript 61.9% HTML 37.2% SQL 0.7%
1.0 KB · 25 lines typescript
Raw Blame History
1import { readFileSync, existsSync } from 'node:fs';2import path from 'node:path';3import { fileURLToPath } from 'node:url';45/**6 * Minimal .env loader (no dependency): loads <repo>/.env into process.env when a key is not7 * already set. Called once at worker/CLI startup so `env()` from @rareindex/shared sees secrets.8 */9export function loadDotenv(): void {10  const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');11  for (const file of ['.env', '.env.local']) {12    const p = path.join(root, file);13    if (!existsSync(p)) continue;14    for (const line of readFileSync(p, 'utf8').split('\n')) {15      const m = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/);16      if (!m) continue;17      let v = m[2]!;18      if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1);19      if (process.env[m[1]!] === undefined) process.env[m[1]!] = v;20    }21  }22}2324export const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');25