11import { readFileSync , existsSync } from 'node:fs' ;
22import { join } from 'node:path' ;
3- import { parse as parseDotenv } from 'dotenv' ;
43import type { EnvironmentInfo , EnvironmentCheckResult , DoctorOptions } from '../types.js' ;
54
5+ function parseEnvFile ( content : string ) : Record < string , string > {
6+ const result : Record < string , string > = { } ;
7+
8+ for ( const line of content . split ( '\n' ) ) {
9+ const trimmed = line . trim ( ) ;
10+ if ( ! trimmed || trimmed . startsWith ( '#' ) ) continue ;
11+
12+ // Strip optional `export ` prefix
13+ const entry = trimmed . startsWith ( 'export ' ) ? trimmed . slice ( 7 ) : trimmed ;
14+ const eqIndex = entry . indexOf ( '=' ) ;
15+ if ( eqIndex === - 1 ) continue ;
16+
17+ const key = entry . slice ( 0 , eqIndex ) . trim ( ) ;
18+ let value = entry . slice ( eqIndex + 1 ) . trim ( ) ;
19+
20+ // Remove surrounding quotes (single or double)
21+ if ( ( value . startsWith ( '"' ) && value . endsWith ( '"' ) ) || ( value . startsWith ( "'" ) && value . endsWith ( "'" ) ) ) {
22+ value = value . slice ( 1 , - 1 ) ;
23+ }
24+
25+ result [ key ] = value ;
26+ }
27+
28+ return result ;
29+ }
30+
631/**
732 * Load environment variables from project's .env files.
833 * Priority: .env.local > .env (matching Next.js/Vite conventions)
9- * Uses dotenv parser for proper handling of quotes, multiline, exports, etc.
1034 */
1135function loadProjectEnv ( installDir : string ) : Record < string , string > {
1236 const env : Record < string , string > = { } ;
@@ -19,8 +43,7 @@ function loadProjectEnv(installDir: string): Record<string, string> {
1943 if ( existsSync ( filePath ) ) {
2044 try {
2145 const content = readFileSync ( filePath , 'utf-8' ) ;
22- const parsed = parseDotenv ( content ) ;
23- Object . assign ( env , parsed ) ;
46+ Object . assign ( env , parseEnvFile ( content ) ) ;
2447 } catch {
2548 // Ignore read errors
2649 }
0 commit comments