< Summary - Envilder CLI

Information
Class: src/envilder/core/infrastructure/variableStore/EnvFileParser.ts
Assembly: Default
File(s): src/envilder/core/infrastructure/variableStore/EnvFileParser.ts
Tag: 561_41292037308
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 38
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/infrastructure/variableStore/EnvFileParser.ts

#LineLine coverage
 1/**
 2 * dotenv's `LINE` grammar and `parse`, copied from dotenv 17 with two changes.
 3 * The key class also admits `/`, the hierarchy separator map files use
 4 * (`Database/Password`). dotenv skips such a line, so reading the files we
 5 * write through it would lose those keys, both when merging into an existing
 6 * file and when checking the result before it reaches disk.
 7 *
 8 * And the blanks before a key stop at a line break. dotenv's `\s*` there lets
 9 * every blank line start an attempt that scans the whole run after it, so a
 10 * file of blank lines takes quadratic time: about 100 s for 200 KB. The match
 11 * that begins on the key's own line captures the same key and value, so the
 12 * result does not change. dotenv 18 keeps the same grammar.
 13 *
 14 * Everything else is dotenv's, value rules included, so a file without `/` in
 15 * its keys parses exactly as dotenv parses it. The conformance tests pin that.
 16 */
 17const LINE =
 1418  /(?:^|^)[^\S\n\r\u2028\u2029]*(?:export\s+)?([\w./-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:
 19
 20export function parseEnvFile(src: string): Record<string, string> {
 21  // A plain object on purpose, as in dotenv: `__proto__` must vanish here too,
 22  // which is why the name rule rejects it.
 48323  const obj: Record<string, string> = {};
 48324  const lines = src.replace(/\r\n?/gm, '\n');
 25
 48326  for (const match of lines.matchAll(LINE)) {
 64027    const key = match[1];
 64028    let value = (match[2] || '').trim();
 64029    const maybeQuote = value[0];
 64030    value = value.replace(/^(['"`])([\s\S]*)\1$/gm, '$2');
 64031    if (maybeQuote === '"') {
 9832      value = value.replace(/\\n/g, '\n').replace(/\\r/g, '\r');
 33    }
 64034    obj[key] = value;
 35  }
 36
 48337  return obj;
 38}

Methods/Properties

parseEnvFile