Skip to content

Commit

Permalink
change function signature to accept options object and log loaded fil…
Browse files Browse the repository at this point in the history
…es paths
  • Loading branch information
ivandotv committed Jun 14, 2024
1 parent f668ef0 commit c510c92
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/healthy-fans-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"puntoenv": major
---

- change function signature to accept options object.
- add options to log loaded files paths
2 changes: 1 addition & 1 deletion src/__tests__/filesystem/dev-env/dev-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Dev Files", () => {
test("env dev test", () => {
process.env.NODE_CONTEXT = "DEVELOPMENT"

setupEnv(__dirname, "NODE_CONTEXT")
setupEnv(__dirname, { envVar: "NODE_CONTEXT" })

expect(process.env.MY_ENV).toEqual(".env.development.local")
expect(process.env.FROM_EXPAND).toEqual(".env.development.local")
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/filesystem/test-env/test-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("env: TEST", () => {
test("do not load .local files", () => {
process.env.NODE_CONTEXT = "TEST"

const loaded = setupEnv(__dirname, "NODE_CONTEXT")
const loaded = setupEnv(__dirname, { envVar: "NODE_CONTEXT" })

expect(loaded).toEqual([".env.test", ".env"])
expect(process.env.MY_ENV).toEqual(".env.test")
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/setupEnv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("Env", () => {

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

const loaded = setupEnv(path)
const loaded = setupEnv(path, { logLoadedFiles: true })

expect(loaded).toEqual([
".env.production.local",
Expand Down Expand Up @@ -109,8 +109,8 @@ describe("Env", () => {
})

test("throw an error if env variable is not present", () => {
expect(() => setupEnv(__dirname, "DOES_NOT_EXIST_IN_PROCESS_ENV")).toThrow(
"Environment variable DOES_NOT_EXIST_IN_PROCESS_ENV is not set",
)
expect(() =>
setupEnv(__dirname, { envVar: "DOES_NOT_EXIST_IN_PROCESS_ENV" }),
).toThrow("Environment variable DOES_NOT_EXIST_IN_PROCESS_ENV is not set")
})
})
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import { expand } from "dotenv-expand"
*/
export function setupEnv(
rootPath: string,
envVar = "NODE_ENV",
debug = false,
{
envVar = "NODE_ENV",
debug = false,
logLoadedFiles = false,
}: { envVar?: string; debug?: boolean; logLoadedFiles?: boolean } = {},
): string[] {
const resolvedEnv = (process.env[envVar] || "").toLowerCase()

Expand All @@ -32,11 +35,17 @@ export function setupEnv(
}

const loaded = []

if (logLoadedFiles) {
console.log("loading env files:")
}
for (const file of files) {
const fullPath = path.normalize(`${rootPath}/${file}`)

if (fs.existsSync(fullPath)) {
if (logLoadedFiles) {
console.log(` - ${file}`)
}

loaded.push(file)

dotEnv.config({
Expand Down

0 comments on commit c510c92

Please sign in to comment.