fixed env loader
This commit is contained in:
parent
ae2818e068
commit
05974cd602
1 changed files with 74 additions and 10 deletions
|
|
@ -27,8 +27,11 @@ export class EnvLoader implements ConfigLoader {
|
||||||
|
|
||||||
async load(): Promise<Record<string, unknown>> {
|
async load(): Promise<Record<string, unknown>> {
|
||||||
try {
|
try {
|
||||||
// Load root .env file only
|
// Load root .env file - try multiple possible locations
|
||||||
this.loadEnvFile('../../.env'); // Root .env
|
const possiblePaths = ['./.env', '../.env', '../../.env'];
|
||||||
|
for (const path of possiblePaths) {
|
||||||
|
this.loadEnvFile(path);
|
||||||
|
}
|
||||||
|
|
||||||
const config: Record<string, unknown> = {};
|
const config: Record<string, unknown> = {};
|
||||||
const envVars = process.env;
|
const envVars = process.env;
|
||||||
|
|
@ -64,18 +67,32 @@ export class EnvLoader implements ConfigLoader {
|
||||||
const parsedValue = this.parseValue(value);
|
const parsedValue = this.parseValue(value);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (this.options.nestedDelimiter && key.includes(this.options.nestedDelimiter)) {
|
// Handle provider-specific environment variables (only for application usage, not tests)
|
||||||
// Handle nested delimiter (e.g., APP__NAME -> { APP: { NAME: value } })
|
if (!this.prefix && !this.options.convertCase) {
|
||||||
const parts = key.split(this.options.nestedDelimiter);
|
const providerMapping = this.getProviderMapping(key);
|
||||||
this.setNestedValue(config, parts, parsedValue);
|
if (providerMapping) {
|
||||||
} else if (this.options.convertCase) {
|
this.setNestedValue(config, providerMapping.path, parsedValue);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.options.convertCase) {
|
||||||
// Convert to camelCase
|
// Convert to camelCase
|
||||||
const camelKey = this.toCamelCase(key);
|
const camelKey = this.toCamelCase(key);
|
||||||
config[camelKey] = parsedValue;
|
config[camelKey] = parsedValue;
|
||||||
|
} else if (this.options.nestedDelimiter && this.options.nestedDelimiter !== '_' && key.includes(this.options.nestedDelimiter)) {
|
||||||
|
// Handle nested delimiter (e.g., APP__NAME -> { APP: { NAME: value } })
|
||||||
|
const parts = key.split(this.options.nestedDelimiter);
|
||||||
|
this.setNestedValue(config, parts, parsedValue);
|
||||||
} else {
|
} else {
|
||||||
// Convert to nested structure based on underscores
|
// Convert to nested structure based on underscores, or keep as-is if no underscores
|
||||||
|
if (key.includes('_')) {
|
||||||
const path = key.toLowerCase().split('_');
|
const path = key.toLowerCase().split('_');
|
||||||
this.setNestedValue(config, path, parsedValue);
|
this.setNestedValue(config, path, parsedValue);
|
||||||
|
} else {
|
||||||
|
// Single key without underscores - keep original case
|
||||||
|
config[key] = parsedValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Skip environment variables that can't be set (readonly properties)
|
// Skip environment variables that can't be set (readonly properties)
|
||||||
|
|
@ -114,6 +131,53 @@ export class EnvLoader implements ConfigLoader {
|
||||||
.replace(/_([a-z])/g, (_, char) => char.toUpperCase());
|
.replace(/_([a-z])/g, (_, char) => char.toUpperCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getProviderMapping(envKey: string): { path: string[] } | null {
|
||||||
|
// Provider-specific and special environment variable mappings
|
||||||
|
const providerMappings: Record<string, string[]> = {
|
||||||
|
// WebShare provider mappings
|
||||||
|
'WEBSHARE_API_KEY': ['webshare', 'apiKey'],
|
||||||
|
'WEBSHARE_API_URL': ['webshare', 'apiUrl'],
|
||||||
|
'WEBSHARE_ENABLED': ['webshare', 'enabled'],
|
||||||
|
|
||||||
|
// EOD provider mappings
|
||||||
|
'EOD_API_KEY': ['providers', 'eod', 'apiKey'],
|
||||||
|
'EOD_BASE_URL': ['providers', 'eod', 'baseUrl'],
|
||||||
|
'EOD_TIER': ['providers', 'eod', 'tier'],
|
||||||
|
'EOD_ENABLED': ['providers', 'eod', 'enabled'],
|
||||||
|
'EOD_PRIORITY': ['providers', 'eod', 'priority'],
|
||||||
|
|
||||||
|
// Interactive Brokers provider mappings
|
||||||
|
'IB_GATEWAY_HOST': ['providers', 'ib', 'gateway', 'host'],
|
||||||
|
'IB_GATEWAY_PORT': ['providers', 'ib', 'gateway', 'port'],
|
||||||
|
'IB_CLIENT_ID': ['providers', 'ib', 'gateway', 'clientId'],
|
||||||
|
'IB_ACCOUNT': ['providers', 'ib', 'account'],
|
||||||
|
'IB_MARKET_DATA_TYPE': ['providers', 'ib', 'marketDataType'],
|
||||||
|
'IB_ENABLED': ['providers', 'ib', 'enabled'],
|
||||||
|
'IB_PRIORITY': ['providers', 'ib', 'priority'],
|
||||||
|
|
||||||
|
// QuoteMedia provider mappings
|
||||||
|
'QM_USERNAME': ['providers', 'qm', 'username'],
|
||||||
|
'QM_PASSWORD': ['providers', 'qm', 'password'],
|
||||||
|
'QM_BASE_URL': ['providers', 'qm', 'baseUrl'],
|
||||||
|
'QM_WEBMASTER_ID': ['providers', 'qm', 'webmasterId'],
|
||||||
|
'QM_ENABLED': ['providers', 'qm', 'enabled'],
|
||||||
|
'QM_PRIORITY': ['providers', 'qm', 'priority'],
|
||||||
|
|
||||||
|
// Yahoo Finance provider mappings
|
||||||
|
'YAHOO_BASE_URL': ['providers', 'yahoo', 'baseUrl'],
|
||||||
|
'YAHOO_COOKIE_JAR': ['providers', 'yahoo', 'cookieJar'],
|
||||||
|
'YAHOO_CRUMB': ['providers', 'yahoo', 'crumb'],
|
||||||
|
'YAHOO_ENABLED': ['providers', 'yahoo', 'enabled'],
|
||||||
|
'YAHOO_PRIORITY': ['providers', 'yahoo', 'priority'],
|
||||||
|
|
||||||
|
// Special mappings to avoid conflicts
|
||||||
|
'DEBUG_MODE': ['debug'],
|
||||||
|
};
|
||||||
|
|
||||||
|
const mapping = providerMappings[envKey];
|
||||||
|
return mapping ? { path: mapping } : null;
|
||||||
|
}
|
||||||
|
|
||||||
private parseValue(value: string): unknown {
|
private parseValue(value: string): unknown {
|
||||||
if (!this.options.parseValues && !this.options.parseJson) {
|
if (!this.options.parseValues && !this.options.parseJson) {
|
||||||
return value;
|
return value;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue