@@ -6,23 +6,37 @@ const fs = require('fs');
66const { exec } = require ( 'child_process' ) ;
77const bodyParser = require ( 'body-parser' ) ;
88const cors = require ( 'cors' ) ;
9+ require ( 'dotenv' ) . config ( ) ;
910
1011// Initialize Express app
1112const app = express ( ) ;
1213const server = http . createServer ( app ) ;
13- const io = socketIo ( server ) ;
14+ const io = socketIo ( server , {
15+ cors : {
16+ origin : process . env . CORS_ORIGIN || '*' ,
17+ methods : [ 'GET' , 'POST' ]
18+ }
19+ } ) ;
1420
1521// Middleware
16- app . use ( cors ( ) ) ;
22+ app . use ( cors ( {
23+ origin : process . env . CORS_ORIGIN || '*'
24+ } ) ) ;
1725app . use ( bodyParser . json ( ) ) ;
1826app . use ( express . static ( path . join ( __dirname , 'dist' ) ) ) ;
1927
2028// Temporary file storage
21- const TEMP_DIR = path . join ( __dirname , 'temp' ) ;
29+ const TEMP_DIR = process . env . TEMP_DIR || path . join ( __dirname , 'temp' ) ;
2230if ( ! fs . existsSync ( TEMP_DIR ) ) {
2331 fs . mkdirSync ( TEMP_DIR ) ;
2432}
2533
34+ // Logs directory
35+ const LOGS_DIR = process . env . LOGS_DIR || path . join ( __dirname , 'logs' ) ;
36+ if ( ! fs . existsSync ( LOGS_DIR ) ) {
37+ fs . mkdirSync ( LOGS_DIR ) ;
38+ }
39+
2640// Socket.IO connection
2741io . on ( 'connection' , ( socket ) => {
2842 console . log ( 'Client connected' ) ;
@@ -34,13 +48,29 @@ io.on('connection', (socket) => {
3448 // Save code to temporary file
3549 fs . writeFileSync ( tempFile , code ) ;
3650
51+ // Build command with environment variables
52+ const analyzeCommand = `reactstream analyze ${ tempFile } ${
53+ process . env . VERBOSE_OUTPUT === 'true' ? '--verbose' : ''
54+ } ${
55+ process . env . AUTO_FIX === 'true' ? '--fix' : ''
56+ } ${
57+ process . env . ENABLE_DEBUG === 'true' ? '--debug' : ''
58+ } `;
59+
3760 // Run ReactStream analyze command
38- exec ( `reactstream analyze ${ tempFile } --verbose` , ( error , stdout , stderr ) => {
61+ exec ( analyzeCommand , ( error , stdout , stderr ) => {
3962 socket . emit ( 'analysis-result' , {
4063 output : stdout ,
4164 error : stderr || ( error ? error . message : null )
4265 } ) ;
4366
67+ // Log analysis results
68+ const timestamp = new Date ( ) . toISOString ( ) ;
69+ fs . appendFileSync (
70+ path . join ( LOGS_DIR , 'analysis.log' ) ,
71+ `[${ timestamp } ] Analysis performed\n${ stdout } \n${ stderr || '' } \n\n`
72+ ) ;
73+
4474 // Clean up temp file
4575 fs . unlinkSync ( tempFile ) ;
4676 } ) ;
@@ -54,14 +84,30 @@ io.on('connection', (socket) => {
5484 fs . writeFileSync ( tempFile , code ) ;
5585
5686 // Run ReactStream serve command (non-blocking)
57- const serveProcess = exec ( `reactstream serve ${ tempFile } --port=3000` ) ;
87+ const serveProcess = exec (
88+ `reactstream serve ${ tempFile } --port=${ process . env . DEV_SERVER_PORT || 3000 } `
89+ ) ;
5890
5991 serveProcess . stdout . on ( 'data' , ( data ) => {
6092 socket . emit ( 'serve-output' , { type : 'stdout' , data } ) ;
93+
94+ // Log serve output
95+ const timestamp = new Date ( ) . toISOString ( ) ;
96+ fs . appendFileSync (
97+ path . join ( LOGS_DIR , 'serve.log' ) ,
98+ `[${ timestamp } ] STDOUT: ${ data } \n`
99+ ) ;
61100 } ) ;
62101
63102 serveProcess . stderr . on ( 'data' , ( data ) => {
64103 socket . emit ( 'serve-output' , { type : 'stderr' , data } ) ;
104+
105+ // Log serve errors
106+ const timestamp = new Date ( ) . toISOString ( ) ;
107+ fs . appendFileSync (
108+ path . join ( LOGS_DIR , 'serve.log' ) ,
109+ `[${ timestamp } ] STDERR: ${ data } \n`
110+ ) ;
65111 } ) ;
66112
67113 socket . on ( 'disconnect' , ( ) => {
@@ -80,7 +126,16 @@ app.post('/api/analyze', (req, res) => {
80126
81127 fs . writeFileSync ( tempFile , code ) ;
82128
83- exec ( `reactstream analyze ${ tempFile } --verbose` , ( error , stdout , stderr ) => {
129+ // Build command with environment variables
130+ const analyzeCommand = `reactstream analyze ${ tempFile } ${
131+ process . env . VERBOSE_OUTPUT === 'true' ? '--verbose' : ''
132+ } ${
133+ process . env . AUTO_FIX === 'true' ? '--fix' : ''
134+ } ${
135+ process . env . ENABLE_DEBUG === 'true' ? '--debug' : ''
136+ } `;
137+
138+ exec ( analyzeCommand , ( error , stdout , stderr ) => {
84139 res . json ( {
85140 output : stdout ,
86141 error : stderr || ( error ? error . message : null )
@@ -90,13 +145,26 @@ app.post('/api/analyze', (req, res) => {
90145 } ) ;
91146} ) ;
92147
148+ // Add this to server.js in the API routes section
149+ app . get ( '/api/config' , ( req , res ) => {
150+ res . json ( {
151+ devServerPort : process . env . DEV_SERVER_PORT || 3000 ,
152+ debug : process . env . ENABLE_DEBUG === 'true' ,
153+ autoFix : process . env . AUTO_FIX === 'true' ,
154+ verbose : process . env . VERBOSE_OUTPUT === 'true'
155+ } ) ;
156+ } ) ;
157+
158+
93159// Serve the main HTML file for all routes
94160app . get ( '*' , ( req , res ) => {
95161 res . sendFile ( path . join ( __dirname , 'dist' , 'index.html' ) ) ;
96162} ) ;
97163
98164// Start the server
99- const PORT = process . env . PORT || 80 ;
165+ const PORT = process . env . SERVER_PORT || 80 ;
100166server . listen ( PORT , ( ) => {
101167 console . log ( `Server running on port ${ PORT } ` ) ;
168+ console . log ( `Environment: ${ process . env . NODE_ENV } ` ) ;
169+ console . log ( `Dev server port: ${ process . env . DEV_SERVER_PORT || 3000 } ` ) ;
102170} ) ;
0 commit comments