deno.land / std@0.224.0 / dotenv / parse.ts

View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
type LineParseResult = { key: string; unquoted: string; interpolated: string; notInterpolated: string;};
type CharactersMap = { [key: string]: string };
const RE_KEY_VALUE = /^\s*(?:export\s+)?(?<key>[a-zA-Z_]+[a-zA-Z0-9_]*?)\s*=[\ \t]*('\n?(?<notInterpolated>(.|\n)*?)\n?'|"\n?(?<interpolated>(.|\n)*?)\n?"|(?<unquoted>[^\n#]*)) *#*.*$/gm;
const RE_EXPAND_VALUE = /(\${(?<inBrackets>.+?)(\:-(?<inBracketsDefault>.+))?}|(?<!\\)\$(?<notInBrackets>\w+)(\:-(?<notInBracketsDefault>.+))?)/g;
function expandCharacters(str: string): string { const charactersMap: CharactersMap = { "\\n": "\n", "\\r": "\r", "\\t": "\t", };
return str.replace( /\\([nrt])/g, ($1: keyof CharactersMap): string => charactersMap[$1] || "", );}
function expand(str: string, variablesMap: { [key: string]: string }): string { if (RE_EXPAND_VALUE.test(str)) { return expand( str.replace(RE_EXPAND_VALUE, function (...params) { const { inBrackets, inBracketsDefault, notInBrackets, notInBracketsDefault, } = params[params.length - 1]; const expandValue = inBrackets || notInBrackets; const defaultValue = inBracketsDefault || notInBracketsDefault;
let value: string | undefined = variablesMap[expandValue]; if (value === undefined) { value = Deno.env.get(expandValue); } return value === undefined ? expand(defaultValue, variablesMap) : value; }), variablesMap, ); } else { return str; }}
/** * Parse `.env` file output in an object. * * @example * ```ts * import { parse } from "https://deno.land/std@$STD_VERSION/dotenv/parse.ts"; * * const env = parse("GREETING=hello world"); * env.GREETING; // "hello world" * ``` */export function parse(rawDotenv: string): Record<string, string> { const env: Record<string, string> = {};
let match; const keysForExpandCheck = [];
while ((match = RE_KEY_VALUE.exec(rawDotenv)) !== null) { const { key, interpolated, notInterpolated, unquoted } = match ?.groups as LineParseResult;
if (unquoted) { keysForExpandCheck.push(key); }
env[key] = typeof notInterpolated === "string" ? notInterpolated : typeof interpolated === "string" ? expandCharacters(interpolated) : unquoted.trim(); }
//https://github.com/motdotla/dotenv-expand/blob/ed5fea5bf517a09fd743ce2c63150e88c8a5f6d1/lib/main.js#L23 const variablesMap = { ...env }; keysForExpandCheck.forEach((key) => { env[key] = expand(env[key]!, variablesMap); });
return env;}
std

Version Info

Tagged at
6 months ago