(file: string)
| 455 | } |
| 456 | |
| 457 | function readEnvFile(file: string): { [key: string]: string } { |
| 458 | if (!fs.existsSync(file)) { |
| 459 | return {}; |
| 460 | } |
| 461 | |
| 462 | const buffer = stripBOM(fs.readFileSync(file, 'utf8')); |
| 463 | const env: { [key: string]: string } = {}; |
| 464 | for (const line of buffer.split('\n')) { |
| 465 | const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/); |
| 466 | if (!r) { |
| 467 | continue; |
| 468 | } |
| 469 | |
| 470 | let value = r[2] || ''; |
| 471 | // .env variables never overwrite existing variables (see #21169) |
| 472 | if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') { |
| 473 | value = value.replace(/\\n/gm, '\n'); |
| 474 | } |
| 475 | env[r[1]] = value.replace(/(^['"]|['"]$)/g, ''); |
| 476 | } |
| 477 | |
| 478 | return env; |
| 479 | } |
| 480 | |
| 481 | function stripBOM(s: string): string { |
| 482 | if (s && s[0] === '\uFEFF') { |
no test coverage detected