mya-cli
Version:
MYA - AI-Powered Stock & Options Analysis CLI Tool
74 lines • 2.37 kB
JavaScript
/**
* Environment variable provider implementation
*/
import fs from 'node:fs';
import path from 'node:path';
/**
* File-based environment variable provider
* Single responsibility: load and provide environment variables
*/
export class FileEnvironmentProvider {
envPath;
env = {};
/**
* Create a new environment provider
* @param envPath Optional custom path to .env file
*/
constructor(envPath) {
this.envPath = envPath || path.resolve(process.cwd(), '.env');
}
/**
* Load environment variables from .env file
*/
loadEnvironment() {
try {
if (fs.existsSync(this.envPath)) {
console.log('Loading environment variables from .env file');
const envContent = fs.readFileSync(this.envPath, 'utf8');
const envVars = envContent
.split('\n')
.filter((line) => line && !line.startsWith('#'))
.reduce((acc, line) => {
const match = line.match(/^([^=]+)=(.*)$/);
if (match) {
const key = match[1].trim();
const value = match[2]
.trim()
.replace(/^"(.*)"$/, '$1')
.replace(/^'(.*)'$/, '$1');
acc[key] = value;
}
return acc;
}, {});
// Set environment variables if not already set
Object.keys(envVars).forEach((key) => {
if (!process.env[key]) {
process.env[key] = envVars[key];
}
});
this.env = envVars;
}
else {
// .env file not found, use environment variables
}
}
catch {
// Return empty object if environment reading fails
}
}
/**
* Get an environment variable
* @param name Environment variable name
* @param defaultValue Optional default value if not found
*/
get(name, defaultValue) {
return process.env[name] || defaultValue;
}
/**
* Get all environment variables
*/
getEnv() {
return this.env;
}
}
//# sourceMappingURL=env-provider.js.map