(filepath = '.env')
| 13 | } |
| 14 | |
| 15 | envFile(filepath = '.env') { |
| 16 | let content = ''; |
| 17 | try { |
| 18 | content = fs.readFileSync(filepath, 'utf8'); |
| 19 | } catch { |
| 20 | // ignore error |
| 21 | } |
| 22 | const values = content |
| 23 | .split('\n') |
| 24 | .map((line) => line.trim()) |
| 25 | .filter((line) => line && !line.startsWith('#')) |
| 26 | .map((line) => { |
| 27 | const i = line.indexOf('='); |
| 28 | if (i < 0) return []; |
| 29 | const key = line.slice(0, i).trim(); |
| 30 | let value = line.slice(i + 1).trim(); |
| 31 | // Note: escaped characters are not supported |
| 32 | if (/^(['"]).*\1$/.test(value)) value = value.slice(1, -1); |
| 33 | return [key, value]; |
| 34 | }) |
| 35 | .reduce((prev, [key, value]) => { |
| 36 | if (key) prev[key] = value; |
| 37 | return prev; |
| 38 | }, {}); |
| 39 | return this.add(values); |
| 40 | } |
| 41 | |
| 42 | get(key, def) { |
| 43 | return this.data[key] ?? def; |
no test coverage detected