diff --git a/setup.ts b/setup.ts index d6153553eb..2a6c437fa3 100644 --- a/setup.ts +++ b/setup.ts @@ -74,35 +74,26 @@ export async function main(): Promise { const url = new URL(endpoint); isConnected = await checkConnection(url.origin); } + const envPath = '.env'; + const currentEnvContent = fs.readFileSync(envPath, 'utf8'); + const talawaApiUrl = dotenv.parse(currentEnvContent).REACT_APP_TALAWA_URL; - const talawaApiUrl = dotenv.parse( - fs.readFileSync('.env'), - ).REACT_APP_TALAWA_URL; - - fs.readFile('.env', 'utf8', (err, data) => { - const result = data.replace( - `REACT_APP_TALAWA_URL=${talawaApiUrl}`, - `REACT_APP_TALAWA_URL=${endpoint}`, - ); - fs.writeFileSync('.env', result, 'utf8'); - }); + const updatedEnvContent = currentEnvContent.replace( + `REACT_APP_TALAWA_URL=${talawaApiUrl}`, + `REACT_APP_TALAWA_URL=${endpoint}`, + ); + fs.writeFileSync(envPath, updatedEnvContent, 'utf8'); const websocketUrl = endpoint.replace(/^http(s)?:\/\//, 'ws$1://'); - const currentWebSocketUrl = dotenv.parse( - fs.readFileSync('.env'), - ).REACT_APP_BACKEND_WEBSOCKET_URL; + const currentWebSocketUrl = + dotenv.parse(updatedEnvContent).REACT_APP_BACKEND_WEBSOCKET_URL; - fs.readFile('.env', 'utf8', (err, data) => { - if (err) { - console.error('Error reading .env file:', err); - process.exit(1); - } - const result = data.replace( - `REACT_APP_BACKEND_WEBSOCKET_URL=${currentWebSocketUrl}`, - `REACT_APP_BACKEND_WEBSOCKET_URL=${websocketUrl}`, - ); - fs.writeFileSync('.env', result, 'utf8'); - }); + const finalEnvContent = updatedEnvContent.replace( + `REACT_APP_BACKEND_WEBSOCKET_URL=${currentWebSocketUrl}`, + `REACT_APP_BACKEND_WEBSOCKET_URL=${websocketUrl}`, + ); + + fs.writeFileSync(envPath, finalEnvContent, 'utf8'); } const { shouldUseRecaptcha } = await inquirer.prompt({