Skip to content

Commit 73a0065

Browse files
authored
feat(dotenv): add URL as envPath type (#6621)
1 parent 096f0be commit 73a0065

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

dotenv/mod.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export interface LoadOptions {
3535
*
3636
* @default {"./.env"}
3737
*/
38-
envPath?: string | null;
38+
envPath?: string | URL | null;
3939

4040
/**
4141
* Set to `true` to export all `.env` variables to the current processes
@@ -227,7 +227,7 @@ export async function load(
227227
}
228228

229229
function parseFileSync(
230-
filepath: string,
230+
filepath: string | URL,
231231
): Record<string, string> {
232232
try {
233233
return parse(Deno.readTextFileSync(filepath));
@@ -238,7 +238,7 @@ function parseFileSync(
238238
}
239239

240240
async function parseFile(
241-
filepath: string,
241+
filepath: string | URL,
242242
): Promise<Record<string, string>> {
243243
try {
244244
return parse(await Deno.readTextFile(filepath));

dotenv/mod_test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ Deno.test("load() handles non-existent .env files", async () => {
2929
assertEquals({}, loadSync(loadOptions));
3030
});
3131

32+
Deno.test("load() handles URL as path for .env files", async () => {
33+
const conf = loadSync({
34+
envPath: new URL(
35+
path.toFileUrl(path.join(testdataDir, ".env")),
36+
import.meta.url,
37+
),
38+
});
39+
assertEquals(conf.GREETING, "hello world", "loaded from .env");
40+
41+
const asyncConf = await load({
42+
envPath: new URL(
43+
path.toFileUrl(path.join(testdataDir, ".env")),
44+
import.meta.url,
45+
),
46+
});
47+
assertEquals(asyncConf.GREETING, "hello world", "loaded from .env");
48+
});
49+
3250
Deno.test("load() handles comprised .env and .env.defaults", async () => {
3351
const conf = loadSync(testOptions);
3452
assertEquals(conf.GREETING, "hello world", "loaded from .env");

0 commit comments

Comments
 (0)