-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.ts
More file actions
290 lines (269 loc) · 7.33 KB
/
Copy pathmod.ts
File metadata and controls
290 lines (269 loc) · 7.33 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Parse and load environment variables from `.env` files, or stringify data
* into `.env` file format.
*
* This module provides a complete implementation for working with `.env` files
* in Andromeda, supporting advanced features like variable expansion, default
* values, multi-line strings, and more.
*
* Note: Environment variable keys must match the pattern /^[a-zA-Z_][a-zA-Z0-9_]*$/
*
* @example Auto-load from .env file
* ```ts
* // Simply import to automatically load .env into environment
* import "https://tryandromeda.dev/std/dotenv/load.ts";
*
* console.log(Andromeda.env.get("DATABASE_URL"));
* ```
*
* @example Manual loading
* ```ts
* import { load, loadSync } from "https://tryandromeda.dev/std/dotenv/mod.ts";
*
* // Async load
* const config = await load();
* console.log(config.DATABASE_URL);
*
* // Sync load
* const configSync = loadSync();
* console.log(configSync.DATABASE_URL);
* ```
*
* @example Parsing strings
* ```ts
* import { parse } from "https://tryandromeda.dev/std/dotenv/mod.ts";
*
* const envString = `
* DATABASE_URL=postgresql://localhost:5432/mydb
* REDIS_URL=redis://localhost:6379
* `;
* const config = parse(envString);
* ```
*
* @example Stringifying objects
* ```ts
* import { stringify } from "https://tryandromeda.dev/std/dotenv/mod.ts";
*
* const config = {
* DATABASE_URL: "postgresql://localhost:5432/mydb",
* REDIS_URL: "redis://localhost:6379"
* };
* const envContent = stringify(config);
* // Write to file, etc.
* ```
*
* @module
*/
import { parse } from "./parse.ts";
import { stringify } from "./stringify.ts";
// Re-export parse and stringify
export { parse, stringify };
/**
* Options for configuring how environment variables are loaded.
*/
export interface LoadOptions {
/**
* Path to the `.env` file to load. Can be a string path or URL.
* Set to `null` to skip loading any file.
*
* @default {".env"}
*/
envPath?: string | URL | null;
/**
* When `true`, exports all loaded variables to the process environment
* via `Andromeda.env.set()`. Variables already present in the environment
* will not be overwritten.
*
* @default {false}
*/
export?: boolean;
/**
* Path to an example `.env` file that defines required variables.
* If specified, will validate that all variables in the example file
* are present in the loaded configuration.
*
* @default {undefined}
*/
examplePath?: string | URL;
}
/**
* Synchronously load and parse environment variables from a `.env` file.
*
* This function reads the specified `.env` file (or `.env` by default),
* parses it, and optionally exports the variables to the process environment.
*
* Features:
* - Variable expansion with $VAR or ${VAR}
* - Default values with ${VAR:-default}
* - Multi-line values in double quotes
* - Comment support (# prefix)
* - Escape sequences (\n, \r, \t)
* - Export keyword support
*
* @example Basic usage
* ```ts
* import { loadSync } from "https://tryandromeda.dev/std/dotenv/mod.ts";
*
* const config = loadSync();
* console.log(config.DATABASE_URL);
* ```
*
* @example With options
* ```ts
* import { loadSync } from "https://tryandromeda.dev/std/dotenv/mod.ts";
*
* const config = loadSync({
* envPath: ".env.production",
* export: true
* });
* ```
*
* @param options Configuration options for loading
* @returns Object containing parsed environment variables
*/
export function loadSync(options: LoadOptions = {}): Record<string, string> {
const { envPath = ".env", export: _export = false, examplePath } = options;
// Load and parse the main .env file
const conf = envPath ? parseFileSync(envPath) : {};
// Optionally validate against example file
if (examplePath) {
const example = parseFileSync(examplePath);
validateConfig(conf, example, examplePath);
}
// Export to environment if requested
if (_export) {
for (const [key, value] of Object.entries(conf)) {
// @ts-ignore: Andromeda API
if (Andromeda.env.get(key) !== undefined) continue;
// @ts-ignore: Andromeda API
Andromeda.env.set(key, value);
}
}
return conf;
}
/**
* Asynchronously load and parse environment variables from a `.env` file.
*
* This is the async version of `loadSync()`. It provides the same functionality
* but uses async file I/O operations.
*
* @example Basic usage
* ```ts
* import { load } from "https://tryandromeda.dev/std/dotenv/mod.ts";
*
* const config = await load();
* console.log(config.DATABASE_URL);
* ```
*
* @example With export
* ```ts
* import { load } from "https://tryandromeda.dev/std/dotenv/mod.ts";
*
* await load({ export: true });
* // Now accessible via Andromeda.env.get()
* console.log(Andromeda.env.get("DATABASE_URL"));
* ```
*
* @example Custom path
* ```ts
* import { load } from "https://tryandromeda.dev/std/dotenv/mod.ts";
*
* const config = await load({
* envPath: ".env.local",
* export: true
* });
* ```
*
* @param options Configuration options for loading
* @returns Promise resolving to object containing parsed environment variables
*/
export async function load(
options: LoadOptions = {},
): Promise<Record<string, string>> {
const { envPath = ".env", export: _export = false, examplePath } = options;
// Load and parse the main .env file
const conf = envPath ? await parseFile(envPath) : {};
// Optionally validate against example file
if (examplePath) {
const example = await parseFile(examplePath);
validateConfig(conf, example, examplePath);
}
// Export to environment if requested
if (_export) {
for (const [key, value] of Object.entries(conf)) {
// @ts-ignore: Andromeda API
if (Andromeda.env.get(key) !== undefined) continue;
// @ts-ignore: Andromeda API
Andromeda.env.set(key, value);
}
}
return conf;
}
/**
* Synchronously read and parse a .env file
* @internal
*/
function parseFileSync(filepath: string | URL): Record<string, string> {
try {
// @ts-ignore: Andromeda API
const content = Andromeda.readTextFileSync(filepath);
return parse(content);
} catch (e) {
// Return empty object if file not found
if (e && typeof e === "object" && "name" in e && e.name === "NotFound") {
return {};
}
throw e;
}
}
/**
* Asynchronously read and parse a .env file
* @internal
*/
async function parseFile(
filepath: string | URL,
): Promise<Record<string, string>> {
try {
// @ts-ignore: Andromeda API
const content = await Andromeda.readTextFile(filepath);
return parse(content);
} catch (e) {
// Return empty object if file not found
if (e && typeof e === "object" && "name" in e && e.name === "NotFound") {
return {};
}
throw e;
}
}
/**
* Validate that all required variables from example are present in config
* @internal
*/
function validateConfig(
config: Record<string, string>,
example: Record<string, string>,
examplePath: string | URL,
): void {
const missing: string[] = [];
for (const key of Object.keys(example)) {
if (!(key in config)) {
missing.push(key);
}
}
if (missing.length > 0) {
throw new Error(
`[dotenv] Missing required environment variables defined in ${examplePath}: ${missing.join(
", ",
)}`,
);
}
}
/**
* Default export providing all dotenv functionality
*/
export default {
parse,
stringify,
load,
loadSync,
};