import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const envPath = path.resolve(__dirname, '..', '.env');
// Load environment variables
dotenv.config();
export interface Config {
portainerUrl: string;
portainerApiKey: string;
portainerEndpointId: number;
isConfigured: boolean;
}
function loadConfig(): Config {
const portainerUrl = process.env.PORTAINER_URL || '';
const portainerApiKey = process.env.PORTAINER_API_KEY || '';
const portainerEndpointId = process.env.PORTAINER_ENDPOINT_ID || '1';
const isConfigured = !!(portainerUrl && portainerApiKey);
return {
portainerUrl: portainerUrl.replace(/\/$/, ''), // Remove trailing slash
portainerApiKey,
portainerEndpointId: parseInt(portainerEndpointId, 10),
isConfigured,
};
}
export let config = loadConfig();
// Function to reload config after changes
export function reloadConfig() {
// Manually read and parse .env to enforce override
try {
if (fs.existsSync(envPath)) {
const envConfig = dotenv.parse(fs.readFileSync(envPath));
for (const k in envConfig) {
process.env[k] = envConfig[k];
}
}
} catch (e) {
console.error('Error reloading .env:', e);
}
config = loadConfig();
}