kerimdzhanov / dotenv-flow

Loads environment variables from .env.[development|test|production][.local] files for Node.js® projects.
MIT License
859 stars 36 forks source link

feat(dotenv-flow): add `options.pattern` for customizing `.env*` files' naming convention, closes #8 #71

Closed kerimdzhanov closed 1 year ago

kerimdzhanov commented 1 year ago

Summary

This PR introduces a new configuration option pattern. It allows users to define a custom naming convention for .env* files that dotenv-flow will read.

The default value for the pattern option is ".env[.node_env][.local]" which is fully backward-compatible with the current dotenv-flow's naming convention.

Description

options.pattern allows you to change the default .env* files' naming convention if you want to have a specific file naming structure for maintaining your environment variables' files.

Default Value

The default value ".env[.node_env][.local]" makes dotenv-flow look up and load the following files in order:

  1. .env
  2. .env.local
  3. .env.${NODE_ENV}
  4. .env.${NODE_ENV}.local

For example, when the proess.env.NODE_ENV (or options.node_env) is set to "development", dotenv-flow will be looking for and parsing (if found) the following files:

  1. .env
  2. .env.local
  3. .env.development
  4. .env.development.local

Custom Patterns

Here are a couple of examples of customizing the .env* files naming convention:

For example, if you set the pattern to ".env/[local/]env[.node_env]", dotenv-flow will look for these files instead:

  1. .env/env
  2. .env/local/env
  3. .env/env.development
  4. .env/local/env.development

… or if you set the pattern to ".env/[.node_env/].env[.node_env][.local]", the plugin will try to find and parse:

  1. .env/.env
  2. .env/.env.local
  3. .env/development/.env.development
  4. .env/development/.env.development.local

› Please refer to .listFiles([options]) to dive deeper.

BREAKING CHANGE: .listFiles() (exposed internal API method) doesn't receive dirname anymore and returns a list of filenames without the full path (just the name of the file).