@karmaniverous/get-dotenv 5.2.0 → 5.2.2
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 +37 -1
- package/dist/getdotenv.cli.mjs +163 -15
- package/dist/index.cjs +3589 -1455
- package/dist/index.d.cts +43 -2
- package/dist/index.d.mts +43 -2
- package/dist/index.d.ts +43 -2
- package/dist/index.mjs +3590 -1457
- package/dist/plugins-cmd.cjs +2501 -0
- package/dist/plugins-cmd.d.cts +311 -0
- package/dist/plugins-cmd.d.mts +311 -0
- package/dist/plugins-cmd.d.ts +311 -0
- package/dist/plugins-cmd.mjs +2498 -0
- package/dist/plugins-demo.cjs +330 -0
- package/dist/plugins-demo.d.cts +291 -0
- package/dist/plugins-demo.d.mts +291 -0
- package/dist/plugins-demo.d.ts +291 -0
- package/dist/plugins-demo.mjs +328 -0
- package/package.json +23 -3
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var execa = require('execa');
|
|
4
|
+
|
|
5
|
+
// Minimal tokenizer for shell-off execution:
|
|
6
|
+
// Splits by whitespace while preserving quoted segments (single or double quotes).
|
|
7
|
+
const tokenize = (command) => {
|
|
8
|
+
const out = [];
|
|
9
|
+
let cur = '';
|
|
10
|
+
let quote = null;
|
|
11
|
+
for (let i = 0; i < command.length; i++) {
|
|
12
|
+
const c = command.charAt(i);
|
|
13
|
+
if (quote) {
|
|
14
|
+
if (c === quote) {
|
|
15
|
+
// Support doubled quotes inside a quoted segment (Windows/PowerShell style):
|
|
16
|
+
// "" -> " and '' -> '
|
|
17
|
+
const next = command.charAt(i + 1);
|
|
18
|
+
if (next === quote) {
|
|
19
|
+
cur += quote;
|
|
20
|
+
i += 1; // skip the second quote
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
// end of quoted segment
|
|
24
|
+
quote = null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
cur += c;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
if (c === '"' || c === "'") {
|
|
33
|
+
quote = c;
|
|
34
|
+
}
|
|
35
|
+
else if (/\s/.test(c)) {
|
|
36
|
+
if (cur) {
|
|
37
|
+
out.push(cur);
|
|
38
|
+
cur = '';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
cur += c;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (cur)
|
|
47
|
+
out.push(cur);
|
|
48
|
+
return out;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const dbg = (...args) => {
|
|
52
|
+
if (process.env.GETDOTENV_DEBUG) {
|
|
53
|
+
// Use stderr to avoid interfering with stdout assertions
|
|
54
|
+
console.error('[getdotenv:run]', ...args);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
// Strip repeated symmetric outer quotes (single or double) until stable.
|
|
58
|
+
// This is safe for argv arrays passed to execa (no quoting needed) and avoids
|
|
59
|
+
// passing quote characters through to Node (e.g., for `node -e "<code>"`).
|
|
60
|
+
// Handles stacked quotes from shells like PowerShell: """code""" -> code.
|
|
61
|
+
const stripOuterQuotes = (s) => {
|
|
62
|
+
let out = s;
|
|
63
|
+
// Repeatedly trim only when the entire string is wrapped in matching quotes.
|
|
64
|
+
// Stop as soon as the ends are asymmetric or no quotes remain.
|
|
65
|
+
while (out.length >= 2) {
|
|
66
|
+
const a = out.charAt(0);
|
|
67
|
+
const b = out.charAt(out.length - 1);
|
|
68
|
+
const symmetric = (a === '"' && b === '"') || (a === "'" && b === "'");
|
|
69
|
+
if (!symmetric)
|
|
70
|
+
break;
|
|
71
|
+
out = out.slice(1, -1);
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
};
|
|
75
|
+
// Convert NodeJS.ProcessEnv (string | undefined values) to the shape execa
|
|
76
|
+
// expects (Readonly<Partial<Record<string, string>>>), dropping undefineds.
|
|
77
|
+
const sanitizeEnv = (env) => {
|
|
78
|
+
if (!env)
|
|
79
|
+
return undefined;
|
|
80
|
+
const entries = Object.entries(env).filter((e) => typeof e[1] === 'string');
|
|
81
|
+
return entries.length > 0 ? Object.fromEntries(entries) : undefined;
|
|
82
|
+
};
|
|
83
|
+
const runCommand = async (command, shell, opts) => {
|
|
84
|
+
if (shell === false) {
|
|
85
|
+
let file;
|
|
86
|
+
let args = [];
|
|
87
|
+
if (Array.isArray(command)) {
|
|
88
|
+
file = command[0];
|
|
89
|
+
args = command.slice(1).map(stripOuterQuotes);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
const tokens = tokenize(command);
|
|
93
|
+
file = tokens[0];
|
|
94
|
+
args = tokens.slice(1);
|
|
95
|
+
}
|
|
96
|
+
if (!file)
|
|
97
|
+
return 0;
|
|
98
|
+
dbg('exec (plain)', { file, args, stdio: opts.stdio });
|
|
99
|
+
// Build options without injecting undefined properties (exactOptionalPropertyTypes).
|
|
100
|
+
const envSan = sanitizeEnv(opts.env);
|
|
101
|
+
const plainOpts = {};
|
|
102
|
+
if (opts.cwd !== undefined)
|
|
103
|
+
plainOpts.cwd = opts.cwd;
|
|
104
|
+
if (envSan !== undefined)
|
|
105
|
+
plainOpts.env = envSan;
|
|
106
|
+
if (opts.stdio !== undefined)
|
|
107
|
+
plainOpts.stdio = opts.stdio;
|
|
108
|
+
const result = await execa.execa(file, args, plainOpts);
|
|
109
|
+
if (opts.stdio === 'pipe' && result.stdout) {
|
|
110
|
+
process.stdout.write(result.stdout + (result.stdout.endsWith('\n') ? '' : '\n'));
|
|
111
|
+
}
|
|
112
|
+
const exit = result?.exitCode;
|
|
113
|
+
dbg('exit (plain)', { exitCode: exit });
|
|
114
|
+
return typeof exit === 'number' ? exit : Number.NaN;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
const commandStr = Array.isArray(command) ? command.join(' ') : command;
|
|
118
|
+
dbg('exec (shell)', {
|
|
119
|
+
shell: typeof shell === 'string' ? shell : 'custom',
|
|
120
|
+
stdio: opts.stdio,
|
|
121
|
+
command: commandStr,
|
|
122
|
+
});
|
|
123
|
+
const envSan = sanitizeEnv(opts.env);
|
|
124
|
+
const shellOpts = { shell };
|
|
125
|
+
if (opts.cwd !== undefined)
|
|
126
|
+
shellOpts.cwd = opts.cwd;
|
|
127
|
+
if (envSan !== undefined)
|
|
128
|
+
shellOpts.env = envSan;
|
|
129
|
+
if (opts.stdio !== undefined)
|
|
130
|
+
shellOpts.stdio = opts.stdio;
|
|
131
|
+
const result = await execa.execaCommand(commandStr, shellOpts);
|
|
132
|
+
const out = result?.stdout;
|
|
133
|
+
if (opts.stdio === 'pipe' && out) {
|
|
134
|
+
process.stdout.write(out + (out.endsWith('\n') ? '' : '\n'));
|
|
135
|
+
}
|
|
136
|
+
const exit = result?.exitCode;
|
|
137
|
+
dbg('exit (shell)', { exitCode: exit });
|
|
138
|
+
return typeof exit === 'number' ? exit : Number.NaN;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Define a GetDotenv CLI plugin with compositional helpers.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* const parent = definePlugin(\{ id: 'p', setup(cli) \{ /* ... *\/ \} \})
|
|
147
|
+
* .use(childA)
|
|
148
|
+
* .use(childB);
|
|
149
|
+
*/
|
|
150
|
+
const definePlugin = (spec) => {
|
|
151
|
+
const { children = [], ...rest } = spec;
|
|
152
|
+
const plugin = {
|
|
153
|
+
...rest,
|
|
154
|
+
children: [...children],
|
|
155
|
+
use(child) {
|
|
156
|
+
this.children.push(child);
|
|
157
|
+
return this;
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
return plugin;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Batch services (neutral): resolve command and shell settings.
|
|
165
|
+
* Shared by the generator path and the batch plugin to avoid circular deps.
|
|
166
|
+
*/
|
|
167
|
+
/**
|
|
168
|
+
* Resolve a command string from the {@link Scripts} table.
|
|
169
|
+
* A script may be expressed as a string or an object with a `cmd` property.
|
|
170
|
+
*
|
|
171
|
+
* @param scripts - Optional scripts table.
|
|
172
|
+
* @param command - User-provided command name or string.
|
|
173
|
+
* @returns Resolved command string (falls back to the provided command).
|
|
174
|
+
*/
|
|
175
|
+
const resolveCommand = (scripts, command) => scripts && typeof scripts[command] === 'object'
|
|
176
|
+
? scripts[command].cmd
|
|
177
|
+
: (scripts?.[command] ?? command);
|
|
178
|
+
/**
|
|
179
|
+
* Resolve the shell setting for a given command:
|
|
180
|
+
* - If the script entry is an object, prefer its `shell` override.
|
|
181
|
+
* - Otherwise use the provided `shell` (string | boolean).
|
|
182
|
+
*
|
|
183
|
+
* @param scripts - Optional scripts table.
|
|
184
|
+
* @param command - User-provided command name or string.
|
|
185
|
+
* @param shell - Global shell preference (string | boolean).
|
|
186
|
+
*/
|
|
187
|
+
const resolveShell = (scripts, command, shell) => scripts && typeof scripts[command] === 'object'
|
|
188
|
+
? (scripts[command].shell ?? false)
|
|
189
|
+
: (shell ?? false);
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Demo plugin (educational).
|
|
193
|
+
*
|
|
194
|
+
* Purpose
|
|
195
|
+
* - Showcase how to build a plugin for the GetDotenv CLI host.
|
|
196
|
+
* - Demonstrate:
|
|
197
|
+
* - Accessing the resolved dotenv context (ctx).
|
|
198
|
+
* - Executing child processes with explicit env injection.
|
|
199
|
+
* - Resolving commands via scripts and honoring per-script shell overrides.
|
|
200
|
+
* - Thin adapters: business logic stays minimal; use shared helpers.
|
|
201
|
+
*
|
|
202
|
+
* Key host APIs used:
|
|
203
|
+
* - definePlugin: declare a plugin with setup and optional afterResolve.
|
|
204
|
+
* - cli.ns(name): create a namespaced subcommand under the root CLI.
|
|
205
|
+
* - cli.getCtx(): access \{ optionsResolved, dotenv, plugins?, pluginConfigs? \}.
|
|
206
|
+
*
|
|
207
|
+
* Design notes
|
|
208
|
+
* - We use the shared runCommand() helper so behavior matches the built-in
|
|
209
|
+
* cmd/batch plugins (env sanitization, plain vs shell execution, stdio).
|
|
210
|
+
* - We inject ctx.dotenv into child env explicitly to avoid bleeding prior
|
|
211
|
+
* secrets from process.env when exclusions are set (e.g., --exclude-private).
|
|
212
|
+
* - We resolve scripts and shell using shared helpers to honor overrides:
|
|
213
|
+
* resolveCommand(scripts, input) and resolveShell(scripts, input, shell).
|
|
214
|
+
*
|
|
215
|
+
* Usage (examples)
|
|
216
|
+
* getdotenv demo ctx
|
|
217
|
+
* getdotenv demo run --print APP_SETTING
|
|
218
|
+
* getdotenv demo script echo OK
|
|
219
|
+
* getdotenv --trace demo run --print ENV_SETTING
|
|
220
|
+
*/
|
|
221
|
+
const demoPlugin = () => definePlugin({
|
|
222
|
+
id: 'demo',
|
|
223
|
+
setup(cli) {
|
|
224
|
+
const logger = console;
|
|
225
|
+
const ns = cli
|
|
226
|
+
.ns('demo')
|
|
227
|
+
.description('Educational demo of host/plugin features (context, child exec, scripts/shell)');
|
|
228
|
+
/**
|
|
229
|
+
* demo ctx
|
|
230
|
+
* Print a summary of the current dotenv context.
|
|
231
|
+
*
|
|
232
|
+
* Notes:
|
|
233
|
+
* - The host resolves context once per invocation in a preSubcommand hook
|
|
234
|
+
* (added by enhanceGetDotenvCli.passOptions() in the shipped CLI).
|
|
235
|
+
* - ctx.dotenv contains the final merged values after overlays/dynamics.
|
|
236
|
+
*/
|
|
237
|
+
ns.command('ctx')
|
|
238
|
+
.description('Print a summary of the current dotenv context')
|
|
239
|
+
.action(() => {
|
|
240
|
+
const ctx = cli.getCtx();
|
|
241
|
+
const dotenv = ctx?.dotenv ?? {};
|
|
242
|
+
const keys = Object.keys(dotenv).sort();
|
|
243
|
+
const sample = keys.slice(0, 5);
|
|
244
|
+
logger.log('[demo] Context summary:');
|
|
245
|
+
logger.log(`- keys: ${keys.length.toString()}`);
|
|
246
|
+
logger.log(`- sample keys: ${sample.join(', ') || '(none)'}`);
|
|
247
|
+
logger.log('- tip: use "--trace [keys...]" for per-key diagnostics');
|
|
248
|
+
});
|
|
249
|
+
/**
|
|
250
|
+
* demo run [--print KEY]
|
|
251
|
+
* Execute a small child process that prints a dotenv value.
|
|
252
|
+
*
|
|
253
|
+
* Design:
|
|
254
|
+
* - Use shell-off + argv array to avoid cross-platform quoting pitfalls.
|
|
255
|
+
* - Inject ctx.dotenv explicitly into the child env.
|
|
256
|
+
* - Inherit stdio so output streams live (works well outside CI).
|
|
257
|
+
*
|
|
258
|
+
* Tip:
|
|
259
|
+
* - For deterministic capture in CI, run with "--capture" (or set
|
|
260
|
+
* GETDOTENV_STDIO=pipe). The shipped CLI honors both.
|
|
261
|
+
*/
|
|
262
|
+
ns.command('run')
|
|
263
|
+
.description('Run a small child process under the current dotenv (shell-off)')
|
|
264
|
+
.option('--print <key>', 'dotenv key to print', 'APP_SETTING')
|
|
265
|
+
.action(async (opts) => {
|
|
266
|
+
const key = typeof opts.print === 'string' && opts.print.length > 0
|
|
267
|
+
? opts.print
|
|
268
|
+
: 'APP_SETTING';
|
|
269
|
+
// Build a minimal node -e payload via argv array (avoid quoting issues).
|
|
270
|
+
const code = `console.log(process.env.${key} ?? "")`;
|
|
271
|
+
const ctx = cli.getCtx();
|
|
272
|
+
const dotenv = (ctx?.dotenv ?? {});
|
|
273
|
+
// Inherit stdio for an interactive demo. Use --capture for CI.
|
|
274
|
+
await runCommand(['node', '-e', code], false, {
|
|
275
|
+
env: { ...process.env, ...dotenv },
|
|
276
|
+
stdio: 'inherit',
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
/**
|
|
280
|
+
* demo script [command...]
|
|
281
|
+
* Resolve and execute a command using the current scripts table and
|
|
282
|
+
* shell preference (with per-script overrides).
|
|
283
|
+
*
|
|
284
|
+
* How it works:
|
|
285
|
+
* - We read the merged CLI options persisted by the shipped CLI’s
|
|
286
|
+
* passOptions() hook on the current command instance’s parent.
|
|
287
|
+
* - resolveCommand resolves a script name → cmd or passes through a raw
|
|
288
|
+
* command string.
|
|
289
|
+
* - resolveShell chooses the appropriate shell:
|
|
290
|
+
* scripts[name].shell ?? global shell (string|boolean).
|
|
291
|
+
*/
|
|
292
|
+
ns.command('script')
|
|
293
|
+
.description('Resolve a command via scripts and execute it with the proper shell')
|
|
294
|
+
.argument('[command...]')
|
|
295
|
+
.action(async (commandParts, _opts, thisCommand) => {
|
|
296
|
+
// Safely access the parent’s merged options (installed by passOptions()).
|
|
297
|
+
const parent = thisCommand.parent;
|
|
298
|
+
const bag = (parent?.getDotenvCliOptions ?? {});
|
|
299
|
+
const input = Array.isArray(commandParts)
|
|
300
|
+
? commandParts.map(String).join(' ')
|
|
301
|
+
: '';
|
|
302
|
+
if (!input) {
|
|
303
|
+
logger.log('[demo] Please provide a command or script name, e.g. "echo OK" or "git-status".');
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const resolved = resolveCommand(bag?.scripts, input);
|
|
307
|
+
const shell = resolveShell(bag?.scripts, input, bag?.shell);
|
|
308
|
+
// Compose child env (parent + ctx.dotenv). This mirrors cmd/batch behavior.
|
|
309
|
+
const ctx = cli.getCtx();
|
|
310
|
+
const dotenv = (ctx?.dotenv ?? {});
|
|
311
|
+
await runCommand(resolved, shell, {
|
|
312
|
+
env: { ...process.env, ...dotenv },
|
|
313
|
+
stdio: 'inherit',
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
},
|
|
317
|
+
/**
|
|
318
|
+
* Optional: afterResolve can initialize per-plugin state using ctx.dotenv.
|
|
319
|
+
* For the demo we just log once to hint where such logic would live.
|
|
320
|
+
*/
|
|
321
|
+
afterResolve(_cli, ctx) {
|
|
322
|
+
const keys = Object.keys(ctx.dotenv);
|
|
323
|
+
if (keys.length > 0) {
|
|
324
|
+
// Keep noise low; a single-line breadcrumb is sufficient for the demo.
|
|
325
|
+
console.error('[demo] afterResolve: dotenv keys loaded:', keys.length);
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
exports.demoPlugin = demoPlugin;
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A minimal representation of an environment key/value mapping.
|
|
6
|
+
* Values may be `undefined` to represent "unset". */ type ProcessEnv = Record<string, string | undefined>;
|
|
7
|
+
/**
|
|
8
|
+
* Dynamic variable function signature. Receives the current expanded variables
|
|
9
|
+
* and the selected environment (if any), and returns either a string to set
|
|
10
|
+
* or `undefined` to unset/skip the variable.
|
|
11
|
+
*/
|
|
12
|
+
type GetDotenvDynamicFunction = (vars: ProcessEnv, env: string | undefined) => string | undefined;
|
|
13
|
+
type GetDotenvDynamic = Record<string, GetDotenvDynamicFunction | ReturnType<GetDotenvDynamicFunction>>;
|
|
14
|
+
type Logger = Record<string, (...args: unknown[]) => void> | typeof console;
|
|
15
|
+
/**
|
|
16
|
+
* Options passed programmatically to `getDotenv`.
|
|
17
|
+
*/
|
|
18
|
+
interface GetDotenvOptions {
|
|
19
|
+
/**
|
|
20
|
+
* default target environment (used if `env` is not provided)
|
|
21
|
+
*/
|
|
22
|
+
defaultEnv?: string;
|
|
23
|
+
/**
|
|
24
|
+
* token indicating a dotenv file
|
|
25
|
+
*/
|
|
26
|
+
dotenvToken: string;
|
|
27
|
+
/**
|
|
28
|
+
* path to JS/TS module default-exporting an object keyed to dynamic variable functions
|
|
29
|
+
*/
|
|
30
|
+
dynamicPath?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Programmatic dynamic variables map. When provided, this takes precedence
|
|
33
|
+
* over {@link GetDotenvOptions.dynamicPath}.
|
|
34
|
+
*/
|
|
35
|
+
dynamic?: GetDotenvDynamic;
|
|
36
|
+
/**
|
|
37
|
+
* target environment
|
|
38
|
+
*/
|
|
39
|
+
env?: string;
|
|
40
|
+
/**
|
|
41
|
+
* exclude dynamic variables from loading
|
|
42
|
+
*/
|
|
43
|
+
excludeDynamic?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* exclude environment-specific variables from loading
|
|
46
|
+
*/
|
|
47
|
+
excludeEnv?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* exclude global variables from loading
|
|
50
|
+
*/
|
|
51
|
+
excludeGlobal?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* exclude private variables from loading
|
|
54
|
+
*/
|
|
55
|
+
excludePrivate?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* exclude public variables from loading
|
|
58
|
+
*/
|
|
59
|
+
excludePublic?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* load dotenv variables to `process.env`
|
|
62
|
+
*/
|
|
63
|
+
loadProcess?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* log loaded dotenv variables to `logger`
|
|
66
|
+
*/
|
|
67
|
+
log?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* logger object (defaults to console)
|
|
70
|
+
*/
|
|
71
|
+
logger?: Logger;
|
|
72
|
+
/**
|
|
73
|
+
* if populated, writes consolidated dotenv file to this path (follows dotenvExpand rules)
|
|
74
|
+
*/
|
|
75
|
+
outputPath?: string;
|
|
76
|
+
/**
|
|
77
|
+
* array of input directory paths
|
|
78
|
+
*/
|
|
79
|
+
paths?: string[];
|
|
80
|
+
/**
|
|
81
|
+
* filename token indicating private variables
|
|
82
|
+
*/
|
|
83
|
+
privateToken?: string;
|
|
84
|
+
/**
|
|
85
|
+
* explicit variables to include
|
|
86
|
+
*/
|
|
87
|
+
vars?: ProcessEnv;
|
|
88
|
+
/**
|
|
89
|
+
* Reserved: config loader flag (no-op).
|
|
90
|
+
* The plugin-first host and generator paths already use the config
|
|
91
|
+
* loader/overlay pipeline unconditionally (no-op when no config files
|
|
92
|
+
* are present). This flag is accepted for forward compatibility but
|
|
93
|
+
* currently has no effect.
|
|
94
|
+
*/
|
|
95
|
+
useConfigLoader?: boolean;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
type Scripts = Record<string, string | {
|
|
99
|
+
cmd: string;
|
|
100
|
+
shell?: string | boolean;
|
|
101
|
+
}>;
|
|
102
|
+
/**
|
|
103
|
+
* Options passed programmatically to `getDotenvCli`.
|
|
104
|
+
*/
|
|
105
|
+
interface GetDotenvCliOptions extends Omit<GetDotenvOptions, 'paths' | 'vars'> {
|
|
106
|
+
/**
|
|
107
|
+
* Logs CLI internals when true.
|
|
108
|
+
*/
|
|
109
|
+
debug?: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Strict mode: fail the run when env validation issues are detected
|
|
112
|
+
* (schema or requiredKeys). Warns by default when false or unset.
|
|
113
|
+
*/
|
|
114
|
+
strict?: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* Redaction (presentation): mask secret-like values in logs/trace.
|
|
117
|
+
*/
|
|
118
|
+
redact?: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Entropy warnings (presentation): emit once-per-key warnings for high-entropy values.
|
|
121
|
+
*/
|
|
122
|
+
warnEntropy?: boolean;
|
|
123
|
+
entropyThreshold?: number;
|
|
124
|
+
entropyMinLength?: number;
|
|
125
|
+
entropyWhitelist?: string[];
|
|
126
|
+
redactPatterns?: string[];
|
|
127
|
+
/**
|
|
128
|
+
* When true, capture child stdout/stderr and re-emit after completion.
|
|
129
|
+
* Useful for tests/CI. Default behavior is streaming via stdio: 'inherit'.
|
|
130
|
+
*/
|
|
131
|
+
capture?: boolean;
|
|
132
|
+
/**
|
|
133
|
+
* A delimited string of paths to dotenv files.
|
|
134
|
+
*/
|
|
135
|
+
paths?: string;
|
|
136
|
+
/**
|
|
137
|
+
* A delimiter string with which to split `paths`. Only used if
|
|
138
|
+
* `pathsDelimiterPattern` is not provided.
|
|
139
|
+
*/
|
|
140
|
+
pathsDelimiter?: string;
|
|
141
|
+
/**
|
|
142
|
+
* A regular expression pattern with which to split `paths`. Supersedes
|
|
143
|
+
* `pathsDelimiter`.
|
|
144
|
+
*/
|
|
145
|
+
pathsDelimiterPattern?: string;
|
|
146
|
+
/**
|
|
147
|
+
* Scripts that can be executed from the CLI, either individually or via the batch subcommand.
|
|
148
|
+
*/
|
|
149
|
+
scripts?: Scripts;
|
|
150
|
+
/**
|
|
151
|
+
* Determines how commands and scripts are executed. If `false` or
|
|
152
|
+
* `undefined`, commands are executed as plain Javascript using the default
|
|
153
|
+
* execa parser. If `true`, commands are executed using the default OS shell
|
|
154
|
+
* parser. Otherwise the user may provide a specific shell string (e.g.
|
|
155
|
+
* `/bin/bash`)
|
|
156
|
+
*/
|
|
157
|
+
shell?: string | boolean;
|
|
158
|
+
/**
|
|
159
|
+
* A delimited string of key-value pairs declaratively specifying variables &
|
|
160
|
+
* values to be loaded in addition to any dotenv files.
|
|
161
|
+
*/
|
|
162
|
+
vars?: string;
|
|
163
|
+
/**
|
|
164
|
+
* A string with which to split keys from values in `vars`. Only used if
|
|
165
|
+
* `varsDelimiterPattern` is not provided.
|
|
166
|
+
*/
|
|
167
|
+
varsAssignor?: string;
|
|
168
|
+
/**
|
|
169
|
+
* A regular expression pattern with which to split variable names from values
|
|
170
|
+
* in `vars`. Supersedes `varsAssignor`.
|
|
171
|
+
*/
|
|
172
|
+
varsAssignorPattern?: string;
|
|
173
|
+
/**
|
|
174
|
+
* A string with which to split `vars` into key-value pairs. Only used if
|
|
175
|
+
* `varsDelimiterPattern` is not provided.
|
|
176
|
+
*/
|
|
177
|
+
varsDelimiter?: string;
|
|
178
|
+
/**
|
|
179
|
+
* A regular expression pattern with which to split `vars` into key-value
|
|
180
|
+
* pairs. Supersedes `varsDelimiter`.
|
|
181
|
+
*/
|
|
182
|
+
varsDelimiterPattern?: string;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** * Per-invocation context shared with plugins and actions. */
|
|
186
|
+
type GetDotenvCliCtx<TOptions extends GetDotenvOptions = GetDotenvOptions> = {
|
|
187
|
+
optionsResolved: TOptions;
|
|
188
|
+
dotenv: ProcessEnv;
|
|
189
|
+
plugins?: Record<string, unknown>;
|
|
190
|
+
pluginConfigs?: Record<string, unknown>;
|
|
191
|
+
};
|
|
192
|
+
declare const HELP_HEADER_SYMBOL: unique symbol;
|
|
193
|
+
/**
|
|
194
|
+
* Plugin-first CLI host for get-dotenv. Extends Commander.Command.
|
|
195
|
+
*
|
|
196
|
+
* Responsibilities:
|
|
197
|
+
* - Resolve options strictly and compute dotenv context (resolveAndLoad).
|
|
198
|
+
* - Expose a stable accessor for the current context (getCtx).
|
|
199
|
+
* - Provide a namespacing helper (ns).
|
|
200
|
+
* - Support composable plugins with parent → children install and afterResolve.
|
|
201
|
+
*
|
|
202
|
+
* NOTE: This host is additive and does not alter the legacy CLI.
|
|
203
|
+
*/
|
|
204
|
+
declare class GetDotenvCli<TOptions extends GetDotenvOptions = GetDotenvOptions> extends Command {
|
|
205
|
+
#private;
|
|
206
|
+
/** Registered top-level plugins (composition happens via .use()) */
|
|
207
|
+
private _plugins;
|
|
208
|
+
/** One-time installation guard */
|
|
209
|
+
private _installed;
|
|
210
|
+
/** Optional header line to prepend in help output */
|
|
211
|
+
private [HELP_HEADER_SYMBOL];
|
|
212
|
+
constructor(alias?: string);
|
|
213
|
+
/**
|
|
214
|
+
* Resolve options (strict) and compute dotenv context. * Stores the context on the instance under a symbol.
|
|
215
|
+
*/
|
|
216
|
+
resolveAndLoad(customOptions?: Partial<TOptions>): Promise<GetDotenvCliCtx<TOptions>>;
|
|
217
|
+
/**
|
|
218
|
+
* Retrieve the current invocation context (if any).
|
|
219
|
+
*/
|
|
220
|
+
getCtx(): GetDotenvCliCtx<TOptions> | undefined;
|
|
221
|
+
/**
|
|
222
|
+
* Retrieve the merged root CLI options bag (if set by passOptions()).
|
|
223
|
+
* Downstream-safe: no generics required.
|
|
224
|
+
*/
|
|
225
|
+
getOptions(): GetDotenvCliOptions | undefined;
|
|
226
|
+
/** Internal: set the merged root options bag for this run. */
|
|
227
|
+
_setOptionsBag(bag: GetDotenvCliOptions): void;
|
|
228
|
+
/** * Convenience helper to create a namespaced subcommand.
|
|
229
|
+
*/
|
|
230
|
+
ns(name: string): Command;
|
|
231
|
+
/**
|
|
232
|
+
* Tag options added during the provided callback as 'app' for grouped help.
|
|
233
|
+
* Allows downstream apps to demarcate their root-level options.
|
|
234
|
+
*/
|
|
235
|
+
tagAppOptions<T>(fn: (root: Command) => T): T;
|
|
236
|
+
/**
|
|
237
|
+
* Branding helper: set CLI name/description/version and optional help header.
|
|
238
|
+
* If version is omitted and importMetaUrl is provided, attempts to read the
|
|
239
|
+
* nearest package.json version (best-effort; non-fatal on failure).
|
|
240
|
+
*/
|
|
241
|
+
brand(args: {
|
|
242
|
+
name?: string;
|
|
243
|
+
description?: string;
|
|
244
|
+
version?: string;
|
|
245
|
+
importMetaUrl?: string;
|
|
246
|
+
helpHeader?: string;
|
|
247
|
+
}): Promise<this>;
|
|
248
|
+
/**
|
|
249
|
+
* Register a plugin for installation (parent level).
|
|
250
|
+
* Installation occurs on first resolveAndLoad() (or explicit install()).
|
|
251
|
+
*/
|
|
252
|
+
use(plugin: GetDotenvCliPlugin): this;
|
|
253
|
+
/**
|
|
254
|
+
* Install all registered plugins in parent → children (pre-order).
|
|
255
|
+
* Runs only once per CLI instance.
|
|
256
|
+
*/
|
|
257
|
+
install(): Promise<void>;
|
|
258
|
+
/**
|
|
259
|
+
* Run afterResolve hooks for all plugins (parent → children).
|
|
260
|
+
*/
|
|
261
|
+
private _runAfterResolve;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Public plugin contract used by the GetDotenv CLI host. */ interface GetDotenvCliPlugin {
|
|
265
|
+
id?: string /**
|
|
266
|
+
* Setup phase: register commands and wiring on the provided CLI instance. * Runs parent → children (pre-order).
|
|
267
|
+
*/;
|
|
268
|
+
setup: (cli: GetDotenvCli) => void | Promise<void>;
|
|
269
|
+
/**
|
|
270
|
+
* After the dotenv context is resolved, initialize any clients/secrets
|
|
271
|
+
* or attach per-plugin state under ctx.plugins (by convention).
|
|
272
|
+
* Runs parent → children (pre-order).
|
|
273
|
+
*/
|
|
274
|
+
afterResolve?: (cli: GetDotenvCli, ctx: GetDotenvCliCtx) => void | Promise<void>;
|
|
275
|
+
/**
|
|
276
|
+
* Optional Zod schema for this plugin's config slice (from config.plugins[id]).
|
|
277
|
+
* When provided, the host validates the merged config under the guarded loader path.
|
|
278
|
+
*/
|
|
279
|
+
configSchema?: ZodType;
|
|
280
|
+
/**
|
|
281
|
+
* Compositional children. Installed after the parent per pre-order.
|
|
282
|
+
*/ children: GetDotenvCliPlugin[];
|
|
283
|
+
/**
|
|
284
|
+
* Compose a child plugin. Returns the parent to enable chaining.
|
|
285
|
+
*/
|
|
286
|
+
use: (child: GetDotenvCliPlugin) => GetDotenvCliPlugin;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
declare const demoPlugin: () => GetDotenvCliPlugin;
|
|
290
|
+
|
|
291
|
+
export { demoPlugin };
|