@karmaniverous/get-dotenv 6.0.0-0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -334
- package/dist/cli.d.ts +569 -0
- package/dist/cli.mjs +18788 -0
- package/dist/cliHost.d.ts +548 -253
- package/dist/cliHost.mjs +1990 -1458
- package/dist/config.d.ts +192 -14
- package/dist/config.mjs +256 -81
- package/dist/env-overlay.d.ts +226 -18
- package/dist/env-overlay.mjs +181 -22
- package/dist/getdotenv.cli.mjs +18166 -3437
- package/dist/index.d.ts +729 -136
- package/dist/index.mjs +18207 -3457
- package/dist/plugins-aws.d.ts +289 -104
- package/dist/plugins-aws.mjs +2462 -350
- package/dist/plugins-batch.d.ts +355 -105
- package/dist/plugins-batch.mjs +2595 -420
- package/dist/plugins-cmd.d.ts +287 -118
- package/dist/plugins-cmd.mjs +2661 -839
- package/dist/plugins-init.d.ts +272 -100
- package/dist/plugins-init.mjs +2152 -37
- package/dist/plugins.d.ts +323 -140
- package/dist/plugins.mjs +18006 -2025
- package/dist/templates/cli/index.ts +26 -0
- package/dist/templates/cli/plugins/hello.ts +43 -0
- package/dist/templates/config/js/getdotenv.config.js +20 -0
- package/dist/templates/config/json/local/getdotenv.config.local.json +7 -0
- package/dist/templates/config/json/public/getdotenv.config.json +9 -0
- package/dist/templates/config/public/getdotenv.config.json +8 -0
- package/dist/templates/config/ts/getdotenv.config.ts +28 -0
- package/dist/templates/config/yaml/local/getdotenv.config.local.yaml +7 -0
- package/dist/templates/config/yaml/public/getdotenv.config.yaml +7 -0
- package/dist/templates/getdotenv.config.js +20 -0
- package/dist/templates/getdotenv.config.json +9 -0
- package/dist/templates/getdotenv.config.local.json +7 -0
- package/dist/templates/getdotenv.config.local.yaml +7 -0
- package/dist/templates/getdotenv.config.ts +28 -0
- package/dist/templates/getdotenv.config.yaml +7 -0
- package/dist/templates/hello.ts +43 -0
- package/dist/templates/index.ts +26 -0
- package/dist/templates/js/getdotenv.config.js +20 -0
- package/dist/templates/json/local/getdotenv.config.local.json +7 -0
- package/dist/templates/json/public/getdotenv.config.json +9 -0
- package/dist/templates/local/getdotenv.config.local.json +7 -0
- package/dist/templates/local/getdotenv.config.local.yaml +7 -0
- package/dist/templates/plugins/hello.ts +43 -0
- package/dist/templates/public/getdotenv.config.json +9 -0
- package/dist/templates/public/getdotenv.config.yaml +7 -0
- package/dist/templates/ts/getdotenv.config.ts +28 -0
- package/dist/templates/yaml/local/getdotenv.config.local.yaml +7 -0
- package/dist/templates/yaml/public/getdotenv.config.yaml +7 -0
- package/getdotenv.config.json +1 -19
- package/package.json +52 -89
- package/templates/cli/index.ts +26 -0
- package/templates/cli/plugins/hello.ts +43 -0
- package/templates/config/js/getdotenv.config.js +9 -4
- package/templates/config/json/public/getdotenv.config.json +0 -3
- package/templates/config/public/getdotenv.config.json +0 -5
- package/templates/config/ts/getdotenv.config.ts +17 -5
- package/templates/config/yaml/public/getdotenv.config.yaml +0 -3
- package/dist/cliHost.cjs +0 -2078
- package/dist/cliHost.d.cts +0 -451
- package/dist/cliHost.d.mts +0 -451
- package/dist/config.cjs +0 -252
- package/dist/config.d.cts +0 -55
- package/dist/config.d.mts +0 -55
- package/dist/env-overlay.cjs +0 -163
- package/dist/env-overlay.d.cts +0 -50
- package/dist/env-overlay.d.mts +0 -50
- package/dist/index.cjs +0 -4077
- package/dist/index.d.cts +0 -318
- package/dist/index.d.mts +0 -318
- package/dist/plugins-aws.cjs +0 -666
- package/dist/plugins-aws.d.cts +0 -158
- package/dist/plugins-aws.d.mts +0 -158
- package/dist/plugins-batch.cjs +0 -658
- package/dist/plugins-batch.d.cts +0 -181
- package/dist/plugins-batch.d.mts +0 -181
- package/dist/plugins-cmd.cjs +0 -1112
- package/dist/plugins-cmd.d.cts +0 -178
- package/dist/plugins-cmd.d.mts +0 -178
- package/dist/plugins-demo.cjs +0 -352
- package/dist/plugins-demo.d.cts +0 -158
- package/dist/plugins-demo.d.mts +0 -158
- package/dist/plugins-demo.d.ts +0 -158
- package/dist/plugins-demo.mjs +0 -350
- package/dist/plugins-init.cjs +0 -289
- package/dist/plugins-init.d.cts +0 -162
- package/dist/plugins-init.d.mts +0 -162
- package/dist/plugins.cjs +0 -2327
- package/dist/plugins.d.cts +0 -211
- package/dist/plugins.d.mts +0 -211
- package/templates/cli/ts/index.ts +0 -9
- package/templates/cli/ts/plugins/hello.ts +0 -17
package/README.md
CHANGED
|
@@ -1,87 +1,45 @@
|
|
|
1
|
-
> Load, expand, and compose environment variables from a deterministic dotenv cascade, then execute commands under that context. Use
|
|
1
|
+
> Load, expand, and compose environment variables from a deterministic dotenv cascade, then execute commands under that context. Use get‑dotenv as a library, a CLI, or a plugin‑first host to build dotenv‑aware tooling with cross‑platform shell control, CI‑friendly capture, and clear diagnostics.
|
|
2
2
|
|
|
3
3
|
# get-dotenv
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@karmaniverous/get-dotenv)
|
|
6
|
-

