@bb-tools/withenv
v0.2.1
Published
An environment variable loader for your monorepo.
Downloads
1,658
Readme
@bb-tools/withenv
This projects aims to simplify loading of environment variables with dotenv in a monorepo.
It consists of 2 parts:
- A
withenvcommand which we use to call dotenv before running commands. - A
loadEnvfunction which you can reuse in any TS/JS code.
See the Configuration section for details about the .env.yaml file or continue reading for usage instructions.
Installation
npm install @bb-tools/withenvUsage
withenv
Usage:
withenv <env> -- [command...]Example:
withenv test -- jestloadEnv
Usage:
import { loadEnv } from "withenv";
// Loads the environment into process.env
await loadEnv("test");Configuration
Most of the magic behind withenv is a .env.yaml file whose goal is to define execution environments in a declarative way.
Example:
dev:
nodeEnv: development
files:
- .env.devIn this file, an environment named dev is declared.
As you might have guessed, when used with the dev environment, withenv will:
Set the
NODE_ENVenvironment variable value todevelopment.Load non-sensitive environment variables from the
.env.devdotenv file located alongside the.env.yamlfile.Load local overrides and secrets from the
.env.dev.localdotenv file.
Environment options
The properties described in the table below can be used for each environment defined in the .env.yaml file.
| Name | Description | Required | Default value |
| --------- | ------------------------------------------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------- |
| nodeEnv | The value of NODE_ENV to use | NO | Environment name if it exists in the NodeEnv enum of @kwentapay/node-utils package. |
| files | List of .env files to be loaded. | YES | |
| alias | An optional alias to name the environment (e.g. development instead of dev) | NO | |
Local overrides
Local files can be defined for both the dotenv and withenv:
<environment_file>.local(e.g..env.local) to define local environment variables..env.local.yamlto add new environments or override the existing ones.
i.e.: This is where your personal secrets can be placed.
These files must be ignored by Git (documentation). For example, you can add the following lines to your .gitignore file:
*.local
*.local.yamlMore information
See:
The
src/load-env.tsfile for details about the loading process.The
src/config.tsfile to see the configuration TS types.
