-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloadEnvFile.ts
More file actions
48 lines (42 loc) · 1.51 KB
/
loadEnvFile.ts
File metadata and controls
48 lines (42 loc) · 1.51 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
40
41
42
43
44
45
46
47
48
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
// Simple .env loader without external dependencies
function loadEnvFile(filePath: string) {
if (!existsSync(filePath)) {
console.warn(`Warning: File ${filePath} not found`);
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();
}
}
}
}
console.log(`✅ Loaded: ${filePath}`);
} catch (error) {
console.warn(`Warning: Could not load ${filePath}`);
console.warn(error);
}
}
// Determine environment (defaults to development)
const nodeEnv = process.env.NODE_ENV || 'development';
console.log(`🌍 Environment: ${nodeEnv}`);
// Load environment-specific config first (public values, production only)
if (nodeEnv === 'production') {
const envFile = join(process.cwd(), '.env.production');
loadEnvFile(envFile);
}
// Load .env file with secrets and local config (overrides public config if any)
// Required for DISCORD_TOKEN and other secrets
const localEnvFile = join(process.cwd(), '.env');
loadEnvFile(localEnvFile);