@karmaniverous/get-dotenv 6.0.0-1 → 6.1.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 +91 -379
- package/dist/cli.d.ts +569 -0
- package/dist/cli.mjs +18877 -0
- package/dist/cliHost.d.ts +528 -184
- package/dist/cliHost.mjs +1977 -1428
- package/dist/config.d.ts +191 -14
- package/dist/config.mjs +266 -81
- package/dist/env-overlay.d.ts +223 -16
- package/dist/env-overlay.mjs +185 -4
- package/dist/getdotenv.cli.mjs +18025 -3196
- package/dist/index.d.ts +623 -256
- package/dist/index.mjs +18045 -3206
- package/dist/plugins-aws.d.ts +221 -91
- package/dist/plugins-aws.mjs +2411 -369
- package/dist/plugins-batch.d.ts +300 -103
- package/dist/plugins-batch.mjs +2560 -484
- package/dist/plugins-cmd.d.ts +229 -106
- package/dist/plugins-cmd.mjs +2518 -790
- package/dist/plugins-init.d.ts +221 -95
- package/dist/plugins-init.mjs +2170 -105
- package/dist/plugins.d.ts +246 -125
- package/dist/plugins.mjs +17941 -1968
- package/dist/templates/cli/index.ts +25 -0
- package/{templates/cli/ts → dist/templates/cli}/plugins/hello.ts +13 -9
- 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 +42 -0
- package/dist/templates/index.ts +25 -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 +42 -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 +42 -39
- package/templates/cli/index.ts +25 -0
- package/templates/cli/plugins/hello.ts +42 -0
- package/templates/config/js/getdotenv.config.js +8 -3
- 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 +8 -3
- package/templates/config/yaml/public/getdotenv.config.yaml +0 -3
- package/dist/plugins-demo.d.ts +0 -204
- package/dist/plugins-demo.mjs +0 -496
- package/templates/cli/ts/index.ts +0 -9
package/README.md
CHANGED
|
@@ -1,112 +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
|
-
[](
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
- [
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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!
|
|
58
|
-
|
|
8
|
+
[](./CHANGELOG.md)
|
|
9
|
+
[](./LICENSE)
|
|
10
|
+
|
|
11
|
+
get‑dotenv helps you:
|
|
12
|
+
|
|
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.
|
|
18
|
+
|
|
19
|
+
See full guides:
|
|
20
|
+
|
|
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)
|
|
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
|
-
## Quick Start
|
|
64
|
-
|
|
65
|
-
- Zero‑install one‑off command with your env (POSIX):
|
|
66
|
-
|
|
67
|
-
```
|
|
68
|
-
npx @karmaniverous/get-dotenv -c 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
Windows/PowerShell:
|
|
72
|
-
|
|
73
|
-
```
|
|
74
|
-
npx @karmaniverous/get-dotenv -c 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
- Scaffold config and a CLI skeleton (JSON config + .local + CLI “acme”):
|
|
78
|
-
|
|
79
|
-
```
|
|
80
|
-
npx @karmaniverous/get-dotenv init . \
|
|
81
|
-
--config-format json \
|
|
82
|
-
--with-local \
|
|
83
|
-
--cli-name acme \
|
|
84
|
-
--force
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
- Install (for programmatic use and local scripts):
|
|
88
|
-
```
|
|
89
|
-
npm install @karmaniverous/get-dotenv
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## Getting Started
|
|
93
|
-
|
|
94
|
-
- One‑off CLI with your env: `npx @karmaniverous/get-dotenv -c 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'` — see [Shell execution behavior](./guides/shell.md) and [cmd plugin](./guides/shipped/cmd.md).
|
|
95
|
-
- Programmatic load: `const vars = await getDotenv({ env: 'dev', paths: ['./'] });` — see [Config files and overlays](./guides/config.md).
|
|
96
|
-
- Embed a CLI quickly: use [createCli](#cli-embedding-createcli), or wire a [custom host](#custom-host-wire-plugins) to choose plugins.
|
|
97
|
-
- Scaffold config and a CLI skeleton: `npx @karmaniverous/get-dotenv init . --config-format json --with-local --cli-name acme` — see [init plugin](./guides/shipped/init.md).
|
|
98
|
-
|
|
99
|
-
## API Reference
|
|
100
|
-
|
|
101
|
-
[API documentation](https://docs.karmanivero.us/get-dotenv) is built with TypeDoc from the source code in this repository.
|
|
102
|
-
|
|
103
|
-
## Testing
|
|
104
|
-
|
|
105
|
-
This project uses Vitest with the V8 coverage provider. Run:
|
|
106
|
-
|
|
107
|
-
```bash
|
|
108
|
-
npm run test
|
|
109
|
-
```
|
|
42
|
+
- Node.js ≥ 20 (this repository pins 22.19.0 for CI/reproducibility)
|
|
110
43
|
|
|
111
44
|
## Installation
|
|
112
45
|
|
|
@@ -114,328 +47,107 @@ npm run test
|
|
|
114
47
|
npm install @karmaniverous/get-dotenv
|
|
115
48
|
```
|
|
116
49
|
|
|
117
|
-
##
|
|
118
|
-
|
|
119
|
-
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.
|
|
120
|
-
|
|
121
|
-
Examples:
|
|
50
|
+
## Quick Start
|
|
122
51
|
|
|
123
|
-
|
|
124
|
-
# JSON config + .local variant, and a CLI skeleton named "acme"
|
|
125
|
-
npx @karmaniverous/get-dotenv init . \
|
|
126
|
-
--config-format json \
|
|
127
|
-
--with-local \
|
|
128
|
-
--cli-name acme \
|
|
129
|
-
--force
|
|
130
|
-
```
|
|
52
|
+
Run a one‑off command with your env (parent alias so flags apply to getdotenv):
|
|
131
53
|
|
|
132
54
|
```bash
|
|
133
|
-
|
|
134
|
-
npx @karmaniverous/get-dotenv init ./apps/toolbox \
|
|
135
|
-
--config-format ts \
|
|
136
|
-
--cli-name toolbox
|
|
55
|
+
npx @karmaniverous/get-dotenv -c 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
|
|
137
56
|
```
|
|
138
57
|
|
|
139
|
-
|
|
58
|
+
Load programmatically:
|
|
140
59
|
|
|
141
|
-
|
|
142
|
-
- 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`).
|
|
143
|
-
- Precedence: `--force` > `--yes` > auto-detect (non-interactive => Skip All).
|
|
144
|
-
- Options overview:
|
|
145
|
-
- `--config-format <json|yaml|js|ts>`
|
|
146
|
-
- `--with-local` to generate `.local` alongside public config (JSON/YAML)
|
|
147
|
-
- `--cli-name <string>` for token substitution (`__CLI_NAME__`) in the CLI skeleton
|
|
148
|
-
- `--force` to overwrite all; `--yes` to skip all
|
|
149
|
-
|
|
150
|
-
Notes:
|
|
151
|
-
|
|
152
|
-
- Templates are shipped with the package and copied verbatim.
|
|
153
|
-
- The CLI skeleton replaces `__CLI_NAME__` tokens with your chosen name.
|
|
154
|
-
|
|
155
|
-
## Usage
|
|
156
|
-
|
|
157
|
-
```js
|
|
60
|
+
```ts
|
|
158
61
|
import { getDotenv } from '@karmaniverous/get-dotenv';
|
|
159
62
|
|
|
160
|
-
const
|
|
63
|
+
const vars = await getDotenv({ env: 'dev', paths: ['./'] });
|
|
64
|
+
console.log(vars.APP_SETTING);
|
|
161
65
|
```
|
|
162
66
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
See [Config files and overlays](./guides/config.md) for how to author defaults, overlays, validation, and diagnostics in JSON/YAML/JS/TS.
|
|
166
|
-
|
|
167
|
-
## CLI embedding (createCli)
|
|
168
|
-
|
|
169
|
-
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):
|
|
170
68
|
|
|
171
69
|
```ts
|
|
172
70
|
#!/usr/bin/env node
|
|
173
|
-
import { createCli } from '@karmaniverous/get-dotenv';
|
|
174
|
-
|
|
175
|
-
// Build a CLI and run with your argv; alias appears in help.
|
|
176
|
-
await createCli({
|
|
177
|
-
alias: 'mycli',
|
|
178
|
-
// Optional: override the help header (otherwise “mycli v<version>” is used).
|
|
179
|
-
branding: 'mycli v1.2.3',
|
|
180
|
-
}).run(process.argv.slice(2));
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
Notes:
|
|
184
|
-
|
|
185
|
-
- The host resolves dotenv context once per invocation and exposes subcommands: cmd, batch, aws, and init (see Guides below).
|
|
186
|
-
- Use `--trace [keys...]` and `--redact` for diagnostics without altering runtime values.
|
|
187
|
-
- Default shells are normalized across platforms: `/bin/bash` on POSIX and `powershell.exe` on Windows (overridable per‑script or globally).
|
|
188
|
-
- Help/exit behavior (important for embedding and tests):
|
|
189
|
-
- 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.
|
|
190
|
-
- 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).
|
|
191
|
-
- Interop matrix (embedding in different module systems):
|
|
192
|
-
- ESM dynamic:
|
|
193
|
-
```ts
|
|
194
|
-
const { createCli } = await import('@karmaniverous/get-dotenv');
|
|
195
|
-
await createCli({ alias: 'mycli' }).run(['-h']);
|
|
196
|
-
```
|
|
197
|
-
- CommonJS require (using built outputs for smoke checks):
|
|
198
|
-
```js
|
|
199
|
-
const { createCli } = require('@karmaniverous/get-dotenv/dist/index.cjs');
|
|
200
|
-
createCli({ alias: 'mycli' }).run(['-h']);
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
### Custom host (wire plugins)
|
|
204
|
-
|
|
205
|
-
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';
|
|
206
72
|
|
|
207
|
-
|
|
208
|
-
#!/usr/bin/env node
|
|
209
|
-
import type { Command } from 'commander';
|
|
210
|
-
import { GetDotenvCli } from '@karmaniverous/get-dotenv/cliHost';
|
|
211
|
-
import {
|
|
212
|
-
cmdPlugin,
|
|
213
|
-
batchPlugin,
|
|
214
|
-
awsPlugin,
|
|
215
|
-
initPlugin,
|
|
216
|
-
} from '@karmaniverous/get-dotenv/plugins';
|
|
217
|
-
// import { demoPlugin } from '@karmaniverous/get-dotenv/plugins/demo'; // optional
|
|
218
|
-
// import { helloPlugin } from './plugins/hello'; // your plugin
|
|
219
|
-
|
|
220
|
-
const program: Command = new GetDotenvCli('mycli');
|
|
221
|
-
await (program as unknown as GetDotenvCli).brand({
|
|
222
|
-
importMetaUrl: import.meta.url,
|
|
223
|
-
description: 'mycli',
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
program
|
|
227
|
-
.attachRootOptions({ loadProcess: false })
|
|
228
|
-
.use(cmdPlugin({ asDefault: true, optionAlias: '-c, --cmd <command...>' }))
|
|
229
|
-
.use(batchPlugin())
|
|
230
|
-
.use(awsPlugin())
|
|
231
|
-
.use(initPlugin())
|
|
232
|
-
// .use(demoPlugin()) // omit demo by default
|
|
233
|
-
// .use(helloPlugin()) // add your own plugin(s)
|
|
234
|
-
.passOptions({ loadProcess: false });
|
|
235
|
-
|
|
236
|
-
await program.parseAsync();
|
|
73
|
+
await createCli({ alias: 'toolbox' })();
|
|
237
74
|
```
|
|
238
75
|
|
|
239
|
-
|
|
76
|
+
More first steps and tips at [Getting Started](./guides/getting-started.md)
|
|
240
77
|
|
|
241
|
-
|
|
242
|
-
- See [Authoring Plugins → Lifecycle & Wiring](./guides/authoring/lifecycle.md) for a deeper walk‑through and best practices.
|
|
78
|
+
## Configuration & overlays
|
|
243
79
|
|
|
244
|
-
|
|
80
|
+
Author config in JSON/YAML/JS/TS at your project root. The loader is always on in the shipped host:
|
|
245
81
|
|
|
246
|
-
|
|
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`
|
|
247
87
|
|
|
248
|
-
|
|
88
|
+
Overlays apply by kind/env/privacy/source with clear precedence. Details and examples in the [Config guide](./guides/config.md).
|
|
249
89
|
|
|
250
|
-
|
|
251
|
-
export default {
|
|
252
|
-
SOME_DYNAMIC_VARIABLE: (dotenv) => someLogic(dotenv),
|
|
253
|
-
ANOTHER_DYNAMIC_VARIABLE: (dotenv) =>
|
|
254
|
-
someOtherLogic(dotenv.SOME_DYNAMIC_VARIABLE),
|
|
255
|
-
ONE_MORE_TIME: ({ DESTRUCTURED_VARIABLE, ANOTHER_DYNAMIC_VARIABLE }) =>
|
|
256
|
-
DESTRUCTURED_VARIABLE + ANOTHER_DYNAMIC_VARIABLE,
|
|
257
|
-
};
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
If the value corresponding to a key is a function, it will be executed with the current state of `dotenv` as its single argument and the result applied back to the `dotenv` object. Otherwise, the value will just be applied back to `dotenv`. (Although if you're going to do that then you might as well just create a public global variable in the first place.)
|
|
90
|
+
## Dynamic variables (JS/TS)
|
|
261
91
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
### TypeScript-first dynamic processing
|
|
265
|
-
|
|
266
|
-
You can write your dynamic module in TypeScript and point `dynamicPath` at a `.ts` file. Install [`esbuild`](https://esbuild.github.io/) as a dev dependency to enable automatic compilation:
|
|
92
|
+
Add dynamic keys that compute from the composed env. Programmatic or file‑based (JS/TS). For TypeScript, install `esbuild` for auto‑compile.
|
|
267
93
|
|
|
268
94
|
```ts
|
|
269
95
|
// dynamic.ts
|
|
270
96
|
export default {
|
|
271
|
-
MY_DYNAMIC: ({ APP_SETTING = '' }) => `${APP_SETTING}-ts`,
|
|
272
|
-
};
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
### TypeScript-first Vars-aware dynamic typing
|
|
276
|
-
|
|
277
|
-
You can bind the expected variable shape to get strong inference inside your dynamic map.
|
|
278
|
-
|
|
279
|
-
```ts
|
|
280
|
-
import { defineDynamic } from '@karmaniverous/get-dotenv';
|
|
281
|
-
|
|
282
|
-
type Vars = { APP_SETTING?: string; ENV_TAG?: string };
|
|
283
|
-
|
|
284
|
-
export const dynamic = defineDynamic<Vars>({
|
|
285
97
|
GREETING: ({ APP_SETTING = '' }) => `Hello ${APP_SETTING}`,
|
|
286
|
-
}
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
If `esbuild` is not installed and a direct import fails, `get-dotenv` attempts a simple fallback for single-file `.ts` modules without imports; otherwise it will throw with clear guidance to install `esbuild`.
|
|
290
|
-
|
|
291
|
-
Programmatic users can skip files entirely and pass dynamic variables directly:
|
|
292
|
-
|
|
293
|
-
```ts
|
|
294
|
-
import { getDotenv, defineDynamic } from '@karmaniverous/get-dotenv';
|
|
295
|
-
|
|
296
|
-
const dynamic = defineDynamic({
|
|
297
|
-
MY_DYNAMIC(vars, env) {
|
|
298
|
-
return `${vars.APP_SETTING}-${env ?? ''}`;
|
|
299
|
-
},
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
const vars = await getDotenv({ dynamic, paths: ['./'], env: 'dev' });
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
Notes:
|
|
306
|
-
|
|
307
|
-
- Programmatic `dynamic` takes precedence over `dynamicPath` when both are provided.
|
|
308
|
-
- Dynamic keys are evaluated progressively, so later keys can reference earlier results.
|
|
309
|
-
|
|
310
|
-
#### Troubleshooting
|
|
311
|
-
|
|
312
|
-
- “Unknown file extension '.ts'” when loading `dynamic.ts`:
|
|
313
|
-
- Install `esbuild` (`npm i -D esbuild`).
|
|
314
|
-
|
|
315
|
-
- “Unable to load dynamic TypeScript file …”:
|
|
316
|
-
- Install `esbuild`. A simple transpile fallback exists only for trivial single-file modules; any imports in `dynamic.ts` require `esbuild` bundling.
|
|
317
|
-
|
|
318
|
-
## Command Line Interface
|
|
319
|
-
|
|
320
|
-
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).
|
|
321
|
-
|
|
322
|
-
### Default shell behavior
|
|
323
|
-
|
|
324
|
-
To normalize behavior across platforms, the CLI resolves a default shell when `--shell` is true or omitted:
|
|
325
|
-
|
|
326
|
-
- POSIX: `/bin/bash`
|
|
327
|
-
- Windows: `powershell.exe`
|
|
328
|
-
|
|
329
|
-
### Batch Command
|
|
330
|
-
|
|
331
|
-
The `getdotenv` base CLI includes one very useful subcommand: `batch`.
|
|
332
|
-
|
|
333
|
-
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!
|
|
334
|
-
|
|
335
|
-
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.
|
|
336
|
-
|
|
337
|
-
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!
|
|
338
|
-
|
|
339
|
-
Lest you doubt what that kind of leverage can do for you, consider this:
|
|
340
|
-
|
|
341
|
-
[](https://github.com/karmaniverous)
|
|
342
|
-
|
|
343
|
-
```bash
|
|
344
|
-
> getdotenv batch -h
|
|
345
|
-
|
|
346
|
-
# Usage: getdotenv batch [options] [command]
|
|
347
|
-
#
|
|
348
|
-
# Batch command execution across multiple working directories.
|
|
349
|
-
#
|
|
350
|
-
# Options:
|
|
351
|
-
# -p, --pkg-cwd use nearest package directory as current working directory
|
|
352
|
-
# -r, --root-path <string> path to batch root directory from current working directory (default: "./")
|
|
353
|
-
# -g, --globs <string> space-delimited globs from root path (default: "*")
|
|
354
|
-
# -c, --command <string> command executed according to the base --shell option, conflicts with cmd subcommand (dotenv-expanded)
|
|
355
|
-
# -l, --list list working directories without executing command
|
|
356
|
-
# -e, --ignore-errors ignore errors and continue with next path
|
|
357
|
-
# -h, --help display help for command
|
|
358
|
-
#
|
|
359
|
-
# Commands:
|
|
360
|
-
# cmd execute command, conflicts with --command option (default subcommand)
|
|
361
|
-
# help [command] display help for command
|
|
98
|
+
};
|
|
362
99
|
```
|
|
363
100
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
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.
|
|
101
|
+
Learn more in the [Config guide formats section](./guides/config.md#formats).
|
|
367
102
|
|
|
368
|
-
|
|
103
|
+
## CLI basics
|
|
369
104
|
|
|
370
|
-
|
|
105
|
+
The shipped CLI is plugin‑first:
|
|
371
106
|
|
|
372
|
-
|
|
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)
|
|
373
113
|
|
|
374
|
-
|
|
114
|
+
## Diagnostics (trace, capture, redact, entropy)
|
|
375
115
|
|
|
376
|
-
|
|
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.
|
|
377
120
|
|
|
378
|
-
|
|
121
|
+
Learn more:
|
|
379
122
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
```
|
|
123
|
+
- [Shell & capture](./guides/shell.md)
|
|
124
|
+
- [Authoring diagnostics](./guides/authoring/diagnostics.md) (redaction & entropy)
|
|
383
125
|
|
|
384
|
-
|
|
126
|
+
## Shipped plugins
|
|
385
127
|
|
|
386
|
-
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
Now `npm run script -- -e dev` applies `-e` to `getdotenv`, which loads and expands variables before executing the inner command.
|
|
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
|
|
391
132
|
|
|
392
|
-
|
|
133
|
+
Also see the [shipped plugins overview](./guides/shipped/index.md).
|
|
393
134
|
|
|
394
|
-
|
|
395
|
-
- 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.
|
|
396
|
-
- Script-level shell overrides (`scripts[name].shell`) still take precedence over the global `--shell`.
|
|
135
|
+
## Authoring your own CLI & plugins
|
|
397
136
|
|
|
398
|
-
|
|
137
|
+
The host resolves dotenv context once per invocation, overlays config, validates, and then runs plugins with a typed options bag.
|
|
399
138
|
|
|
400
|
-
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
getdotenv --cmd 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
|
|
404
|
-
```
|
|
405
|
-
- PowerShell example (single quotes are literal in PowerShell):
|
|
406
|
-
```
|
|
407
|
-
getdotenv --cmd 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
|
|
408
|
-
```
|
|
409
|
-
- If you do not need to pass additional parent flags after the command, you can prefer the subcommand form instead:
|
|
410
|
-
```
|
|
411
|
-
getdotenv --shell-off cmd node -e "console.log(process.env.APP_SETTING ?? '')"
|
|
412
|
-
```
|
|
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
|
|
413
142
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
- To capture child stdout/stderr deterministically (e.g., in CI), either set the environment variable `GETDOTENV_STDIO=pipe` or pass `--capture`. Outputs are buffered and re-emitted after completion.
|
|
417
|
-
- For debugging environment composition, use:
|
|
418
|
-
```
|
|
419
|
-
getdotenv --trace [keys...] cmd node -e "0"
|
|
420
|
-
```
|
|
421
|
-
When provided without keys, `--trace` emits a concise origin line for every key (parent | dotenv | unset) to stderr before the child process launches.
|
|
422
|
-
|
|
423
|
-
---
|
|
143
|
+
## API Reference
|
|
424
144
|
|
|
425
|
-
|
|
145
|
+
[Typed API docs](https://docs.karmanivero.us/get-dotenv) are built with TypeDoc.
|
|
426
146
|
|
|
427
|
-
|
|
428
|
-
paths and public/private/env axes.
|
|
429
|
-
- [Shell execution behavior](./guides/shell.md) - How commands run cross‑platform;
|
|
430
|
-
quoting rules, default shells, and capture tips.
|
|
431
|
-
- [Config files and overlays](./guides/config.md) - Author JSON/YAML/JS/TS config and
|
|
432
|
-
apply privacy/source overlays (always‑on).
|
|
433
|
-
- [Authoring Plugins](./guides/authoring/index.md) - Compose CLIs with once‑per‑invoke dotenv context and plugin lifecycles.
|
|
434
|
-
- [Shipped Plugins](./guides/shipped/index.md) - The get‑dotenv host ships a small set of plugins that cover needs.
|
|
147
|
+
## Changelog
|
|
435
148
|
|
|
436
|
-
|
|
437
|
-
The guides are also included in the [hosted API docs](https://docs.karmanivero.us/get-dotenv).
|
|
149
|
+
See [CHANGELOG.md](./CHANGELOG.md)
|
|
438
150
|
|
|
439
|
-
|
|
151
|
+
## License
|
|
440
152
|
|
|
441
|
-
|
|
153
|
+
BSD‑3‑Clause — see [LICENSE](./LICENSE)
|