@@ -18,74 +18,49 @@ const fs = require('fs');
1818const path = require ( 'path' ) ;
1919const os = require ( 'os' ) ;
2020
21+ function parseEnvFile ( filePath ) {
22+ if ( ! fs . existsSync ( filePath ) ) return { } ;
23+ const content = fs . readFileSync ( filePath , 'utf8' ) ;
24+
25+ return content . split ( / \r ? \n / ) . reduce ( ( acc , line ) => {
26+ const trimmed = line . trim ( ) ;
27+ if ( ! trimmed || trimmed . startsWith ( '#' ) ) return acc ;
28+
29+ const [ key , ...valueParts ] = trimmed . split ( '=' ) ;
30+ if ( ! key ) return acc ;
31+
32+ let value = valueParts . join ( '=' ) . trim ( ) . replace ( / ^ ( [ ' " ] ) ( .* ) \1$ / , '$2' ) ;
33+ acc [ key . trim ( ) ] = value ;
34+ return acc ;
35+ } , { } ) ;
36+ }
37+
2138function loadEnv ( ) {
2239 try {
23- // Check if a local .env exists in the current working directory of the project
24- const localEnvPath = path . resolve ( process . cwd ( ) , '.env' ) ;
25- if ( fs . existsSync ( localEnvPath ) ) {
26- parseAndSetEnv ( localEnvPath ) ;
27- }
28-
29- // Resolve the extension name from gemini-extension.json
30- // Fallback to 'cloud-sql-postgresql' if we can't find it
3140 let extensionName = 'cloud-sql-postgresql' ;
32- try {
33- const projectRoot = path . resolve ( __dirname , '../../..' ) ;
34- const manifestPath = path . join ( projectRoot , 'gemini-extension.json' ) ;
35- if ( fs . existsSync ( manifestPath ) ) {
36- const manifest = JSON . parse ( fs . readFileSync ( manifestPath , 'utf8' ) ) ;
37- if ( manifest . name ) {
38- extensionName = manifest . name ;
39- }
40- }
41- } catch ( e ) {
42- // Ignore parsing errors, fallback is fine
41+ const projectRoot = path . resolve ( __dirname , '../../..' ) ;
42+ const manifestPath = path . join ( projectRoot , 'gemini-extension.json' ) ;
43+
44+ // Resolve Extension Name
45+ if ( fs . existsSync ( manifestPath ) ) {
46+ const manifest = JSON . parse ( fs . readFileSync ( manifestPath , 'utf8' ) ) ;
47+ extensionName = manifest . name || extensionName ;
4348 }
4449
45- // Load from ~/.gemini/extensions/<name>/.env
46- const homeDir = os . homedir ( ) ;
47- const geminiEnvPath = path . join ( homeDir , '.gemini' , 'extensions' , extensionName , '.env' ) ;
48- if ( fs . existsSync ( geminiEnvPath ) ) {
49- parseAndSetEnv ( geminiEnvPath ) ;
50+ // Define Paths
51+ const globalPath = path . join ( os . homedir ( ) , '.gemini' , 'extensions' , extensionName , '.env' ) ; // ~/.gemini/extensions/<name>/.env
52+ const localPath = path . join ( projectRoot , '.env' ) ; // .env
53+
54+ // Merge Environments (Global < Local)
55+ const finalEnv = { ...parseEnvFile ( globalPath ) , ...parseEnvFile ( localPath ) } ;
56+
57+ // Apply to process.env without overwriting existing shell vars
58+ for ( const [ key , value ] of Object . entries ( finalEnv ) ) {
59+ if ( ! ( key in process . env ) ) process . env [ key ] = value ;
5060 }
5161 } catch ( err ) {
5262 console . error ( "Warning: Failed to load extension environment variables:" , err . message ) ;
5363 }
5464}
5565
56- function parseAndSetEnv ( envPath ) {
57- const envFileContent = fs . readFileSync ( envPath , 'utf8' ) ;
58- const lines = envFileContent . split ( / \r ? \n / ) ;
59-
60- for ( const line of lines ) {
61- const trimmedLine = line . trim ( ) ;
62- // Ignore empty lines and comments
63- if ( ! trimmedLine || trimmedLine . startsWith ( '#' ) ) {
64- continue ;
65- }
66-
67- // Parse KEY=VALUE
68- const equalSignIndex = trimmedLine . indexOf ( '=' ) ;
69- if ( equalSignIndex === - 1 ) {
70- continue ;
71- }
72-
73- const key = trimmedLine . substring ( 0 , equalSignIndex ) . trim ( ) ;
74- let value = trimmedLine . substring ( equalSignIndex + 1 ) . trim ( ) ;
75-
76- // Remove surrounding quotes if present
77- if ( value . startsWith ( '"' ) && value . endsWith ( '"' ) ) {
78- value = value . substring ( 1 , value . length - 1 ) ;
79- } else if ( value . startsWith ( "'" ) && value . endsWith ( "'" ) ) {
80- value = value . substring ( 1 , value . length - 1 ) ;
81- }
82-
83- // Set if not already defined in process.env
84- if ( key && process . env [ key ] === undefined ) {
85- process . env [ key ] = value ;
86- }
87- }
88- }
89-
90- // Execute immediately when required
91- loadEnv ( ) ;
66+ loadEnv ( ) ;
0 commit comments