diff --git a/setup.ts b/setup.ts index da0044c036..7ee0655af2 100644 --- a/setup.ts +++ b/setup.ts @@ -10,11 +10,16 @@ import { askForCustomPort } from './src/setup/askForCustomPort/askForCustomPort' // Update the .env file with new values const updateEnvFile = (key: string, value: string): void => { const currentEnvContent = fs.readFileSync('.env', 'utf8'); - const updatedEnvContent = currentEnvContent.replace( - new RegExp(`^${key}=.*$`, 'm'), - `${key}=${value}`, - ); - fs.writeFileSync('.env', updatedEnvContent, 'utf8'); + const keyRegex = new RegExp(`^${key}=.*$`, 'm'); + if (keyRegex.test(currentEnvContent)) { + const updatedEnvContent = currentEnvContent.replace( + keyRegex, + `${key}=${value}`, + ); + fs.writeFileSync('.env', updatedEnvContent, 'utf8'); + } else { + fs.appendFileSync('.env', `\n${key}=${value}`, 'utf8'); + } }; // Handle .env file creation or validation