@@ -119,6 +119,39 @@ async function findAvailablePort(startPort: number): Promise<number> {
119119 throw new Error ( `No available ports found starting from ${ startPort } ` ) ;
120120}
121121
122+ /**
123+ * Update or add an environment variable in .env file content
124+ */
125+ function updateEnvVariable ( envContent : string , key : string , value : string ) : string {
126+ const lines = envContent . split ( '\n' ) ;
127+ const updatedLines : string [ ] = [ ] ;
128+ let keyFound = false ;
129+
130+ for ( const line of lines ) {
131+ const trimmedLine = line . trim ( ) ;
132+ // Skip empty lines and comments
133+ if ( ! trimmedLine || trimmedLine . startsWith ( '#' ) ) {
134+ updatedLines . push ( line ) ;
135+ continue ;
136+ }
137+
138+ // Check if this line contains the key we're looking for
139+ if ( trimmedLine . startsWith ( `${ key } =` ) ) {
140+ updatedLines . push ( `${ key } =${ value } ` ) ;
141+ keyFound = true ;
142+ } else {
143+ updatedLines . push ( line ) ;
144+ }
145+ }
146+
147+ // If key wasn't found, add it at the end
148+ if ( ! keyFound ) {
149+ updatedLines . push ( `${ key } =${ value } ` ) ;
150+ }
151+
152+ return updatedLines . join ( '\n' ) ;
153+ }
154+
122155async function detectPythonFramework ( backendPath : string ) : Promise < string > {
123156 // Check for common Python files
124157 const pythonFiles = [ 'main.py' , 'app.py' , 'server.py' , 'application.py' ] ;
@@ -290,6 +323,61 @@ async function executeAppLocalNode({
290323 // Ensure backend directory exists and has proper structure
291324 await ensureBackendDirectory ( backendPath ) ;
292325
326+ // Create/update environment files for fullstack integration
327+ try {
328+ logger . info ( `Setting up environment files for fullstack app ${ appId } ` ) ;
329+
330+ // Create/update backend .env file
331+ const backendEnvPath = path . join ( backendPath , '.env' ) ;
332+ let backendEnvContent = '' ;
333+
334+ // Read existing .env file if it exists
335+ if ( fs . existsSync ( backendEnvPath ) ) {
336+ backendEnvContent = fs . readFileSync ( backendEnvPath , 'utf-8' ) ;
337+ }
338+
339+ // Update or add PORT variable
340+ backendEnvContent = updateEnvVariable ( backendEnvContent , 'PORT' , backendPort . toString ( ) ) ;
341+
342+ // Update or add FRONTEND_URL variable for CORS
343+ const frontendUrl = `http://localhost:${ frontendPort } ` ;
344+ backendEnvContent = updateEnvVariable ( backendEnvContent , 'FRONTEND_URL' , frontendUrl ) ;
345+
346+ // Write backend .env file
347+ await fsPromises . writeFile ( backendEnvPath , backendEnvContent , 'utf-8' ) ;
348+ logger . info ( `Updated backend .env file at ${ backendEnvPath } ` ) ;
349+
350+ // Create/update frontend .env file
351+ const frontendEnvPath = path . join ( frontendPath , '.env' ) ;
352+ let frontendEnvContent = '' ;
353+
354+ // Read existing .env file if it exists
355+ if ( fs . existsSync ( frontendEnvPath ) ) {
356+ frontendEnvContent = fs . readFileSync ( frontendEnvPath , 'utf-8' ) ;
357+ }
358+
359+ // Update or add VITE_API_URL variable for frontend to call backend
360+ const backendApiUrl = `http://localhost:${ backendPort } ` ;
361+ frontendEnvContent = updateEnvVariable ( frontendEnvContent , 'VITE_API_URL' , backendApiUrl ) ;
362+
363+ // Write frontend .env file
364+ await fsPromises . writeFile ( frontendEnvPath , frontendEnvContent , 'utf-8' ) ;
365+ logger . info ( `Updated frontend .env file at ${ frontendEnvPath } ` ) ;
366+
367+ safeSend ( event . sender , "app:output" , {
368+ type : "stdout" ,
369+ message : `🔧 Environment files configured - Backend API URL: ${ backendApiUrl } , Frontend CORS URL: ${ frontendUrl } ` ,
370+ appId,
371+ } ) ;
372+ } catch ( error ) {
373+ logger . error ( `Failed to set up environment files for fullstack app ${ appId } :` , error ) ;
374+ safeSend ( event . sender , "app:output" , {
375+ type : "stdout" ,
376+ message : `⚠️ Warning: Failed to configure environment files. Manual configuration may be needed.` ,
377+ appId,
378+ } ) ;
379+ }
380+
293381 // Ensure frontend dependencies are installed
294382 try {
295383 logger . info ( `Ensuring frontend dependencies are installed in ${ frontendPath } ` ) ;
0 commit comments