@@ -2,94 +2,54 @@ import { config } from "dotenv";
2
2
import fs from "fs" ;
3
3
import path from "path" ;
4
4
5
- export interface Settings {
6
- MAIN_WALLET_ADDRESS : string ;
7
- OPENAI_API_KEY : string ;
8
- USE_OPENAI_EMBEDDING : string ;
9
- SYSTEM_PROMPT : string ;
10
- OPENROUTER_MODEL : string ;
11
- SMALL_OPENROUTER_MODEL : string ;
12
- MEDIUM_OPENROUTER_MODEL : string ;
13
- LARGE_OPENROUTER_MODEL : string ;
14
- OLLAMA_MODEL : string ;
15
- LARGE_OLLAMA_MODEL : string ;
16
- MEDIUM_OLLAMA_MODEL : string ;
17
- SMALL_OLLAMA_MODEL : string ;
18
- OLLAMA_SERVER_URL : string ;
19
- OLLAMA_EMBEDDING_MODEL : string ;
20
- RPC_URL : string ;
21
- BASE_MINT : string ;
22
- BACKEND_URL : string ;
23
- BACKEND_TOKEN : string ;
24
- BIRDEYE_API_KEY : string ;
25
- HELIUS_API_KEY : string ;
26
- SERVER_PORT : string ;
27
- CAPSOLVER_API_KEY : string ;
28
- CUDA_PATH : string ;
29
- }
30
-
31
5
/**
32
6
* Recursively searches for a .env file starting from the current directory
33
7
* and moving up through parent directories
8
+ * @param {string } [startDir=process.cwd()] - Starting directory for the search
9
+ * @returns {string|null } Path to the nearest .env file or null if not found
34
10
*/
35
11
export function findNearestEnvFile ( startDir = process . cwd ( ) ) {
36
- console . error ( 'DEBUG - Starting env file search' ) ;
37
- console . error ( 'DEBUG - Current working directory:' , process . cwd ( ) ) ;
38
-
39
- // In test environment, use the known working path
40
- if ( process . env . NODE_ENV === 'test' || process . env . VITEST ) {
41
- const testPath = path . join ( process . cwd ( ) , '.env.test' ) ;
42
- console . error ( 'DEBUG - Checking test path:' , testPath ) ;
43
- if ( fs . existsSync ( testPath ) ) {
44
- console . error ( 'DEBUG - Found test env file at:' , testPath ) ;
45
- return testPath ;
46
- }
47
- }
48
-
49
- // Look for regular .env
50
12
let currentDir = startDir ;
51
- const envFile = process . env . NODE_ENV === 'test' ? '.env.test' : '.env' ;
52
13
14
+ // Continue searching until we reach the root directory
53
15
while ( currentDir !== path . parse ( currentDir ) . root ) {
54
- const envPath = path . join ( currentDir , envFile ) ;
55
- console . error ( 'DEBUG - Checking path:' , envPath ) ;
16
+ const envPath = path . join ( currentDir , ".env" ) ;
17
+
56
18
if ( fs . existsSync ( envPath ) ) {
57
- console . error ( 'DEBUG - Found env file at:' , envPath ) ;
58
19
return envPath ;
59
20
}
21
+
22
+ // Move up to parent directory
60
23
currentDir = path . dirname ( currentDir ) ;
61
24
}
62
25
63
- console . error ( 'DEBUG - No env file found after checking all paths' ) ;
64
- console . error ( 'DEBUG - Final cwd:' , process . cwd ( ) ) ;
65
- console . error ( 'DEBUG - Final NODE_ENV:' , process . env . NODE_ENV ) ;
66
- return null ;
26
+ // Check root directory as well
27
+ const rootEnvPath = path . join ( path . parse ( currentDir ) . root , ".env" ) ;
28
+ return fs . existsSync ( rootEnvPath ) ? rootEnvPath : null ;
67
29
}
68
30
69
31
/**
70
32
* Loads environment variables from the nearest .env file
33
+ * @returns {Object } Environment variables object
34
+ * @throws {Error } If no .env file is found
71
35
*/
72
36
export function loadEnvConfig ( ) {
73
- console . error ( 'DEBUG - loadEnvConfig called' ) ;
74
- console . error ( 'DEBUG - Current working directory:' , process . cwd ( ) ) ;
75
- console . error ( 'DEBUG - NODE_ENV:' , process . env . NODE_ENV ) ;
76
-
77
37
const envPath = findNearestEnvFile ( ) ;
78
38
79
39
if ( ! envPath ) {
80
40
throw new Error ( "No .env file found in current or parent directories." ) ;
81
41
}
82
42
83
- console . error ( 'DEBUG - Loading env file from:' , envPath ) ;
43
+ // Load the . env file
84
44
const result = config ( { path : envPath } ) ;
45
+
85
46
if ( result . error ) {
86
47
throw new Error ( `Error loading .env file: ${ result . error } ` ) ;
87
48
}
88
49
89
- console . error ( 'DEBUG - Successfully loaded env file' ) ;
90
-
91
- // Populate the settings object with the environment variables
92
- Object . assign ( settings , process . env ) ;
50
+ console . log ( `Loaded .env file from: ${ envPath } ` ) ;
51
+ return process . env ;
93
52
}
94
53
95
- export const settings : Settings = { } as Settings ;
54
+ export const settings = loadEnvConfig ( ) ;
55
+ export default settings ;
0 commit comments