Skip to content

Commit c510c92

Browse files
committed
change function signature to accept options object and log loaded files paths
1 parent f668ef0 commit c510c92

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

.changeset/healthy-fans-push.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"puntoenv": major
3+
---
4+
5+
- change function signature to accept options object.
6+
- add options to log loaded files paths

src/__tests__/filesystem/dev-env/dev-env.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("Dev Files", () => {
2828
test("env dev test", () => {
2929
process.env.NODE_CONTEXT = "DEVELOPMENT"
3030

31-
setupEnv(__dirname, "NODE_CONTEXT")
31+
setupEnv(__dirname, { envVar: "NODE_CONTEXT" })
3232

3333
expect(process.env.MY_ENV).toEqual(".env.development.local")
3434
expect(process.env.FROM_EXPAND).toEqual(".env.development.local")

src/__tests__/filesystem/test-env/test-env.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("env: TEST", () => {
1919
test("do not load .local files", () => {
2020
process.env.NODE_CONTEXT = "TEST"
2121

22-
const loaded = setupEnv(__dirname, "NODE_CONTEXT")
22+
const loaded = setupEnv(__dirname, { envVar: "NODE_CONTEXT" })
2323

2424
expect(loaded).toEqual([".env.test", ".env"])
2525
expect(process.env.MY_ENV).toEqual(".env.test")

src/__tests__/setupEnv.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe("Env", () => {
5555

5656
fs.existsSync = vitest.fn().mockReturnValue(true)
5757

58-
const loaded = setupEnv(path)
58+
const loaded = setupEnv(path, { logLoadedFiles: true })
5959

6060
expect(loaded).toEqual([
6161
".env.production.local",
@@ -109,8 +109,8 @@ describe("Env", () => {
109109
})
110110

111111
test("throw an error if env variable is not present", () => {
112-
expect(() => setupEnv(__dirname, "DOES_NOT_EXIST_IN_PROCESS_ENV")).toThrow(
113-
"Environment variable DOES_NOT_EXIST_IN_PROCESS_ENV is not set",
114-
)
112+
expect(() =>
113+
setupEnv(__dirname, { envVar: "DOES_NOT_EXIST_IN_PROCESS_ENV" }),
114+
).toThrow("Environment variable DOES_NOT_EXIST_IN_PROCESS_ENV is not set")
115115
})
116116
})

src/index.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import { expand } from "dotenv-expand"
1111
*/
1212
export function setupEnv(
1313
rootPath: string,
14-
envVar = "NODE_ENV",
15-
debug = false,
14+
{
15+
envVar = "NODE_ENV",
16+
debug = false,
17+
logLoadedFiles = false,
18+
}: { envVar?: string; debug?: boolean; logLoadedFiles?: boolean } = {},
1619
): string[] {
1720
const resolvedEnv = (process.env[envVar] || "").toLowerCase()
1821

@@ -32,11 +35,17 @@ export function setupEnv(
3235
}
3336

3437
const loaded = []
35-
38+
if (logLoadedFiles) {
39+
console.log("loading env files:")
40+
}
3641
for (const file of files) {
3742
const fullPath = path.normalize(`${rootPath}/${file}`)
3843

3944
if (fs.existsSync(fullPath)) {
45+
if (logLoadedFiles) {
46+
console.log(` - ${file}`)
47+
}
48+
4049
loaded.push(file)
4150

4251
dotEnv.config({

0 commit comments

Comments
 (0)