@@ -183,16 +183,30 @@ export default class {
183183
184184 if ( existsSync ( exampleEnvPath ) ) {
185185 const env = await readFile ( exampleEnvPath , 'utf-8' )
186- let lines = env . split ( / \r ? \n / )
187- for ( let i = 0 ; i < lines . length ; i ++ ) {
188- const line = lines [ i ]
189- const key = line . split ( '=' ) . at ( 0 ) ?? ''
190- if ( key === '' || line === '' || line . trim ( ) . startsWith ( '#' ) ) continue
191- if ( ! allowed . includes ( key ) ) delete lines [ i ]
186+
187+ const kept : string [ ] = [ ]
188+ for ( const line of env . split ( / \r ? \n / ) ) {
189+ const trimmed = line . trim ( )
190+ const isBlank = trimmed === ''
191+ const isComment = trimmed . startsWith ( '#' )
192+
193+ // Drop disallowed variable lines outright (keep comments/blank lines).
194+ if ( ! isBlank && ! isComment ) {
195+ const key = line . split ( '=' ) . at ( 0 ) ?. trim ( ) ?? ''
196+ if ( ! allowed . includes ( key ) ) continue
197+ }
198+
199+ // Collapse the runs of blank lines left behind by removed vars.
200+ if ( isBlank && kept . at ( - 1 ) ?. trim ( ) === '' ) continue
201+
202+ kept . push ( line )
192203 }
193- lines = lines . slice ( lines . findIndex ( v => v ) , lines . findLastIndex ( v => v ) + 1 )
194204
195- await writeFile ( exampleEnvPath , lines . join ( '\n' ) )
205+ // Trim leading/trailing blank lines.
206+ while ( kept . length && kept [ 0 ] . trim ( ) === '' ) kept . shift ( )
207+ while ( kept . length && kept . at ( - 1 ) ! . trim ( ) === '' ) kept . pop ( )
208+
209+ await writeFile ( exampleEnvPath , kept . join ( '\n' ) + '\n' )
196210 await copyFile ( exampleEnvPath , envPath )
197211 }
198212 }
0 commit comments