@karmaniverous/get-dotenv 6.2.3 → 6.3.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 +1 -0
- package/dist/chunks/AwsRestJsonProtocol-Dv5q8CFK.mjs +932 -0
- package/dist/chunks/createCli-BSn6Be40.mjs +440 -0
- package/dist/chunks/externalDataInterceptor-pqHO-Qmn.mjs +19 -0
- package/dist/chunks/getSSOTokenFromFile-otmZHSRV.mjs +22 -0
- package/dist/chunks/helpConfig-CGejgwWW.mjs +12 -0
- package/dist/chunks/index-B18W-ELX.mjs +273 -0
- package/dist/chunks/index-BNcKuiBy.mjs +9375 -0
- package/dist/chunks/index-Bi0RIILn.mjs +25 -0
- package/dist/chunks/index-BqZ3PB6c.mjs +375 -0
- package/dist/chunks/index-C4Ac6feq.mjs +355 -0
- package/dist/chunks/index-C6uLiKpC.mjs +703 -0
- package/dist/chunks/index-CGg5wWCm.mjs +188 -0
- package/dist/chunks/index-CXpZ0pei.mjs +579 -0
- package/dist/chunks/index-CYoFYXZv.mjs +103 -0
- package/dist/chunks/index-DFNcs3pR.mjs +444 -0
- package/dist/chunks/index-DLQEHTw4.mjs +83 -0
- package/dist/chunks/index-DtRaL61T.mjs +380 -0
- package/dist/chunks/index-eZMlmESW.mjs +522 -0
- package/dist/chunks/invoke-DuRPU1oC.mjs +60 -0
- package/dist/chunks/loadModuleDefault-Dj8B3Stt.mjs +123 -0
- package/dist/chunks/loadSso-CJ_XUhEj.mjs +413 -0
- package/dist/chunks/loader-CePOf74i.mjs +347 -0
- package/dist/chunks/overlayEnv-Bqh_kPGA.mjs +235 -0
- package/dist/chunks/package-boo9EyYs.mjs +5 -0
- package/dist/chunks/parseKnownFiles-B6x1cUmR.mjs +23 -0
- package/dist/chunks/readMergedOptions-DLBDzpXX.mjs +1627 -0
- package/dist/chunks/resolveCliOptions-_qtsVxda.mjs +139 -0
- package/dist/chunks/sdk-stream-mixin-DCdC70Up.mjs +167 -0
- package/dist/chunks/spawnEnv-CQwFu7ZJ.mjs +307 -0
- package/dist/chunks/types-DdqcXCV1.mjs +59 -0
- package/dist/chunks/validate-CDl0rE6k.mjs +61 -0
- package/dist/cli.mjs +39 -19307
- package/dist/cliHost.mjs +21 -2800
- package/dist/config.mjs +11 -509
- package/dist/env-overlay.mjs +7 -337
- package/dist/getdotenv.cli.mjs +39 -19305
- package/dist/index.d.ts +485 -2
- package/dist/index.mjs +657 -19322
- package/dist/plugins-aws.d.ts +1 -4
- package/dist/plugins-aws.mjs +66 -2568
- package/dist/plugins-batch.mjs +17 -2573
- package/dist/plugins-cmd.mjs +20 -3094
- package/dist/plugins-init.d.ts +8 -0
- package/dist/plugins-init.mjs +85 -2297
- package/dist/plugins.mjs +36 -18817
- package/package.json +1 -2
- package/dist/templates/cli/index.ts +0 -25
- package/dist/templates/cli/plugins/hello/defaultAction.ts +0 -27
- package/dist/templates/cli/plugins/hello/index.ts +0 -26
- package/dist/templates/cli/plugins/hello/options.ts +0 -31
- package/dist/templates/cli/plugins/hello/strangerAction.ts +0 -20
- package/dist/templates/cli/plugins/hello/types.ts +0 -13
- package/dist/templates/config/js/getdotenv.config.js +0 -20
- package/dist/templates/config/json/local/getdotenv.config.local.json +0 -7
- package/dist/templates/config/json/public/getdotenv.config.json +0 -9
- package/dist/templates/config/public/getdotenv.config.json +0 -8
- package/dist/templates/config/ts/getdotenv.config.ts +0 -28
- package/dist/templates/config/yaml/local/getdotenv.config.local.yaml +0 -7
- package/dist/templates/config/yaml/public/getdotenv.config.yaml +0 -7
- package/dist/templates/defaultAction.ts +0 -27
- package/dist/templates/getdotenv.config.js +0 -20
- package/dist/templates/getdotenv.config.json +0 -9
- package/dist/templates/getdotenv.config.local.json +0 -7
- package/dist/templates/getdotenv.config.local.yaml +0 -7
- package/dist/templates/getdotenv.config.ts +0 -28
- package/dist/templates/getdotenv.config.yaml +0 -7
- package/dist/templates/hello/defaultAction.ts +0 -27
- package/dist/templates/hello/index.ts +0 -26
- package/dist/templates/hello/options.ts +0 -31
- package/dist/templates/hello/strangerAction.ts +0 -20
- package/dist/templates/hello/types.ts +0 -13
- package/dist/templates/index.ts +0 -26
- package/dist/templates/js/getdotenv.config.js +0 -20
- package/dist/templates/json/local/getdotenv.config.local.json +0 -7
- package/dist/templates/json/public/getdotenv.config.json +0 -9
- package/dist/templates/local/getdotenv.config.local.json +0 -7
- package/dist/templates/local/getdotenv.config.local.yaml +0 -7
- package/dist/templates/options.ts +0 -31
- package/dist/templates/plugins/hello/defaultAction.ts +0 -27
- package/dist/templates/plugins/hello/index.ts +0 -26
- package/dist/templates/plugins/hello/options.ts +0 -31
- package/dist/templates/plugins/hello/strangerAction.ts +0 -20
- package/dist/templates/plugins/hello/types.ts +0 -13
- package/dist/templates/public/getdotenv.config.json +0 -9
- package/dist/templates/public/getdotenv.config.yaml +0 -7
- package/dist/templates/strangerAction.ts +0 -20
- package/dist/templates/ts/getdotenv.config.ts +0 -28
- package/dist/templates/types.ts +0 -13
- package/dist/templates/yaml/local/getdotenv.config.local.yaml +0 -7
- package/dist/templates/yaml/public/getdotenv.config.yaml +0 -7
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export { f as ENV_ACCOUNT_ID, e as ENV_CREDENTIAL_SCOPE, d as ENV_EXPIRATION, E as ENV_KEY, b as ENV_SECRET, c as ENV_SESSION, g as fromEnv } from './index-BNcKuiBy.mjs';
|
|
2
|
+
import './readMergedOptions-DLBDzpXX.mjs';
|
|
3
|
+
import 'zod';
|
|
4
|
+
import '@commander-js/extra-typings';
|
|
5
|
+
import './overlayEnv-Bqh_kPGA.mjs';
|
|
6
|
+
import 'fs-extra';
|
|
7
|
+
import 'node:path';
|
|
8
|
+
import './loadModuleDefault-Dj8B3Stt.mjs';
|
|
9
|
+
import 'crypto';
|
|
10
|
+
import 'path';
|
|
11
|
+
import 'url';
|
|
12
|
+
import 'nanoid';
|
|
13
|
+
import 'dotenv';
|
|
14
|
+
import './loader-CePOf74i.mjs';
|
|
15
|
+
import 'package-directory';
|
|
16
|
+
import 'yaml';
|
|
17
|
+
import 'execa';
|
|
18
|
+
import 'buffer';
|
|
19
|
+
import 'os';
|
|
20
|
+
import 'node:fs/promises';
|
|
21
|
+
import 'http';
|
|
22
|
+
import 'https';
|
|
23
|
+
import 'stream';
|
|
24
|
+
import 'process';
|
|
25
|
+
import 'node:fs';
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { l as redactTriple, m as maybeWarnEntropy, r as readMergedOptions, g as getDotenvCliOptions2Options, d as definePlugin } from './readMergedOptions-DLBDzpXX.mjs';
|
|
2
|
+
import 'execa';
|
|
3
|
+
import 'fs-extra';
|
|
4
|
+
import 'node:path';
|
|
5
|
+
import 'crypto';
|
|
6
|
+
import 'path';
|
|
7
|
+
import 'url';
|
|
8
|
+
import '@commander-js/extra-typings';
|
|
9
|
+
import 'nanoid';
|
|
10
|
+
import 'dotenv';
|
|
11
|
+
import './loader-CePOf74i.mjs';
|
|
12
|
+
import 'package-directory';
|
|
13
|
+
import 'yaml';
|
|
14
|
+
import { z } from 'zod';
|
|
15
|
+
import { b as resolveCommand, c as resolveShell, t as tokenize, s as shouldCapture, r as runCommand, d as buildSpawnEnv } from './spawnEnv-CQwFu7ZJ.mjs';
|
|
16
|
+
import { m as maybePreserveNodeEvalArgv, c as composeNestedEnv, s as stripOne } from './invoke-DuRPU1oC.mjs';
|
|
17
|
+
import { c as dotenvExpandFromProcessEnv } from './overlayEnv-Bqh_kPGA.mjs';
|
|
18
|
+
import { r as resolveCliOptions, b as baseGetDotenvCliOptions } from './resolveCliOptions-_qtsVxda.mjs';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Trace child env composition with redaction and entropy warnings.
|
|
22
|
+
* Presentation-only: does not mutate env; writes lines via the provided sink.
|
|
23
|
+
*/
|
|
24
|
+
function traceChildEnv(opts) {
|
|
25
|
+
const { parentEnv, dotenv, keys, redact, redactPatterns, warnEntropy, entropyThreshold, entropyMinLength, entropyWhitelist, write, } = opts;
|
|
26
|
+
const parentKeys = Object.keys(parentEnv);
|
|
27
|
+
const dotenvKeys = Object.keys(dotenv);
|
|
28
|
+
const allKeys = Array.from(new Set([...parentKeys, ...dotenvKeys])).sort();
|
|
29
|
+
const effectiveKeys = Array.isArray(keys) && keys.length > 0 ? keys : allKeys;
|
|
30
|
+
// Redaction options for display
|
|
31
|
+
const redOpts = {};
|
|
32
|
+
if (redact) {
|
|
33
|
+
redOpts.redact = true;
|
|
34
|
+
if (Array.isArray(redactPatterns))
|
|
35
|
+
redOpts.redactPatterns = redactPatterns;
|
|
36
|
+
}
|
|
37
|
+
// Entropy warning options
|
|
38
|
+
const entOpts = {};
|
|
39
|
+
if (typeof warnEntropy === 'boolean')
|
|
40
|
+
entOpts.warnEntropy = warnEntropy;
|
|
41
|
+
if (typeof entropyThreshold === 'number')
|
|
42
|
+
entOpts.entropyThreshold = entropyThreshold;
|
|
43
|
+
if (typeof entropyMinLength === 'number')
|
|
44
|
+
entOpts.entropyMinLength = entropyMinLength;
|
|
45
|
+
if (Array.isArray(entropyWhitelist))
|
|
46
|
+
entOpts.entropyWhitelist = entropyWhitelist;
|
|
47
|
+
for (const k of effectiveKeys) {
|
|
48
|
+
const parentVal = parentEnv[k];
|
|
49
|
+
const dot = dotenv[k];
|
|
50
|
+
const final = dot !== undefined ? dot : parentVal;
|
|
51
|
+
const origin = dot !== undefined
|
|
52
|
+
? 'dotenv'
|
|
53
|
+
: parentVal !== undefined
|
|
54
|
+
? 'parent'
|
|
55
|
+
: 'unset';
|
|
56
|
+
const tripleIn = {};
|
|
57
|
+
if (parentVal !== undefined)
|
|
58
|
+
tripleIn.parent = parentVal;
|
|
59
|
+
if (dot !== undefined)
|
|
60
|
+
tripleIn.dotenv = dot;
|
|
61
|
+
if (final !== undefined)
|
|
62
|
+
tripleIn.final = final;
|
|
63
|
+
const triple = redactTriple(k, tripleIn, redOpts);
|
|
64
|
+
write(`[trace] key=${k} origin=${origin} parent=${triple.parent ?? ''} dotenv=${triple.dotenv ?? ''} final=${triple.final ?? ''}`);
|
|
65
|
+
maybeWarnEntropy(k, final, origin, entOpts, (line) => {
|
|
66
|
+
write(line);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** src/plugins/cmd/actions/runner.ts
|
|
72
|
+
* Unified runner for cmd subcommand and parent alias.
|
|
73
|
+
* - Resolves command via scripts
|
|
74
|
+
* - Emits --trace diagnostics (centralized helper with redaction/entropy)
|
|
75
|
+
* - Preserves Node -e argv under shell-off
|
|
76
|
+
* - Normalizes child env and honors capture
|
|
77
|
+
*/
|
|
78
|
+
/**
|
|
79
|
+
* Execute a command using the current CLI context.
|
|
80
|
+
* Resolves command/shell via scripts, emits traces, and handles execution.
|
|
81
|
+
*
|
|
82
|
+
* @param cli - The CLI instance.
|
|
83
|
+
* @param merged - Merged CLI options.
|
|
84
|
+
* @param command - Command string or array of args.
|
|
85
|
+
* @param _opts - Execution options (e.g. origin label).
|
|
86
|
+
*/
|
|
87
|
+
async function runCmdWithContext(cli, merged, command, _opts) {
|
|
88
|
+
const { logger, debug, capture, scripts: scriptsCfg, shell: shellPref, trace, redact, redactPatterns, warnEntropy, entropyThreshold, entropyMinLength, entropyWhitelist, } = merged;
|
|
89
|
+
const dotenv = cli.getCtx().dotenv;
|
|
90
|
+
// Build input string and note original argv (when available).
|
|
91
|
+
const parts = Array.isArray(command) ? command.map(String) : [];
|
|
92
|
+
const inputStr = Array.isArray(command) ? parts.join(' ') : command;
|
|
93
|
+
// Resolve command and shell from scripts/global.
|
|
94
|
+
const resolved = resolveCommand(scriptsCfg, inputStr);
|
|
95
|
+
if (debug)
|
|
96
|
+
logger.debug('\n*** command ***\n', `'${resolved}'`);
|
|
97
|
+
const shellSetting = resolveShell(scriptsCfg, inputStr, shellPref);
|
|
98
|
+
// Diagnostics: --trace [keys...]
|
|
99
|
+
const traceOpt = trace;
|
|
100
|
+
if (traceOpt) {
|
|
101
|
+
traceChildEnv({
|
|
102
|
+
parentEnv: process.env,
|
|
103
|
+
dotenv,
|
|
104
|
+
...(Array.isArray(traceOpt) ? { keys: traceOpt } : {}),
|
|
105
|
+
...(redact ? { redact: true } : {}),
|
|
106
|
+
...(redact && Array.isArray(redactPatterns) ? { redactPatterns } : {}),
|
|
107
|
+
...(typeof warnEntropy === 'boolean' ? { warnEntropy } : {}),
|
|
108
|
+
...(typeof entropyThreshold === 'number' ? { entropyThreshold } : {}),
|
|
109
|
+
...(typeof entropyMinLength === 'number' ? { entropyMinLength } : {}),
|
|
110
|
+
...(Array.isArray(entropyWhitelist) ? { entropyWhitelist } : {}),
|
|
111
|
+
write: (line) => {
|
|
112
|
+
try {
|
|
113
|
+
process.stderr.write(line + '\n');
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
/* ignore */
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
// Preserve Node -e argv under shell-off when the script did not remap input.
|
|
122
|
+
let commandArg = resolved;
|
|
123
|
+
if (shellSetting === false && resolved === inputStr) {
|
|
124
|
+
if (Array.isArray(command)) {
|
|
125
|
+
commandArg = maybePreserveNodeEvalArgv(parts);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
const toks = tokenize(inputStr, { preserveDoubledQuotes: true });
|
|
129
|
+
if (toks.length >= 3 &&
|
|
130
|
+
(toks[0] ?? '').toLowerCase() === 'node' &&
|
|
131
|
+
(toks[1] === '-e' || toks[1] === '--eval')) {
|
|
132
|
+
toks[2] = stripOne(toks[2] ?? '');
|
|
133
|
+
commandArg = toks;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// Child env: compose nested bag and sanitize.
|
|
138
|
+
const childOverlay = composeNestedEnv(merged, dotenv);
|
|
139
|
+
const captureFlag = shouldCapture(capture);
|
|
140
|
+
let exit;
|
|
141
|
+
try {
|
|
142
|
+
exit = await runCommand(commandArg, shellSetting, {
|
|
143
|
+
env: buildSpawnEnv(process.env, childOverlay),
|
|
144
|
+
stdio: captureFlag ? 'pipe' : 'inherit',
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
// Under unit tests, execa may be mocked to return undefined which causes
|
|
149
|
+
// pickResult access to throw. Treat as success to allow call-count assertions.
|
|
150
|
+
if (process.env.VITEST_WORKER_ID)
|
|
151
|
+
return 0;
|
|
152
|
+
throw e;
|
|
153
|
+
}
|
|
154
|
+
return typeof exit === 'number' ? exit : 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Attach the default "cmd" subcommand action.
|
|
159
|
+
* Mirrors the prior inline implementation in cmd/index.ts.
|
|
160
|
+
*/
|
|
161
|
+
const attachCmdDefaultAction = (cli, cmd, aliasKey) => {
|
|
162
|
+
cmd.action(async function (...allArgs) {
|
|
163
|
+
// Commander passes: [...positionals, options, thisCommand]
|
|
164
|
+
const thisCommand = allArgs[allArgs.length - 1];
|
|
165
|
+
const commandParts = allArgs[0];
|
|
166
|
+
const args = Array.isArray(commandParts)
|
|
167
|
+
? commandParts.map(String)
|
|
168
|
+
: [];
|
|
169
|
+
// No-op when invoked as the default command with no args.
|
|
170
|
+
if (args.length === 0)
|
|
171
|
+
return;
|
|
172
|
+
const merged = readMergedOptions(thisCommand);
|
|
173
|
+
const parent = thisCommand.parent;
|
|
174
|
+
if (!parent)
|
|
175
|
+
throw new Error('parent command not found');
|
|
176
|
+
// Conflict detection: if an alias option is present on parent, do not
|
|
177
|
+
// also accept positional cmd args.
|
|
178
|
+
if (aliasKey) {
|
|
179
|
+
const p = parent;
|
|
180
|
+
const pv = typeof p.optsWithGlobals === 'function'
|
|
181
|
+
? p.optsWithGlobals()
|
|
182
|
+
: typeof p.opts === 'function'
|
|
183
|
+
? p.opts()
|
|
184
|
+
: {};
|
|
185
|
+
const ov = pv[aliasKey];
|
|
186
|
+
if (ov !== undefined) {
|
|
187
|
+
merged.logger.error(`--${aliasKey} option conflicts with cmd subcommand.`);
|
|
188
|
+
process.exit(0);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
await runCmdWithContext(cli, merged, args);
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Attach options/arguments for the cmd plugin mount.
|
|
197
|
+
*
|
|
198
|
+
* Note: the plugin description is owned by `src/plugins/cmd/index.ts` and must
|
|
199
|
+
* not be set here.
|
|
200
|
+
*
|
|
201
|
+
* @param cli - The `cmd` command mount.
|
|
202
|
+
* @returns The same `cli` instance for chaining.
|
|
203
|
+
*
|
|
204
|
+
* @internal
|
|
205
|
+
*/
|
|
206
|
+
function attachCmdOptions(cli) {
|
|
207
|
+
return cli
|
|
208
|
+
.enablePositionalOptions()
|
|
209
|
+
.passThroughOptions()
|
|
210
|
+
.argument('[command...]');
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Install the parent-level invoker (alias) for the cmd plugin.
|
|
215
|
+
* Unifies naming with batch attachParentInvoker; behavior unchanged.
|
|
216
|
+
*/
|
|
217
|
+
const attachCmdParentInvoker = (cli, options, plugin) => {
|
|
218
|
+
const dbg = (...args) => {
|
|
219
|
+
if (process.env.GETDOTENV_DEBUG) {
|
|
220
|
+
try {
|
|
221
|
+
const line = args
|
|
222
|
+
.map((a) => (typeof a === 'string' ? a : JSON.stringify(a)))
|
|
223
|
+
.join(' ');
|
|
224
|
+
process.stderr.write(`[getdotenv:alias] ${line}\n`);
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
/* ignore */
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
const aliasSpec = typeof options.optionAlias === 'string'
|
|
232
|
+
? { flags: options.optionAlias, description: undefined}
|
|
233
|
+
: options.optionAlias;
|
|
234
|
+
if (!aliasSpec)
|
|
235
|
+
return;
|
|
236
|
+
const deriveKey = (flags) => {
|
|
237
|
+
// install alias option
|
|
238
|
+
if (process.env.GETDOTENV_DEBUG) {
|
|
239
|
+
console.error('[getdotenv:alias] install alias option', flags);
|
|
240
|
+
}
|
|
241
|
+
const long = flags.split(/[ ,|]+/).find((f) => f.startsWith('--')) ?? '--cmd';
|
|
242
|
+
const name = long.replace(/^--/, '');
|
|
243
|
+
return name.replace(/-([a-z])/g, (_m, c) => c.toUpperCase());
|
|
244
|
+
};
|
|
245
|
+
const aliasKey = deriveKey(aliasSpec.flags);
|
|
246
|
+
// Expose the option on the parent (root) command.
|
|
247
|
+
const parentCmd = cli.parent ??
|
|
248
|
+
cli;
|
|
249
|
+
const desc = aliasSpec.description ??
|
|
250
|
+
'alias of cmd subcommand; provide command tokens (variadic)';
|
|
251
|
+
parentCmd.option(aliasSpec.flags, desc);
|
|
252
|
+
// Tag the just-added parent option for grouped help rendering at the root.
|
|
253
|
+
const optsArr = parentCmd.options;
|
|
254
|
+
if (optsArr.length > 0) {
|
|
255
|
+
const last = optsArr[optsArr.length - 1];
|
|
256
|
+
if (last)
|
|
257
|
+
parentCmd.setOptionGroup(last, 'plugin:cmd');
|
|
258
|
+
}
|
|
259
|
+
// Shared alias executor for either preAction or preSubcommand hooks.
|
|
260
|
+
// Ensure we only execute once even if both hooks fire in a single parse.
|
|
261
|
+
const aliasState = { handled: false };
|
|
262
|
+
const maybeRun = async (thisCommand) => {
|
|
263
|
+
dbg('maybeRun:start');
|
|
264
|
+
// Read plugin config expand default; fall back to undefined (handled in maybeRunAlias)
|
|
265
|
+
let expandDefault = undefined;
|
|
266
|
+
try {
|
|
267
|
+
const cfg = plugin.readConfig(cli);
|
|
268
|
+
if (typeof cfg.expand === 'boolean')
|
|
269
|
+
expandDefault = cfg.expand;
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
/* config may be unavailable before resolve; default handled downstream */
|
|
273
|
+
}
|
|
274
|
+
// Inspect parent options and rawArgs to detect alias-only invocation.
|
|
275
|
+
if (aliasState.handled)
|
|
276
|
+
return;
|
|
277
|
+
const cmd = thisCommand;
|
|
278
|
+
dbg('maybeRun:rawArgs', cmd.rawArgs ?? []);
|
|
279
|
+
const raw = typeof cmd.opts === 'function' ? cmd.opts() : {};
|
|
280
|
+
const val = raw[aliasKey];
|
|
281
|
+
const provided = typeof val === 'string'
|
|
282
|
+
? val.length > 0
|
|
283
|
+
: Array.isArray(val)
|
|
284
|
+
? val.length > 0
|
|
285
|
+
: false;
|
|
286
|
+
dbg('maybeRun:aliasKey', aliasKey, 'provided', provided, 'value', val);
|
|
287
|
+
if (!provided)
|
|
288
|
+
return;
|
|
289
|
+
const childNames = cmd.commands.flatMap((c) => [c.name(), ...c.aliases()]);
|
|
290
|
+
dbg('maybeRun:childNames', childNames);
|
|
291
|
+
const hasSub = (cmd.rawArgs ?? []).some((t) => childNames.includes(t));
|
|
292
|
+
dbg('maybeRun:hasSub', hasSub);
|
|
293
|
+
if (hasSub)
|
|
294
|
+
return; // do not run alias when an explicit subcommand is present
|
|
295
|
+
aliasState.handled = true;
|
|
296
|
+
// Merge CLI options and resolve dotenv context for this invocation.
|
|
297
|
+
const { merged } = resolveCliOptions(raw, baseGetDotenvCliOptions, process.env.getDotenvCliOptions);
|
|
298
|
+
const serviceOptions = getDotenvCliOptions2Options(merged);
|
|
299
|
+
await cli.resolveAndLoad(serviceOptions);
|
|
300
|
+
// Build input string and apply optional expansion (by config default).
|
|
301
|
+
const joined = typeof val === 'string'
|
|
302
|
+
? val
|
|
303
|
+
: Array.isArray(val)
|
|
304
|
+
? val.map(String).join(' ')
|
|
305
|
+
: '';
|
|
306
|
+
const effectiveExpand = expandDefault !== false;
|
|
307
|
+
const expanded = dotenvExpandFromProcessEnv(joined);
|
|
308
|
+
const input = effectiveExpand && expanded !== undefined ? expanded : joined;
|
|
309
|
+
// Hand off to shared runner and terminate (except under tests).
|
|
310
|
+
const exitCode = await runCmdWithContext(cli, merged, input);
|
|
311
|
+
const underTests = process.env.GETDOTENV_TEST === '1' ||
|
|
312
|
+
typeof process.env.VITEST_WORKER_ID === 'string';
|
|
313
|
+
if (!underTests) {
|
|
314
|
+
process.exit(typeof exitCode === 'number' ? exitCode : 0);
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
parentCmd.hook('preAction', async (thisCommand) => {
|
|
318
|
+
await maybeRun(thisCommand);
|
|
319
|
+
});
|
|
320
|
+
parentCmd.hook('preSubcommand', async (thisCommand) => {
|
|
321
|
+
await maybeRun(thisCommand);
|
|
322
|
+
});
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Zod schema for cmd plugin configuration.
|
|
327
|
+
*/
|
|
328
|
+
const cmdPluginConfigSchema = z
|
|
329
|
+
.object({
|
|
330
|
+
/** When true, expand the alias value before execution (default behavior when omitted). */
|
|
331
|
+
expand: z.boolean().optional(),
|
|
332
|
+
})
|
|
333
|
+
.strict();
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* @packageDocumentation
|
|
337
|
+
* Cmd plugin subpath. Provides the `cmd` subcommand and an optional parent‑level
|
|
338
|
+
* alias to execute a command within the resolved dotenv context.
|
|
339
|
+
*/
|
|
340
|
+
/**
|
|
341
|
+
* Cmd plugin: executes a command using the current getdotenv CLI context.
|
|
342
|
+
* Registers the `cmd` subcommand and optionally attaches a parent-level alias (e.g. `-c`).
|
|
343
|
+
*
|
|
344
|
+
* @param options - Plugin configuration options.
|
|
345
|
+
*/
|
|
346
|
+
const cmdPlugin = (options = {}) => {
|
|
347
|
+
const plugin = definePlugin({
|
|
348
|
+
ns: 'cmd',
|
|
349
|
+
configSchema: cmdPluginConfigSchema,
|
|
350
|
+
setup(cli) {
|
|
351
|
+
const aliasSpec = typeof options.optionAlias === 'string'
|
|
352
|
+
? { flags: options.optionAlias}
|
|
353
|
+
: options.optionAlias;
|
|
354
|
+
const deriveKey = (flags) => {
|
|
355
|
+
const long = flags.split(/[ ,|]+/).find((f) => f.startsWith('--')) ?? '--cmd';
|
|
356
|
+
const name = long.replace(/^--/, '');
|
|
357
|
+
return name.replace(/-([a-z])/g, (_m, c) => c.toUpperCase());
|
|
358
|
+
};
|
|
359
|
+
const aliasKey = aliasSpec ? deriveKey(aliasSpec.flags) : undefined;
|
|
360
|
+
// Mount is the command ('cmd'); attach default action.
|
|
361
|
+
cli.description('Execute command according to the --shell option, conflicts with --command option (default subcommand)');
|
|
362
|
+
// Options/arguments (positional payload, argv routing) are attached separately.
|
|
363
|
+
attachCmdOptions(cli);
|
|
364
|
+
attachCmdDefaultAction(cli, cli, aliasKey);
|
|
365
|
+
// Parent-attached option alias (optional, unified naming).
|
|
366
|
+
if (aliasSpec !== undefined) {
|
|
367
|
+
attachCmdParentInvoker(cli, options, plugin);
|
|
368
|
+
}
|
|
369
|
+
return undefined;
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
return plugin;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
export { cmdPlugin as c, traceChildEnv as t };
|