-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloadEnvFile.ts
More file actions
39 lines (35 loc) · 1.18 KB
/
Copy pathloadEnvFile.ts
File metadata and controls
39 lines (35 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
// Simple .env loader without external dependencies
function loadEnvFile(filePath: string) {
if (!existsSync(filePath)) {
return;
}
try {
const content = readFileSync(filePath, 'utf8');
const lines = content.split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith('#')) {
const [key, ...valueParts] = trimmed.split('=');
if (key && valueParts.length > 0) {
const value = valueParts.join('=');
// Only set if not already set (allows CI/CD to override)
if (!process.env[key.trim()]) {
process.env[key.trim()] = value.trim();
}
}
}
}
} catch (error) {
console.warn(`Warning: Could not load ${filePath}`);
console.warn('Make sure a valid .env.local file exists.');
console.warn(error);
}
}
// Load local environment file if it exists
// In production, docker will use .env.production and .env.secrets automatically
const isProd = process.env.NODE_ENV === 'production';
if (!isProd) {
loadEnvFile(join(process.cwd(), '.env.local'));
}