Skip to content

Commit 10e2b51

Browse files
committed
remove logLoadedFiles and introduce an optional callback when the file is loaded
1 parent bee358b commit 10e2b51

File tree

5 files changed

+103
-11
lines changed

5 files changed

+103
-11
lines changed

.changeset/tricky-mice-dance.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"puntoenv": major
3+
---
4+
5+
remove `logLoadedFiles` and introduce an optional callback when the file is loaded with the info about the loaded file and data.

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PuntoEnv is a simple package that enables you to load `.env` files in to `proces
1010
- [Motivation](#motivation)
1111
- [Installation](#installation)
1212
- [Getting Started](#getting-started)
13+
* [Custom ENV variable](#custom-env-variable)
14+
* [Loaded file callback](#loaded-file-callback)
1315
- [How it works.](#how-it-works)
1416
* [Variable expansion](#variable-expansion)
1517
- [License](#license)
@@ -35,6 +37,7 @@ import { setupEnv } from 'puntoenv'
3537

3638
setupEnv('/path/to/your-dir/')
3739
```
40+
### Custom ENV variable
3841

3942
Also note that `NODE_ENV` will be the default environment variable that will be checked, but you can use any other variable.
4043
```ts
@@ -44,6 +47,21 @@ setupEnv('/path/to/your-dir/','NODE_CONTEXT')
4447

4548
Make sure you call the function as early as possible in your code.
4649

50+
### Loaded file callback
51+
52+
You can pass in optional `onLoad` callback (to the options object), that will fire for every loaded file. Inside the callback there is info about the `path`, `filename` and the result of the
53+
`dotEnv.config` method call.
54+
55+
```ts
56+
setupEnv(__dirname, {
57+
onLoad: (data) => {
58+
console.log('path ',data.path)
59+
console.log('filename ',data.filename)
60+
console.log('result ',data.result) // {error?: Error , parsed:Record<string,string>}
61+
},
62+
})
63+
64+
```
4765
## How it works.
4866

4967
PuntoEnv will load `.env` files in a particular order.

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

+61
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,66 @@ describe("Dev Files", () => {
3333
expect(process.env.MY_ENV).toEqual(".env.development.local")
3434
expect(process.env.FROM_EXPAND).toEqual(".env.development.local")
3535
})
36+
37+
test("onLoad callback", () => {
38+
process.env.NODE_CONTEXT = "DEVELOPMENT"
39+
const path = "/__tests__/filesystem/dev-env"
40+
41+
const result: Record<string, unknown>[] = []
42+
setupEnv(__dirname, {
43+
envVar: "NODE_CONTEXT",
44+
onLoad: (data) => {
45+
result.push(data)
46+
},
47+
})
48+
49+
expect(result[0]).toEqual({
50+
path: expect.stringContaining(path),
51+
filename: ".env.development.local",
52+
result: {
53+
parsed: {
54+
MY_ENV: ".env.development.local",
55+
TO_EXPAND: ".env.development.local",
56+
FROM_EXPAND: "$TO_EXPAND",
57+
},
58+
},
59+
})
60+
61+
expect(result[1]).toEqual({
62+
path: expect.stringContaining(path),
63+
filename: ".env.local",
64+
result: {
65+
parsed: {
66+
MY_ENV: ".env.local",
67+
TO_EXPAND: ".env.local",
68+
FROM_EXPAND: "$TO_EXPAND",
69+
},
70+
},
71+
})
72+
73+
expect(result[2]).toEqual({
74+
path: expect.stringContaining(path),
75+
filename: ".env.development",
76+
result: {
77+
parsed: {
78+
MY_ENV: ".env.development",
79+
TO_EXPAND: ".env.development",
80+
FROM_EXPAND: "$TO_EXPAND",
81+
},
82+
},
83+
})
84+
85+
expect(result[3]).toEqual({
86+
path: expect.stringContaining(path),
87+
filename: ".env",
88+
result: {
89+
parsed: {
90+
MY_ENV: ".env",
91+
TO_EXPAND: ".env",
92+
FROM_EXPAND: "$TO_EXPAND",
93+
},
94+
},
95+
})
96+
})
3697
})
3798
})

src/__tests__/setupEnv.test.ts

+1-1
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, { logLoadedFiles: true })
58+
const loaded = setupEnv(path)
5959

6060
expect(loaded).toEqual([
6161
".env.production.local",

src/index.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ export function setupEnv(
1414
{
1515
envVar = "NODE_ENV",
1616
debug = false,
17-
logLoadedFiles = false,
18-
}: { envVar?: string; debug?: boolean; logLoadedFiles?: boolean } = {},
17+
onLoad,
18+
}: {
19+
envVar?: string
20+
debug?: boolean
21+
onLoad?: (data: {
22+
path: string
23+
filename: string
24+
result: {
25+
error?: Error
26+
parsed?: Record<string, string>
27+
}
28+
}) => void
29+
} = {},
1930
): string[] {
2031
const resolvedEnv = (process.env[envVar] || "").toLowerCase()
2132

@@ -35,24 +46,21 @@ export function setupEnv(
3546
}
3647

3748
const loaded = []
38-
if (logLoadedFiles) {
39-
console.log("loading env files:")
40-
}
4149
for (const file of files) {
4250
const fullPath = path.normalize(`${rootPath}/${file}`)
4351

4452
if (fs.existsSync(fullPath)) {
45-
if (logLoadedFiles) {
46-
console.log(` - ${file}`)
47-
}
48-
4953
loaded.push(file)
5054

51-
dotEnv.config({
55+
const result = dotEnv.config({
5256
path: fullPath,
5357
override: false,
5458
debug,
5559
})
60+
61+
if (onLoad) {
62+
onLoad({ path: rootPath, filename: file, result })
63+
}
5664
}
5765
}
5866

0 commit comments

Comments
 (0)