-
Notifications
You must be signed in to change notification settings - Fork 5
/
dotenv.esm.js
258 lines (210 loc) · 6.21 KB
/
dotenv.esm.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// node types
export const KEY_VALUE = "KEY_VALUE";
export const COMMENT = "COMMENT";
export const INVALID_LINE = "INVALID_LINE";
function splitAtFirst(string, delimiter) {
const [first, ...rest] = string.split(delimiter);
return [first, rest.length > 0 ? rest.join(delimiter) : null];
}
function escape(string) {
return string.replace(/\n/g, "\\n");
}
function unescape(string) {
return string.replace(/\\n/g, "\n");
}
// parse a single line into a node
function parseLine(line) {
// handle comments...
const [firstNonWhitespaceChar] = line.match(/\S/) || [];
if (firstNonWhitespaceChar === "#") {
const [_fullMatch, prefix, comment] = line.match(/(^\s*#\s*)(.*)/);
return { type: COMMENT, prefix, comment };
}
// ...now try to parse as a KV pair
const [fullKey, fullValue] = splitAtFirst(line, "=");
// if there is no `=` in the line, it's an invalid line
if (fullValue === null) {
return { type: INVALID_LINE, text: line };
}
const key = fullKey.trim();
// if the key contains whitespace or is empty, it's an invalid line
if (key.match(/\s/) || key === "") {
return { type: INVALID_LINE, text: line };
}
let escapedValue = fullValue.trim();
// find and remove quotes if the value is quoted
let quote = "";
const [startQuote] = escapedValue.match(/^("|')/) || [];
if (startQuote && escapedValue.endsWith(startQuote)) {
escapedValue = escapedValue.slice(1, -1);
quote = startQuote;
}
// if double quoted, unescape newlines
let value = escapedValue;
if (quote === '"') {
value = unescape(value);
}
return {
type: KEY_VALUE,
key,
fullKey,
value,
escapedValue,
fullValue,
quote,
originalQuote: quote,
};
}
// split at newlines, parse each line, push results into node list
export function textToNodes(text) {
if (text === "") {
return [];
}
const lines = text.split("\n");
const result = [];
for (const line of lines) {
result.push(parseLine(line));
}
return result;
}
// maintain reference after name change
export const textToAst = textToNodes;
// build a string from a node list
export function nodesToText(nodes) {
let result = "";
for (const node of nodes) {
if (node.type === KEY_VALUE && node.key === "") {
continue;
} else if (node.type === KEY_VALUE) {
result += `${node.fullKey}=${node.fullValue}`;
} else if (node.type === COMMENT) {
result += `${node.prefix}${node.comment}`;
} else if (node.type === INVALID_LINE) {
result += node.text;
} else {
// if someone manually manipulated the node list, we could have an invalid node
throw new Error(`Invalid node type: ${node.type}`);
}
result += "\n";
}
// remove extra newline at the end
return result.slice(0, -1);
}
// maintain reference after name change
export const astToText = nodesToText;
// find node, ensure it's a kv pair, and immutably update its `key` and `fullKey` properties
export function changeKey(nodes, index, newKey) {
const node = nodes[index];
if (node.type !== KEY_VALUE) {
return nodes;
}
const key = newKey.trim();
const newNode = {
...node,
key,
fullKey: node.fullKey.replace(node.key, key),
};
const newNodes = nodes.slice(); // clone array since .splice mutates
newNodes.splice(index, 1, newNode); // replace element at index with newNode
return newNodes;
}
// find node, ensure it's a kv pair, and immutably update its `value`, `fullValue`, and `quote` properties
export function changeValue(nodes, index, newValue) {
const node = nodes[index];
if (node.type !== KEY_VALUE) {
return nodes;
}
// if the new value needs to be escaped (contains a newline or has leading or trailing whitespace), change quote
// to double quotes, otherwise keep whatever the original node had. if a value later doesn't need to be quoted,
// it will swap back to the quotes used in the original text
const quote =
newValue.match(/\n/) || newValue.match(/^\s/) || newValue.match(/\s$/)
? '"'
: node.originalQuote;
const escapedNewValue = escape(newValue);
const fullValue = node.fullValue.replace(
`${node.quote}${node.escapedValue}${node.quote}`,
`${quote}${escapedNewValue}${quote}`,
);
const newNode = {
...node,
value: newValue,
escapedValue: escapedNewValue,
fullValue,
quote,
};
const newNodes = nodes.slice(); // clone array since .splice mutates
newNodes.splice(index, 1, newNode); // replace element at index with newNode
return newNodes;
}
export function setQuote(nodes, index, quote) {
const node = nodes[index];
if (node.type !== KEY_VALUE) {
return nodes;
}
if (quote === node.quote) {
return nodes;
}
if (node.value.match(/\n/) && quote === "") {
throw new Error("values with newlines require quotes");
}
const fullValue = node.fullValue.replace(
`${node.quote}${node.escapedValue}${node.quote}`,
`${quote}${node.escapedValue}${quote}`,
);
const newNode = {
...node,
fullValue,
quote,
};
const newNodes = nodes.slice();
newNodes.splice(index, 1, newNode);
return newNodes;
}
// append a key value pair to the end of the node list
export function appendKeyValue(nodes, key, value) {
if (key.match(/\s/)) {
throw new Error("Keys cannot contain whitespace");
}
const escapedValue = escape(value);
const quote = value !== escapedValue ? '"' : "";
const newNode = {
type: KEY_VALUE,
key,
fullKey: key,
value,
escapedValue,
fullValue: escapedValue,
// if we escaped newlines, set quote
quote,
originalQuote: quote,
};
return [...nodes, newNode];
}
// remove a key value pair from the node list at index
export function removeKeyValue(nodes, index) {
const node = nodes[index];
if (node.type !== KEY_VALUE) {
return nodes;
}
const newNodes = nodes.slice(); // clone array since .splice mutates
newNodes.splice(index, 1); // replace element at index with newNode
return newNodes;
}
// extract key value pairs from a node list into a Javascript object
export function parseNodes(nodes) {
const result = {};
for (const node of nodes) {
if (node.type === KEY_VALUE) {
result[node.key] = node.value;
}
}
return result;
}
// maintain reference after name change
export const parseAst = parseNodes;
// leverage textToNodes and parseNodes to extract key value pairs from a dotenv string into a Javascript object
export function parseText(text) {
const nodes = textToNodes(text);
return parseNodes(nodes);
}