|
|
6
|
+

|
|
7
7
|
[](https://docs.karmanivero.us/get-dotenv)
|
|
8
|
-
[](
|
|
9
|
-
[](
|
|
8
|
+
[](./CHANGELOG.md)
|
|
9
|
+
[](./LICENSE)
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
get‑dotenv helps you:
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
- Load and merge env vars from a deterministic cascade (global/env × public/private) across multiple paths.
|
|
14
|
+
- Expand values recursively with defaults.
|
|
15
|
+
- Add dynamic variables (JS/TS) that compute from the composed env.
|
|
16
|
+
- Run commands with normalized shell behavior and a consistent child environment.
|
|
17
|
+
- Compose your own CLI from shipped plugins or your own.
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
See full guides:
|
|
16
20
|
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
- [
|
|
22
|
-
|
|
23
|
-
✅ Dynamically specify which variables to load by type.
|
|
24
|
-
|
|
25
|
-
✅ Explicitly add variables to the loaded set.
|
|
26
|
-
|
|
27
|
-
✅ Extract the resulting variables to an object, `process.env`, a dotenv file, or a logger object, in any combination.
|
|
28
|
-
|
|
29
|
-
✅ Customize your dotenv file directories & naming patterns.
|
|
30
|
-
|
|
31
|
-
✅ Perform all of the above either programmatically or [from the command line](#command-line-interface), where you can also execute additional commands within the resulting context... including nested `getdotenv` commands that inherit the parent command's settings & context!
|
|
32
|
-
|
|
33
|
-
✅ [Execute batched CLI commands](#batch-command) across multiple working directories, with each command inheriting the `getdotenv` context.
|
|
34
|
-
|
|
35
|
-
✅ Set defaults for all options in a `getdotenv.config.json` file in your project root directory.
|
|
36
|
-
|
|
37
|
-
✅ Validate your final composed environment via config: JSON/YAML `requiredKeys` or a JS/TS Zod `schema`. Validation runs once after Phase C (interpolation). Use `--strict` to fail on issues; otherwise warnings are printed. See the [Config files and overlays](./guides/config.md) guide.
|
|
38
|
-
|
|
39
|
-
✅ Diagnostics for safer visibility without altering runtime values:
|
|
40
|
-
|
|
41
|
-
- Redaction at presentation time for secret-like keys (`--redact`, `--redact-pattern`).
|
|
42
|
-
- Entropy warnings (on by default) for high-entropy strings; gated by length/printability and tunable via `--entropy-*` flags. See [Config files and overlays](./guides/config.md).
|
|
43
|
-
|
|
44
|
-
✅ Clear tracing and CI-friendly capture:
|
|
45
|
-
|
|
46
|
-
- `--trace [keys...]` shows per-key origin (parent | dotenv | unset) before spawning.
|
|
47
|
-
- Set `GETDOTENV_STDIO=pipe` or use `--capture` to buffer child stdout/stderr deterministically. See [Shell execution behavior](./guides/shell.md).
|
|
48
|
-
|
|
49
|
-
✅ Cross-platform shells and normalized child environments: defaults to `/bin/bash` on POSIX and `powershell.exe` on Windows; subprocess env is composed once via a unified helper that drops undefineds and normalizes temp/home variables. See [Shell execution behavior](./guides/shell.md).
|
|
50
|
-
|
|
51
|
-
✅ Embed the plugin‑first get‑dotenv host and wire shipped or custom plugins to build your own CLI. See [Authoring Plugins](./guides/authoring/index.md).
|
|
52
|
-
|
|
53
|
-
`getdotenv` relies on the excellent [`dotenv`](https://www.npmjs.com/package/dotenv) parser and somewhat improves on [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) for recursive variable expansion.
|
|
54
|
-
|
|
55
|
-
You can always use `getdotenv` directly on the command line, but its REAL power comes into play when you use it as the foundation of your own CLI. This lets you set defaults globally and configure pre- and post-hooks that mutate your `getdotenv` context and do useful things like grab an AWS session from your dev environment and add it to the command execution context.
|
|
56
|
-
|
|
57
|
-
When you plug your own [`commander`](https://www.npmjs.com/package/commander) CLI commands into the `getdotenv` base, they will execute within all of the environmental context created above!
|
|
21
|
+
- [Getting started](./guides/getting-started.md)
|
|
22
|
+
- [Config and overlays](./guides/config.md) (dynamic, validation, defaults):
|
|
23
|
+
- [Shell execution behavior, quoting, capture](./guides/shell.md)
|
|
24
|
+
- [Shipped plugins](./guides/shipped/index.md)
|
|
25
|
+
- [Authoring plugins](./guides/authoring/index.md) (host, lifecycle, exec, diagnostics)
|
|
58
26
|
|
|
27
|
+
## Use Cases
|
|
28
|
+
|
|
29
|
+
- Execute deployment steps across many repositories from one command sequence. See [batch plugin guide](./guides/shipped/batch.md)
|
|
30
|
+
- Run config‑driven, environment‑aware operations with simple CLI commands. See [config guide](./guides/config.md)
|
|
31
|
+
- Compose a rich CLI from shipped and third‑party plugins and share it across projects. See [shipped plugins overview](./guides/shipped/index.md)
|
|
32
|
+
- Drive complex AWS workflows in an authenticated, environment‑aware context. See [aws plugin guide](./guides/shipped/aws.md)
|
|
33
|
+
- Scaffold a project config and a host‑based CLI skeleton in seconds. See [init plugin guide](./guides/shipped/init.md)
|
|
34
|
+
- Batch lint/build/test across a monorepo with deterministic output. See [batch plugin guide](./guides/shipped/batch.md)
|
|
35
|
+
- Run cross‑platform commands in CI with normalized shells and capture. See [shell guide](./guides/shell.md)
|
|
36
|
+
- Programmatically compose env and run tools inside Node scripts. See [Getting started](./guides/getting-started.md)
|
|
37
|
+
- Add observability without leaking secrets using trace, redaction, and entropy warnings. See [diagnostics guide](./guides/authoring/diagnostics.md)
|
|
38
|
+
- Author new plugins with maximum DX and minimal boilerplate. See [authoring plugins guide](./guides/authoring/index.md)
|
|
39
|
+
|
|
59
40
|
## Requirements
|
|
60
41
|
|
|
61
|
-
- Node.js
|
|
62
|
-
|
|
63
|
-
## Getting Started
|
|
64
|
-
|
|
65
|
-
- One‑off CLI with your env: `getdotenv -c 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'` — see [Shell execution behavior](./guides/shell.md) and [cmd plugin](./guides/shipped/cmd.md).
|
|
66
|
-
- Programmatic load: `const vars = await getDotenv({ env: 'dev', paths: ['./'] });` — see [Config files and overlays](./guides/config.md).
|
|
67
|
-
- Embed a CLI quickly: use [createCli](#cli-embedding-createcli), or wire a [custom host](#custom-host-wire-plugins) to choose plugins.
|
|
68
|
-
- Scaffold config and a CLI skeleton: `npx getdotenv init . --config-format json --with-local --cli-name acme` — see [init plugin](./guides/shipped/init.md).
|
|
69
|
-
|
|
70
|
-
## API Reference
|
|
71
|
-
|
|
72
|
-
Generated API documentation is hosted at:
|
|
73
|
-
|
|
74
|
-
- https://docs.karmanivero.us/get-dotenv
|
|
75
|
-
|
|
76
|
-
The site is built with TypeDoc from the source code in this repository.
|
|
77
|
-
|
|
78
|
-
## Testing
|
|
79
|
-
|
|
80
|
-
This project uses Vitest with the V8 coverage provider. Run:
|
|
81
|
-
|
|
82
|
-
```bash
|
|
83
|
-
npm run test
|
|
84
|
-
```
|
|
42
|
+
- Node.js ≥ 20 (this repository pins 22.19.0 for CI/reproducibility)
|
|
85
43
|
|
|
86
44
|
## Installation
|
|
87
45
|
|
|
@@ -89,313 +47,107 @@ npm run test
|
|
|
89
47
|
npm install @karmaniverous/get-dotenv
|
|
90
48
|
```
|
|
91
49
|
|
|
92
|
-
##
|
|
93
|
-
|
|
94
|
-
You can scaffold config files and a host-based CLI skeleton using the built-in init command. Templates are shipped with the package and copied verbatim.
|
|
50
|
+
## Quick Start
|
|
95
51
|
|
|
96
|
-
|
|
52
|
+
Run a one‑off command with your env (parent alias so flags apply to getdotenv):
|
|
97
53
|
|
|
98
54
|
```bash
|
|
99
|
-
|
|
100
|
-
npx getdotenv init . \
|
|
101
|
-
--config-format json \
|
|
102
|
-
--with-local \
|
|
103
|
-
--cli-name acme \
|
|
104
|
-
--force
|
|
55
|
+
npx @karmaniverous/get-dotenv -c 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
|
|
105
56
|
```
|
|
106
57
|
|
|
107
|
-
|
|
108
|
-
# TypeScript config with a dynamic example; CLI named "toolbox"
|
|
109
|
-
npx getdotenv init ./apps/toolbox \
|
|
110
|
-
--config-format ts \
|
|
111
|
-
--cli-name toolbox
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
Collision flow (when a destination file exists):
|
|
115
|
-
|
|
116
|
-
- Interactive prompt: [o]verwrite, [e]xample, [s]kip, or their “all” variants [O]/[E]/[S].
|
|
117
|
-
- Non-interactive detection: Treated as `--yes` (Skip All) unless `--force` is provided (Overwrite All). Considered non-interactive when stdin or stdout is not a TTY OR when a CI-like environment variable is present (`CI`, `GITHUB_ACTIONS`, `BUILDKITE`, `TEAMCITY_VERSION`, `TF_BUILD`).
|
|
118
|
-
- Precedence: `--force` > `--yes` > auto-detect (non-interactive => Skip All).
|
|
119
|
-
- Options overview:
|
|
120
|
-
- `--config-format <json|yaml|js|ts>`
|
|
121
|
-
- `--with-local` to generate `.local` alongside public config (JSON/YAML)
|
|
122
|
-
- `--cli-name <string>` for token substitution (`__CLI_NAME__`) in the CLI skeleton
|
|
123
|
-
- `--force` to overwrite all; `--yes` to skip all
|
|
124
|
-
|
|
125
|
-
Notes:
|
|
58
|
+
Load programmatically:
|
|
126
59
|
|
|
127
|
-
|
|
128
|
-
- The CLI skeleton replaces `__CLI_NAME__` tokens with your chosen name.
|
|
129
|
-
|
|
130
|
-
## Usage
|
|
131
|
-
|
|
132
|
-
```js
|
|
60
|
+
```ts
|
|
133
61
|
import { getDotenv } from '@karmaniverous/get-dotenv';
|
|
134
62
|
|
|
135
|
-
const
|
|
63
|
+
const vars = await getDotenv({ env: 'dev', paths: ['./'] });
|
|
64
|
+
console.log(vars.APP_SETTING);
|
|
136
65
|
```
|
|
137
66
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
See [Config files and overlays](./guides/config.md) for how to author defaults, overlays, validation, and diagnostics in JSON/YAML/JS/TS.
|
|
141
|
-
|
|
142
|
-
## CLI embedding (createCli)
|
|
143
|
-
|
|
144
|
-
Prefer the named factory when you want to embed the get‑dotenv CLI in your own tool. It wires the plugin‑first host with the included plugins and returns a small runner.
|
|
67
|
+
Embed a CLI quickly (shipped plugins wired for you):
|
|
145
68
|
|
|
146
69
|
```ts
|
|
147
70
|
#!/usr/bin/env node
|
|
148
|
-
import { createCli } from '@karmaniverous/get-dotenv';
|
|
149
|
-
|
|
150
|
-
// Build a CLI and run with your argv; alias appears in help.
|
|
151
|
-
await createCli({
|
|
152
|
-
alias: 'mycli',
|
|
153
|
-
// Optional: override the help header (otherwise “mycli v<version>” is used).
|
|
154
|
-
branding: 'mycli v1.2.3',
|
|
155
|
-
}).run(process.argv.slice(2));
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
Notes:
|
|
159
|
-
|
|
160
|
-
- The host resolves dotenv context once per invocation and exposes subcommands: cmd, batch, aws, and init (see Guides below).
|
|
161
|
-
- Use `--trace [keys...]` and `--redact` for diagnostics without altering runtime values.
|
|
162
|
-
- Default shells are normalized across platforms: `/bin/bash` on POSIX and `powershell.exe` on Windows (overridable per‑script or globally).
|
|
163
|
-
- Help/exit behavior (important for embedding and tests):
|
|
164
|
-
- To keep `-h/--help` deterministic across ESM/CJS and avoid Commander’s default `process.exit`, `createCli().run(['-h'])` prints help and returns without exiting the process.
|
|
165
|
-
- Because help is printed before the internal `brand()` call runs, the optional branding header may be omitted when `-h/--help` is used at the top level. If you need a branded header, prefer `mycli help` (which runs through normal parsing) or construct and brand a host directly (see “Branding the host CLI” in Guides).
|
|
166
|
-
- Interop matrix (embedding in different module systems):
|
|
167
|
-
- ESM dynamic:
|
|
168
|
-
```ts
|
|
169
|
-
const { createCli } = await import('@karmaniverous/get-dotenv');
|
|
170
|
-
await createCli({ alias: 'mycli' }).run(['-h']);
|
|
171
|
-
```
|
|
172
|
-
- CommonJS require (using built outputs for smoke checks):
|
|
173
|
-
```js
|
|
174
|
-
const { createCli } = require('@karmaniverous/get-dotenv/dist/index.cjs');
|
|
175
|
-
createCli({ alias: 'mycli' }).run(['-h']);
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
### Custom host (wire plugins)
|
|
179
|
-
|
|
180
|
-
When you want full control over the command surface, construct a host directly and choose which plugins to install. This example omits the demo plugin by default and shows where to add your own:
|
|
71
|
+
import { createCli } from '@karmaniverous/get-dotenv/cli';
|
|
181
72
|
|
|
182
|
-
|
|
183
|
-
#!/usr/bin/env node
|
|
184
|
-
import type { Command } from 'commander';
|
|
185
|
-
import { GetDotenvCli } from '@karmaniverous/get-dotenv/cliHost';
|
|
186
|
-
import {
|
|
187
|
-
cmdPlugin,
|
|
188
|
-
batchPlugin,
|
|
189
|
-
awsPlugin,
|
|
190
|
-
initPlugin,
|
|
191
|
-
} from '@karmaniverous/get-dotenv/plugins';
|
|
192
|
-
// import { demoPlugin } from '@karmaniverous/get-dotenv/plugins/demo'; // optional
|
|
193
|
-
// import { helloPlugin } from './plugins/hello'; // your plugin
|
|
194
|
-
|
|
195
|
-
const program: Command = new GetDotenvCli('mycli');
|
|
196
|
-
await (program as unknown as GetDotenvCli).brand({
|
|
197
|
-
importMetaUrl: import.meta.url,
|
|
198
|
-
description: 'mycli',
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
program
|
|
202
|
-
.attachRootOptions({ loadProcess: false })
|
|
203
|
-
.use(cmdPlugin({ asDefault: true, optionAlias: '-c, --cmd <command...>' }))
|
|
204
|
-
.use(batchPlugin())
|
|
205
|
-
.use(awsPlugin())
|
|
206
|
-
.use(initPlugin())
|
|
207
|
-
// .use(demoPlugin()) // omit demo by default
|
|
208
|
-
// .use(helloPlugin()) // add your own plugin(s)
|
|
209
|
-
.passOptions({ loadProcess: false });
|
|
210
|
-
|
|
211
|
-
await program.parseAsync();
|
|
73
|
+
await createCli({ alias: 'toolbox' }).run(process.argv.slice(2));
|
|
212
74
|
```
|
|
213
75
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
- Root flags come from `attachRootOptions()`. `passOptions()` merges flags (parent < current), resolves dotenv context once, validates, and persists the merged options bag for nested flows.
|
|
217
|
-
- See [Authoring Plugins → Lifecycle & Wiring](./guides/authoring/lifecycle.md) for a deeper walk‑through and best practices.
|
|
76
|
+
More first steps and tips at [Getting Started](./guides/getting-started.md)
|
|
218
77
|
|
|
219
|
-
##
|
|
78
|
+
## Configuration & overlays
|
|
220
79
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
Use the `dynamicPath` option to add a relative path to a Javascript module with a default export like this:
|
|
224
|
-
|
|
225
|
-
```js
|
|
226
|
-
export default {
|
|
227
|
-
SOME_DYNAMIC_VARIABLE: (dotenv) => someLogic(dotenv),
|
|
228
|
-
ANOTHER_DYNAMIC_VARIABLE: (dotenv) =>
|
|
229
|
-
someOtherLogic(dotenv.SOME_DYNAMIC_VARIABLE),
|
|
230
|
-
ONE_MORE_TIME: ({ DESTRUCTURED_VARIABLE, ANOTHER_DYNAMIC_VARIABLE }) =>
|
|
231
|
-
DESTRUCTURED_VARIABLE + ANOTHER_DYNAMIC_VARIABLE,
|
|
232
|
-
};
|
|
233
|
-
```
|
|
80
|
+
Author config in JSON/YAML/JS/TS at your project root. The loader is always on in the shipped host:
|
|
234
81
|
|
|
235
|
-
|
|
82
|
+
- Data: `vars` (global), `envVars` (per‑env)
|
|
83
|
+
- Root defaults and visibility for CLI flags: `rootOptionDefaults`, `rootOptionVisibility`
|
|
84
|
+
- Optional scripts table: `scripts`
|
|
85
|
+
- Dynamic (JS/TS only): `dynamic`
|
|
86
|
+
- Validation (JS/TS schema or required keys): `schema`, `requiredKeys`
|
|
236
87
|
|
|
237
|
-
|
|
88
|
+
Overlays apply by kind/env/privacy/source with clear precedence. Details and examples in the [Config guide](./guides/config.md).
|
|
238
89
|
|
|
239
|
-
|
|
90
|
+
## Dynamic variables (JS/TS)
|
|
240
91
|
|
|
241
|
-
|
|
92
|
+
Add dynamic keys that compute from the composed env. Programmatic or file‑based (JS/TS). For TypeScript, install `esbuild` for auto‑compile.
|
|
242
93
|
|
|
243
94
|
```ts
|
|
244
95
|
// dynamic.ts
|
|
245
96
|
export default {
|
|
246
|
-
|
|
97
|
+
GREETING: ({ APP_SETTING = '' }) => `Hello ${APP_SETTING}`,
|
|
247
98
|
};
|
|
248
99
|
```
|
|
249
100
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
Programmatic users can skip files entirely and pass dynamic variables directly:
|
|
253
|
-
|
|
254
|
-
```ts
|
|
255
|
-
import { getDotenv, defineDynamic } from '@karmaniverous/get-dotenv';
|
|
256
|
-
|
|
257
|
-
const dynamic = defineDynamic({
|
|
258
|
-
MY_DYNAMIC(vars, env) {
|
|
259
|
-
return `${vars.APP_SETTING}-${env ?? ''}`;
|
|
260
|
-
},
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
const vars = await getDotenv({ dynamic, paths: ['./'], env: 'dev' });
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
Notes:
|
|
267
|
-
|
|
268
|
-
- Programmatic `dynamic` takes precedence over `dynamicPath` when both are provided.
|
|
269
|
-
- Dynamic keys are evaluated progressively, so later keys can reference earlier results.
|
|
270
|
-
|
|
271
|
-
#### Troubleshooting
|
|
272
|
-
|
|
273
|
-
- “Unknown file extension '.ts'” when loading `dynamic.ts`:
|
|
274
|
-
- Install `esbuild` (`npm i -D esbuild`).
|
|
275
|
-
|
|
276
|
-
- “Unable to load dynamic TypeScript file …”:
|
|
277
|
-
- Install `esbuild`. A simple transpile fallback exists only for trivial single-file modules; any imports in `dynamic.ts` require `esbuild` bundling.
|
|
278
|
-
|
|
279
|
-
## Command Line Interface
|
|
280
|
-
|
|
281
|
-
You can also use `getdotenv` from the command line. For a concise overview run `getdotenv -h`, and see the shipped plugin pages for details: [cmd](./guides/shipped/cmd.md), [batch](./guides/shipped/batch.md), [aws](./guides/shipped/aws.md), and [init](./guides/shipped/init.md). For quoting and default shell behavior, see [Shell execution behavior](./guides/shell.md).
|
|
282
|
-
|
|
283
|
-
### Default shell behavior
|
|
284
|
-
|
|
285
|
-
To normalize behavior across platforms, the CLI resolves a default shell when `--shell` is true or omitted:
|
|
286
|
-
|
|
287
|
-
- POSIX: `/bin/bash`
|
|
288
|
-
- Windows: `powershell.exe`
|
|
289
|
-
|
|
290
|
-
### Batch Command
|
|
291
|
-
|
|
292
|
-
The `getdotenv` base CLI includes one very useful subcommand: `batch`.
|
|
293
|
-
|
|
294
|
-
This command lets you execute a shell command across multiple working directories. Executions occur within the loaded `dotenv` context. Might not be relevant to your specific use case, but when you need it, it's a game-changer!
|
|
295
|
-
|
|
296
|
-
My most common use case for this command is a microservice project where release day finds me updating dependencies & performing a release in well over a dozen very similar repositories. The sequence of steps in each case is exactly the same, but I need to respond individually as issues arise, so scripting the whole thing out would fail more often than it would work.
|
|
297
|
-
|
|
298
|
-
I use the `batch` command to perform each step across all repositories at once. Once you get used to it, it feels like a superpower!
|
|
299
|
-
|
|
300
|
-
Lest you doubt what that kind of leverage can do for you, consider this:
|
|
301
|
-
|
|
302
|
-
[](https://github.com/karmaniverous)
|
|
303
|
-
|
|
304
|
-
```bash
|
|
305
|
-
> getdotenv batch -h
|
|
306
|
-
|
|
307
|
-
# Usage: getdotenv batch [options] [command]
|
|
308
|
-
#
|
|
309
|
-
# Batch command execution across multiple working directories.
|
|
310
|
-
#
|
|
311
|
-
# Options:
|
|
312
|
-
# -p, --pkg-cwd use nearest package directory as current working directory
|
|
313
|
-
# -r, --root-path <string> path to batch root directory from current working directory (default: "./")
|
|
314
|
-
# -g, --globs <string> space-delimited globs from root path (default: "*")
|
|
315
|
-
# -c, --command <string> command executed according to the base --shell option, conflicts with cmd subcommand (dotenv-expanded)
|
|
316
|
-
# -l, --list list working directories without executing command
|
|
317
|
-
# -e, --ignore-errors ignore errors and continue with next path
|
|
318
|
-
# -h, --help display help for command
|
|
319
|
-
#
|
|
320
|
-
# Commands:
|
|
321
|
-
# cmd execute command, conflicts with --command option (default subcommand)
|
|
322
|
-
# help [command] display help for command
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
Note that `batch` executes its commands in sequence, rather than in parallel!
|
|
326
|
-
|
|
327
|
-
To understand why, imagine running `npm install` in a dozen repos from the same command line. The visual feedback would be impossible to follow, and if something broke you'd have a really hard time figuring out why.
|
|
328
|
-
|
|
329
|
-
Instead, everything runs in sequence, and you get a clear record of exactly what happened and where. Also worth noting that many complex processes are resource hogs: you would not _want_ to run a dozen Serverless deployments at once!
|
|
101
|
+
Learn more in the [Config guide formats section](./guides/config.md#formats).
|
|
330
102
|
|
|
331
|
-
|
|
103
|
+
## CLI basics
|
|
332
104
|
|
|
333
|
-
|
|
105
|
+
The shipped CLI is plugin‑first:
|
|
334
106
|
|
|
335
|
-
|
|
107
|
+
- Execute commands within your dotenv context using the `cmd` subcommand or the parent alias:
|
|
108
|
+
- `getdotenv cmd ...` or `getdotenv -c 'echo $APP_SETTING'`
|
|
109
|
+
- Quoting, alias conflicts, expansion behavior. [More info...](./guides/shipped/cmd.md)
|
|
110
|
+
- Normalize shell behavior across platforms; use `--shell` (default OS shell) or `--shell-off`, and enable capture for CI:
|
|
111
|
+
- [Shell guide](./guides/shell.md)
|
|
112
|
+
- Execute across multiple working directories with the [`batch` plugin][guides/shipped/batch.md](./guides/shipped/batch.md)
|
|
336
113
|
|
|
337
|
-
|
|
114
|
+
## Diagnostics (trace, capture, redact, entropy)
|
|
338
115
|
|
|
339
|
-
-
|
|
116
|
+
- `--trace [keys...]` prints per‑key origin (dotenv | parent | unset) before spawning.
|
|
117
|
+
- Deterministic output for CI: set `GETDOTENV_STDIO=pipe` or pass `--capture`.
|
|
118
|
+
- Presentation‑time redaction for secret‑like keys: `--redact` / `--redact-off` (+ `--redact-pattern` for additional key matches).
|
|
119
|
+
- Optional entropy warnings (length/printable/threshold gated) for likely secrets in logs/trace.
|
|
340
120
|
|
|
341
|
-
|
|
342
|
-
{ "scripts": { "script": "getdotenv echo $APP_SETTING" } }
|
|
343
|
-
```
|
|
121
|
+
Learn more:
|
|
344
122
|
|
|
345
|
-
|
|
123
|
+
- [Shell & capture](./guides/shell.md)
|
|
124
|
+
- [Authoring diagnostics](./guides/authoring/diagnostics.md) (redaction & entropy)
|
|
346
125
|
|
|
347
|
-
|
|
348
|
-
```json
|
|
349
|
-
{ "scripts": { "script": "getdotenv -c 'echo $APP_SETTING'" } }
|
|
350
|
-
```
|
|
351
|
-
Now `npm run script -- -e dev` applies `-e` to `getdotenv`, which loads and expands variables before executing the inner command.
|
|
126
|
+
## Shipped plugins
|
|
352
127
|
|
|
353
|
-
|
|
128
|
+
- [cmd](./guides/shipped/cmd.md) — execute a command (with parent alias)
|
|
129
|
+
- [batch](./guides/shipped/batch.md) — run a command across multiple working directories
|
|
130
|
+
- [aws](./guides/shipped/aws.md) — establish a session and optionally forward to AWS CLI
|
|
131
|
+
- [init](./guides/shipped/init.md) — scaffold config files and a host‑based CLI skeleton
|
|
354
132
|
|
|
355
|
-
|
|
356
|
-
- On POSIX shells, prefer single quotes to prevent the outer shell from expanding `$VAR` before Node sees it. On PowerShell, single quotes are also literal.
|
|
357
|
-
- Script-level shell overrides (`scripts[name].shell`) still take precedence over the global `--shell`.
|
|
133
|
+
Also see the [shipped plugins overview](./guides/shipped/index.md).
|
|
358
134
|
|
|
359
|
-
|
|
135
|
+
## Authoring your own CLI & plugins
|
|
360
136
|
|
|
361
|
-
|
|
362
|
-
- POSIX example:
|
|
363
|
-
```
|
|
364
|
-
getdotenv --cmd 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
|
|
365
|
-
```
|
|
366
|
-
- PowerShell example (single quotes are literal in PowerShell):
|
|
367
|
-
```
|
|
368
|
-
getdotenv --cmd 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
|
|
369
|
-
```
|
|
370
|
-
- If you do not need to pass additional parent flags after the command, you can prefer the subcommand form instead:
|
|
371
|
-
```
|
|
372
|
-
getdotenv --shell-off cmd node -e "console.log(process.env.APP_SETTING ?? '')"
|
|
373
|
-
```
|
|
137
|
+
The host resolves dotenv context once per invocation, overlays config, validates, and then runs plugins with a typed options bag.
|
|
374
138
|
|
|
375
|
-
|
|
139
|
+
- [Lifecycle & wiring](./guides/authoring/lifecycle.md)
|
|
140
|
+
- [Executing shell commands](./guides/authoring/exec.md) from plugins
|
|
141
|
+
- [Config & validation](./guides/authoring/config.md) for plugins
|
|
376
142
|
|
|
377
|
-
|
|
378
|
-
- For debugging environment composition, use:
|
|
379
|
-
```
|
|
380
|
-
getdotenv --trace [keys...] cmd node -e "0"
|
|
381
|
-
```
|
|
382
|
-
When provided without keys, `--trace` emits a concise origin line for every key (parent | dotenv | unset) to stderr before the child process launches.
|
|
143
|
+
## API Reference
|
|
383
144
|
|
|
384
|
-
|
|
145
|
+
[Typed API docs](https://docs.karmanivero.us/get-dotenv) are built with TypeDoc.
|
|
385
146
|
|
|
386
|
-
##
|
|
147
|
+
## Changelog
|
|
387
148
|
|
|
388
|
-
|
|
389
|
-
paths and public/private/env axes.
|
|
390
|
-
- [Shell execution behavior](./guides/shell.md) - How commands run cross‑platform;
|
|
391
|
-
quoting rules, default shells, and capture tips.
|
|
392
|
-
- [Config files and overlays](./guides/config.md) - Author JSON/YAML/JS/TS config and
|
|
393
|
-
apply privacy/source overlays (always‑on).
|
|
394
|
-
- [Authoring Plugins](./guides/authoring/index.md) - Compose CLIs with once‑per‑invoke dotenv context and plugin lifecycles.
|
|
395
|
-
- [Shipped Plugins](./guides/shipped/index.md) - The get‑dotenv host ships a small set of plugins that cover needs.
|
|
149
|
+
See [CHANGELOG.md](./CHANGELOG.md)
|
|
396
150
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
The guides are also included in the [hosted API docs](https://docs.karmanivero.us/get-dotenv).
|
|
151
|
+
## License
|
|
400
152
|
|
|
401
|
-
|
|
153
|
+
BSD‑3‑Clause — see [LICENSE](./LICENSE)
|