(text: string)
| 75 | * @returns The parsed object. |
| 76 | */ |
| 77 | export function parse(text: string): Record<string, string> { |
| 78 | const env: Record<string, string> = Object.create(null); |
| 79 | |
| 80 | const keysForExpandCheck = []; |
| 81 | |
| 82 | for (const match of text.matchAll(KEY_VALUE_REGEXP)) { |
| 83 | const { key, interpolated, notInterpolated, unquoted } = match |
| 84 | ?.groups as LineParseResult; |
| 85 | |
| 86 | if (!VALID_KEY_REGEXP.test(key)) { |
| 87 | // deno-lint-ignore no-console |
| 88 | console.warn( |
| 89 | `Ignored the key "${key}" as it is not a valid identifier: The key need to match the pattern /^[a-zA-Z_][a-zA-Z0-9_]*$/.`, |
| 90 | ); |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | if (unquoted) { |
| 95 | keysForExpandCheck.push(key); |
| 96 | } |
| 97 | |
| 98 | env[key] = typeof notInterpolated === "string" |
| 99 | ? notInterpolated |
| 100 | : typeof interpolated === "string" |
| 101 | ? expandCharacters(interpolated) |
| 102 | : unquoted.trim(); |
| 103 | } |
| 104 | |
| 105 | //https://github.com/motdotla/dotenv-expand/blob/ed5fea5bf517a09fd743ce2c63150e88c8a5f6d1/lib/main.js#L23 |
| 106 | const variablesMap = { ...env }; |
| 107 | |
| 108 | for (const key of keysForExpandCheck) { |
| 109 | env[key] = expand(env[key]!, variablesMap); |
| 110 | } |
| 111 | |
| 112 | return env; |
| 113 | } |
no test coverage detected