motdotla / dotenv

Loads environment variables from .env for nodejs projects.
https://www.dotenvx.com
BSD 2-Clause "Simplified" License
19.14k stars 855 forks source link

Process multiple files in options.path, if provided. #805

Closed thanosd closed 8 months ago

thanosd commented 8 months ago

I cleaned up the code a bit to first gather a list of files (that exist) in a uniform way. The logic is as close as to what existed:

  1. If options.path has one or more options use those.
  2. If none were provided, or none of them existed, use the default .env.

The next step is to parse and combine those values - flattening them. The sequence would be:

existing env -> options.path[0] (if exists) -> options.path[1] -> etc

There were two choices here:

  1. Move from left to right, and if something exists "first one wins" unless override is set.
  2. Start from options.path[0] and move right using first one wins (no overrides), then take the combo result and apply it to existing env honoring the override flag.

I went with (2) as I did not deem the override semantics made sense when collapsing multiple environment files - but let me know if you disagree.

Also cleaned up the docs to reflect this.

motdotla commented 8 months ago

@thanosd can you switch to

Move from left to right, and if something exists "first one wins" unless override is set.

that is what dotenvx is doing.

thank you for all your hard work here @thanosd. it's really contributing to making dotenv much better for everyone.

thanosd commented 8 months ago

@motdotla Updated as requested. Little had to change in the code structure, as we need to hold a copy of the combined parsed object to return it.

motdotla commented 8 months ago

thank you. i should merged and released today.

codecov[bot] commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

:exclamation: No coverage uploaded for pull request base (master@85dbe87). Click here to learn what that means.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #805 +/- ## ========================================== Coverage ? 100.00% ========================================== Files ? 4 Lines ? 193 Branches ? 0 ========================================== Hits ? 193 Misses ? 0 Partials ? 0 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.