* Parse a .env file into a plain object. * Handles comments, blank lines, and quoted values.
(filePath)
| 329 | * Handles comments, blank lines, and quoted values. |
| 330 | */ |
| 331 | function parseEnvFile(filePath) { |
| 332 | const env = {}; |
| 333 | if (!existsSync(filePath)) return env; |
| 334 | |
| 335 | const lines = readFileSync(filePath, 'utf8').split('\n'); |
| 336 | for (const line of lines) { |
| 337 | const trimmed = line.trim(); |
| 338 | if (!trimmed || trimmed.startsWith('#')) continue; |
| 339 | |
| 340 | const eqIdx = trimmed.indexOf('='); |
| 341 | if (eqIdx === -1) continue; |
| 342 | |
| 343 | const key = trimmed.slice(0, eqIdx).trim(); |
| 344 | let value = trimmed.slice(eqIdx + 1).trim(); |
| 345 | |
| 346 | // Strip matching quotes (single or double) |
| 347 | if ( |
| 348 | (value.startsWith('"') && value.endsWith('"')) || |
| 349 | (value.startsWith("'") && value.endsWith("'")) |
| 350 | ) { |
| 351 | value = value.slice(1, -1); |
| 352 | } |
| 353 | |
| 354 | if (key) { |
| 355 | env[key] = value; |
| 356 | } |
| 357 | } |
| 358 | return env; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Ensure ~/.autoforge/.env exists. On first run, copy .env.example |