@karmaniverous/get-dotenv 4.6.0-0 → 5.0.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.
Files changed (48) hide show
  1. package/README.md +130 -23
  2. package/dist/cliHost.cjs +1078 -0
  3. package/dist/cliHost.d.cts +193 -0
  4. package/dist/cliHost.d.mts +193 -0
  5. package/dist/cliHost.d.ts +193 -0
  6. package/dist/cliHost.mjs +1074 -0
  7. package/dist/config.cjs +247 -0
  8. package/dist/config.d.cts +53 -0
  9. package/dist/config.d.mts +53 -0
  10. package/dist/config.d.ts +53 -0
  11. package/dist/config.mjs +242 -0
  12. package/dist/env-overlay.cjs +163 -0
  13. package/dist/env-overlay.d.cts +50 -0
  14. package/dist/env-overlay.d.mts +50 -0
  15. package/dist/env-overlay.d.ts +50 -0
  16. package/dist/env-overlay.mjs +161 -0
  17. package/dist/getdotenv.cli.mjs +2776 -733
  18. package/dist/index.cjs +886 -275
  19. package/dist/index.d.cts +117 -61
  20. package/dist/index.d.mts +117 -61
  21. package/dist/index.d.ts +117 -61
  22. package/dist/index.mjs +888 -278
  23. package/dist/plugins-aws.cjs +618 -0
  24. package/dist/plugins-aws.d.cts +178 -0
  25. package/dist/plugins-aws.d.mts +178 -0
  26. package/dist/plugins-aws.d.ts +178 -0
  27. package/dist/plugins-aws.mjs +616 -0
  28. package/dist/plugins-batch.cjs +569 -0
  29. package/dist/plugins-batch.d.cts +200 -0
  30. package/dist/plugins-batch.d.mts +200 -0
  31. package/dist/plugins-batch.d.ts +200 -0
  32. package/dist/plugins-batch.mjs +567 -0
  33. package/dist/plugins-init.cjs +282 -0
  34. package/dist/plugins-init.d.cts +182 -0
  35. package/dist/plugins-init.d.mts +182 -0
  36. package/dist/plugins-init.d.ts +182 -0
  37. package/dist/plugins-init.mjs +280 -0
  38. package/getdotenv.config.json +19 -0
  39. package/package.json +88 -17
  40. package/templates/cli/ts/index.ts +9 -0
  41. package/templates/cli/ts/plugins/hello.ts +17 -0
  42. package/templates/config/js/getdotenv.config.js +15 -0
  43. package/templates/config/json/local/getdotenv.config.local.json +7 -0
  44. package/templates/config/json/public/getdotenv.config.json +12 -0
  45. package/templates/config/public/getdotenv.config.json +13 -0
  46. package/templates/config/ts/getdotenv.config.ts +16 -0
  47. package/templates/config/yaml/local/getdotenv.config.local.yaml +7 -0
  48. package/templates/config/yaml/public/getdotenv.config.yaml +10 -0
package/dist/index.mjs CHANGED
@@ -1,13 +1,15 @@
1
- import { Command, Option } from 'commander';
2
- import { execaCommand } from 'execa';
1
+ import { Option, Command } from 'commander';
3
2
  import { globby } from 'globby';
4
3
  import { packageDirectory } from 'package-directory';
5
- import path, { join } from 'path';
4
+ import path, { join, extname } from 'path';
5
+ import { execa, execaCommand } from 'execa';
6
6
  import fs from 'fs-extra';
7
- import url, { fileURLToPath } from 'url';
8
- import { createHash } from 'crypto';
7
+ import url, { fileURLToPath, pathToFileURL } from 'url';
9
8
  import { nanoid } from 'nanoid';
10
9
  import { parse } from 'dotenv';
10
+ import { createHash } from 'crypto';
11
+ import YAML from 'yaml';
12
+ import { z } from 'zod';
11
13
 
12
14
  /**
13
15
  * Dotenv expansion utilities.
@@ -145,25 +147,90 @@ const dotenvExpandAll = (values = {}, options = {}) => Object.keys(values).reduc
145
147
  */
146
148
  const dotenvExpandFromProcessEnv = (value) => dotenvExpand(value, process.env);
147
149
 
148
- const baseGetDotenvCliOptions = {
149
- dotenvToken: '.env',
150
- loadProcess: true,
151
- logger: console,
152
- paths: './',
153
- pathsDelimiter: ' ',
154
- privateToken: 'local',
155
- scripts: {
156
- 'git-status': {
157
- cmd: 'git branch --show-current && git status -s -u',
158
- shell: true,
159
- },
160
- },
161
- shell: true,
162
- vars: '',
163
- varsAssignor: '=',
164
- varsDelimiter: ' ',
150
+ /**
151
+ * Attach legacy root flags to a Commander program.
152
+ * Uses provided defaults to render help labels without coupling to generators.
153
+ */
154
+ const attachRootOptions = (program, defaults, opts) => {
155
+ const { defaultEnv, dotenvToken, dynamicPath, env, excludeDynamic, excludeEnv, excludeGlobal, excludePrivate, excludePublic, loadProcess, log, outputPath, paths, pathsDelimiter, pathsDelimiterPattern, privateToken, scripts, shell, varsAssignor, varsAssignorPattern, varsDelimiter, varsDelimiterPattern, } = defaults ?? {};
156
+ const va = typeof defaults?.varsAssignor === 'string' ? defaults.varsAssignor : '=';
157
+ const vd = typeof defaults?.varsDelimiter === 'string' ? defaults.varsDelimiter : ' ';
158
+ // Build initial chain.
159
+ let p = program
160
+ .enablePositionalOptions()
161
+ .passThroughOptions()
162
+ .option('-e, --env <string>', `target environment (dotenv-expanded)`, dotenvExpandFromProcessEnv, env);
163
+ p = p.option('-v, --vars <string>', `extra variables expressed as delimited key-value pairs (dotenv-expanded): ${[
164
+ ['KEY1', 'VAL1'],
165
+ ['KEY2', 'VAL2'],
166
+ ]
167
+ .map((v) => v.join(va))
168
+ .join(vd)}`, dotenvExpandFromProcessEnv);
169
+ // Optional legacy root command flag (kept for generated CLI compatibility).
170
+ // Default is OFF; the generator opts in explicitly.
171
+ {
172
+ p = p.option('-c, --command <string>', 'command executed according to the --shell option, conflicts with cmd subcommand (dotenv-expanded)', dotenvExpandFromProcessEnv);
173
+ }
174
+ p = p
175
+ .option('-o, --output-path <string>', 'consolidated output file (dotenv-expanded)', dotenvExpandFromProcessEnv, outputPath)
176
+ .addOption(new Option('-s, --shell [string]', (() => {
177
+ let defaultLabel = '';
178
+ if (shell !== undefined) {
179
+ if (typeof shell === 'boolean') {
180
+ defaultLabel = ' (default OS shell)';
181
+ }
182
+ else if (typeof shell === 'string') {
183
+ // Safe string interpolation
184
+ defaultLabel = ` (default ${shell})`;
185
+ }
186
+ }
187
+ return `command execution shell, no argument for default OS shell or provide shell string${defaultLabel}`;
188
+ })()).conflicts('shellOff'))
189
+ .addOption(new Option('-S, --shell-off', `command execution shell OFF${!shell ? ' (default)' : ''}`).conflicts('shell'))
190
+ .addOption(new Option('-p, --load-process', `load variables to process.env ON${loadProcess ? ' (default)' : ''}`).conflicts('loadProcessOff'))
191
+ .addOption(new Option('-P, --load-process-off', `load variables to process.env OFF${!loadProcess ? ' (default)' : ''}`).conflicts('loadProcess'))
192
+ .addOption(new Option('-a, --exclude-all', `exclude all dotenv variables from loading ON${excludeDynamic &&
193
+ ((excludeEnv && excludeGlobal) || (excludePrivate && excludePublic))
194
+ ? ' (default)'
195
+ : ''}`).conflicts('excludeAllOff'))
196
+ .addOption(new Option('-A, --exclude-all-off', `exclude all dotenv variables from loading OFF (default)`).conflicts('excludeAll'))
197
+ .addOption(new Option('-z, --exclude-dynamic', `exclude dynamic dotenv variables from loading ON${excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamicOff'))
198
+ .addOption(new Option('-Z, --exclude-dynamic-off', `exclude dynamic dotenv variables from loading OFF${!excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamic'))
199
+ .addOption(new Option('-n, --exclude-env', `exclude environment-specific dotenv variables from loading${excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnvOff'))
200
+ .addOption(new Option('-N, --exclude-env-off', `exclude environment-specific dotenv variables from loading OFF${!excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnv'))
201
+ .addOption(new Option('-g, --exclude-global', `exclude global dotenv variables from loading ON${excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobalOff'))
202
+ .addOption(new Option('-G, --exclude-global-off', `exclude global dotenv variables from loading OFF${!excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobal'))
203
+ .addOption(new Option('-r, --exclude-private', `exclude private dotenv variables from loading ON${excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivateOff'))
204
+ .addOption(new Option('-R, --exclude-private-off', `exclude private dotenv variables from loading OFF${!excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivate'))
205
+ .addOption(new Option('-u, --exclude-public', `exclude public dotenv variables from loading ON${excludePublic ? ' (default)' : ''}`).conflicts('excludePublicOff'))
206
+ .addOption(new Option('-U, --exclude-public-off', `exclude public dotenv variables from loading OFF${!excludePublic ? ' (default)' : ''}`).conflicts('excludePublic'))
207
+ .addOption(new Option('-l, --log', `console log loaded variables ON${log ? ' (default)' : ''}`).conflicts('logOff'))
208
+ .addOption(new Option('-L, --log-off', `console log loaded variables OFF${!log ? ' (default)' : ''}`).conflicts('log'))
209
+ .option('--capture', 'capture child process stdio for commands (tests/CI)')
210
+ .option('--default-env <string>', 'default target environment', dotenvExpandFromProcessEnv, defaultEnv)
211
+ .option('--dotenv-token <string>', 'dotenv-expanded token indicating a dotenv file', dotenvExpandFromProcessEnv, dotenvToken)
212
+ .option('--dynamic-path <string>', 'dynamic variables path (.js or .ts; .ts is auto-compiled when esbuild is available, otherwise precompile)', dotenvExpandFromProcessEnv, dynamicPath)
213
+ .option('--paths <string>', 'dotenv-expanded delimited list of paths to dotenv directory', dotenvExpandFromProcessEnv, paths)
214
+ .option('--paths-delimiter <string>', 'paths delimiter string', pathsDelimiter)
215
+ .option('--paths-delimiter-pattern <string>', 'paths delimiter regex pattern', pathsDelimiterPattern)
216
+ .option('--private-token <string>', 'dotenv-expanded token indicating private variables', dotenvExpandFromProcessEnv, privateToken)
217
+ .option('--vars-delimiter <string>', 'vars delimiter string', varsDelimiter)
218
+ .option('--vars-delimiter-pattern <string>', 'vars delimiter regex pattern', varsDelimiterPattern)
219
+ .option('--vars-assignor <string>', 'vars assignment operator string', varsAssignor)
220
+ .option('--vars-assignor-pattern <string>', 'vars assignment operator regex pattern', varsAssignorPattern)
221
+ // Hidden scripts pipe-through (stringified)
222
+ .addOption(new Option('--scripts <string>')
223
+ .default(JSON.stringify(scripts))
224
+ .hideHelp());
225
+ // Diagnostics: opt-in tracing; optional variadic keys after the flag.
226
+ p = p.option('--trace [keys...]', 'emit diagnostics for child env composition (optional keys)');
227
+ return p;
165
228
  };
166
229
 
230
+ /**
231
+ * Batch services (neutral): resolve command and shell settings.
232
+ * Shared by the generator path and the batch plugin to avoid circular deps.
233
+ */
167
234
  /**
168
235
  * Resolve a command string from the {@link Scripts} table.
169
236
  * A script may be expressed as a string or an object with a `cmd` property.
@@ -188,6 +255,143 @@ const resolveShell = (scripts, command, shell) => scripts && typeof scripts[comm
188
255
  ? (scripts[command].shell ?? false)
189
256
  : (shell ?? false);
190
257
 
258
+ // Minimal tokenizer for shell-off execution:
259
+ // Splits by whitespace while preserving quoted segments (single or double quotes).
260
+ const tokenize = (command) => {
261
+ const out = [];
262
+ let cur = '';
263
+ let quote = null;
264
+ for (let i = 0; i < command.length; i++) {
265
+ const c = command.charAt(i);
266
+ if (quote) {
267
+ if (c === quote) {
268
+ // Support doubled quotes inside a quoted segment (Windows/PowerShell style):
269
+ // "" -> " and '' -> '
270
+ const next = command.charAt(i + 1);
271
+ if (next === quote) {
272
+ cur += quote;
273
+ i += 1; // skip the second quote
274
+ }
275
+ else {
276
+ // end of quoted segment
277
+ quote = null;
278
+ }
279
+ }
280
+ else {
281
+ cur += c;
282
+ }
283
+ }
284
+ else {
285
+ if (c === '"' || c === "'") {
286
+ quote = c;
287
+ }
288
+ else if (/\s/.test(c)) {
289
+ if (cur) {
290
+ out.push(cur);
291
+ cur = '';
292
+ }
293
+ }
294
+ else {
295
+ cur += c;
296
+ }
297
+ }
298
+ }
299
+ if (cur)
300
+ out.push(cur);
301
+ return out;
302
+ };
303
+
304
+ const dbg = (...args) => {
305
+ if (process.env.GETDOTENV_DEBUG) {
306
+ // Use stderr to avoid interfering with stdout assertions
307
+ console.error('[getdotenv:run]', ...args);
308
+ }
309
+ };
310
+ // Strip repeated symmetric outer quotes (single or double) until stable.
311
+ // This is safe for argv arrays passed to execa (no quoting needed) and avoids
312
+ // passing quote characters through to Node (e.g., for `node -e "<code>"`).
313
+ // Handles stacked quotes from shells like PowerShell: """code""" -> code.
314
+ const stripOuterQuotes = (s) => {
315
+ let out = s;
316
+ // Repeatedly trim only when the entire string is wrapped in matching quotes.
317
+ // Stop as soon as the ends are asymmetric or no quotes remain.
318
+ while (out.length >= 2) {
319
+ const a = out.charAt(0);
320
+ const b = out.charAt(out.length - 1);
321
+ const symmetric = (a === '"' && b === '"') || (a === "'" && b === "'");
322
+ if (!symmetric)
323
+ break;
324
+ out = out.slice(1, -1);
325
+ }
326
+ return out;
327
+ };
328
+ // Convert NodeJS.ProcessEnv (string | undefined values) to the shape execa
329
+ // expects (Readonly<Partial<Record<string, string>>>), dropping undefineds.
330
+ const sanitizeEnv = (env) => {
331
+ if (!env)
332
+ return undefined;
333
+ const entries = Object.entries(env).filter((e) => typeof e[1] === 'string');
334
+ return entries.length > 0 ? Object.fromEntries(entries) : undefined;
335
+ };
336
+ const runCommand = async (command, shell, opts) => {
337
+ if (shell === false) {
338
+ let file;
339
+ let args = [];
340
+ if (Array.isArray(command)) {
341
+ file = command[0];
342
+ args = command.slice(1).map(stripOuterQuotes);
343
+ }
344
+ else {
345
+ const tokens = tokenize(command);
346
+ file = tokens[0];
347
+ args = tokens.slice(1);
348
+ }
349
+ if (!file)
350
+ return 0;
351
+ dbg('exec (plain)', { file, args, stdio: opts.stdio });
352
+ // Build options without injecting undefined properties (exactOptionalPropertyTypes).
353
+ const envSan = sanitizeEnv(opts.env);
354
+ const plainOpts = {};
355
+ if (opts.cwd !== undefined)
356
+ plainOpts.cwd = opts.cwd;
357
+ if (envSan !== undefined)
358
+ plainOpts.env = envSan;
359
+ if (opts.stdio !== undefined)
360
+ plainOpts.stdio = opts.stdio;
361
+ const result = await execa(file, args, plainOpts);
362
+ if (opts.stdio === 'pipe' && result.stdout) {
363
+ process.stdout.write(result.stdout + (result.stdout.endsWith('\n') ? '' : '\n'));
364
+ }
365
+ const exit = result?.exitCode;
366
+ dbg('exit (plain)', { exitCode: exit });
367
+ return typeof exit === 'number' ? exit : Number.NaN;
368
+ }
369
+ else {
370
+ const commandStr = Array.isArray(command) ? command.join(' ') : command;
371
+ dbg('exec (shell)', {
372
+ shell: typeof shell === 'string' ? shell : 'custom',
373
+ stdio: opts.stdio,
374
+ command: commandStr,
375
+ });
376
+ const envSan = sanitizeEnv(opts.env);
377
+ const shellOpts = { shell };
378
+ if (opts.cwd !== undefined)
379
+ shellOpts.cwd = opts.cwd;
380
+ if (envSan !== undefined)
381
+ shellOpts.env = envSan;
382
+ if (opts.stdio !== undefined)
383
+ shellOpts.stdio = opts.stdio;
384
+ const result = await execaCommand(commandStr, shellOpts);
385
+ const out = result?.stdout;
386
+ if (opts.stdio === 'pipe' && out) {
387
+ process.stdout.write(out + (out.endsWith('\n') ? '' : '\n'));
388
+ }
389
+ const exit = result?.exitCode;
390
+ dbg('exit (shell)', { exitCode: exit });
391
+ return typeof exit === 'number' ? exit : Number.NaN;
392
+ }
393
+ };
394
+
191
395
  const globPaths = async ({ globs, logger, pkgCwd, rootPath, }) => {
192
396
  let cwd = process.cwd();
193
397
  if (pkgCwd) {
@@ -212,8 +416,10 @@ const globPaths = async ({ globs, logger, pkgCwd, rootPath, }) => {
212
416
  return { absRootPath, paths };
213
417
  };
214
418
  const execShellCommandBatch = async ({ command, getDotenvCliOptions, globs, ignoreErrors, list, logger, pkgCwd, rootPath, shell, }) => {
215
- if (!command) {
216
- logger.error(`No command provided.`);
419
+ const capture = process.env.GETDOTENV_STDIO === 'pipe' ||
420
+ Boolean(getDotenvCliOptions?.capture); // Require a command only when not listing. In list mode, a command is optional.
421
+ if (!command && !list) {
422
+ logger.error(`No command provided. Use --command or --list.`);
217
423
  process.exit(0);
218
424
  }
219
425
  const { absRootPath, paths } = await globPaths({
@@ -229,7 +435,13 @@ const execShellCommandBatch = async ({ command, getDotenvCliOptions, globs, igno
229
435
  logger.info('');
230
436
  const headerRootPath = `ROOT: ${absRootPath}`;
231
437
  const headerGlobs = `GLOBS: ${globs}`;
232
- const headerCommand = `CMD: ${command}`;
438
+ // Prepare a safe label for the header (avoid undefined in template)
439
+ const commandLabel = Array.isArray(command)
440
+ ? command.join(' ')
441
+ : typeof command === 'string' && command.length > 0
442
+ ? command
443
+ : '';
444
+ const headerCommand = list ? `CMD: (list only)` : `CMD: ${commandLabel}`;
233
445
  logger.info('*'.repeat(Math.max(headerTitle.length, headerRootPath.length, headerGlobs.length, headerCommand.length)));
234
446
  logger.info(headerTitle);
235
447
  logger.info('');
@@ -249,17 +461,25 @@ const execShellCommandBatch = async ({ command, getDotenvCliOptions, globs, igno
249
461
  logger.info(headerCommand);
250
462
  // Execute command.
251
463
  try {
252
- await execaCommand(command, {
253
- cwd: path,
254
- env: {
255
- ...process.env,
256
- getDotenvCliOptions: getDotenvCliOptions
257
- ? JSON.stringify(getDotenvCliOptions)
258
- : undefined,
259
- },
260
- stdio: 'inherit',
261
- shell, // already normalized to string | boolean | URL
262
- });
464
+ const hasCmd = (typeof command === 'string' && command.length > 0) ||
465
+ (Array.isArray(command) && command.length > 0);
466
+ if (hasCmd) {
467
+ await runCommand(command, shell, {
468
+ cwd: path,
469
+ env: {
470
+ ...process.env,
471
+ getDotenvCliOptions: getDotenvCliOptions
472
+ ? JSON.stringify(getDotenvCliOptions)
473
+ : undefined,
474
+ },
475
+ stdio: capture ? 'pipe' : 'inherit',
476
+ });
477
+ }
478
+ else {
479
+ // Should not occur due to the early guard; retain for type safety.
480
+ logger.error(`No command provided. Use --command or --list.`);
481
+ process.exit(0);
482
+ }
263
483
  }
264
484
  catch (error) {
265
485
  if (!ignoreErrors) {
@@ -379,72 +599,43 @@ const cmdCommand = new Command()
379
599
  });
380
600
 
381
601
  /**
382
- * Create the root Commander command with all options and subcommands.
383
- * Pure builder: no side-effects; the caller attaches lifecycle hooks.
602
+ * Create the root Commander command with legacy root options (via cliCore)
603
+ * and built-in subcommands. Pure builder: no side-effects; the caller attaches
604
+ * lifecycle hooks separately.
384
605
  */
385
606
  const createRootCommand = (opts) => {
386
- const { alias, debug, defaultEnv, description, dotenvToken, dynamicPath, env, excludeDynamic, excludeEnv, excludeGlobal, excludePrivate, excludePublic, loadProcess, log, outputPath, paths, pathsDelimiter, pathsDelimiterPattern, privateToken, scripts, shell, varsAssignor, varsAssignorPattern, varsDelimiter, varsDelimiterPattern, } = opts;
387
- const excludeAll = !!excludeDynamic &&
388
- ((!!excludeEnv && !!excludeGlobal) ||
389
- (!!excludePrivate && !!excludePublic));
390
- const program = new Command()
391
- .name(alias)
392
- .description(description)
393
- .enablePositionalOptions()
394
- .passThroughOptions()
395
- .option('-e, --env <string>', `target environment (dotenv-expanded)`, dotenvExpandFromProcessEnv, env)
396
- .option('-v, --vars <string>', `extra variables expressed as delimited key-value pairs (dotenv-expanded): ${[
397
- ['KEY1', 'VAL1'],
398
- ['KEY2', 'VAL2'],
399
- ]
400
- .map((v) => v.join(varsAssignor ?? '='))
401
- .join(varsDelimiter ?? ' ')}`, dotenvExpandFromProcessEnv)
402
- .option('-c, --command <string>', 'command executed according to the --shell option, conflicts with cmd subcommand (dotenv-expanded)', dotenvExpandFromProcessEnv)
403
- .option('-o, --output-path <string>', 'consolidated output file (dotenv-expanded)', dotenvExpandFromProcessEnv, outputPath)
404
- .addOption(new Option('-s, --shell [string]', (() => {
405
- const defaultLabel = shell
406
- ? ` (default ${typeof shell === 'boolean' ? 'OS shell' : shell})`
407
- : '';
408
- return `command execution shell, no argument for default OS shell or provide shell string${defaultLabel}`;
409
- })()).conflicts('shellOff'))
410
- .addOption(new Option('-S, --shell-off', `command execution shell OFF${!shell ? ' (default)' : ''}`).conflicts('shell'))
411
- .addOption(new Option('-p, --load-process', `load variables to process.env ON${loadProcess ? ' (default)' : ''}`).conflicts('loadProcessOff'))
412
- .addOption(new Option('-P, --load-process-off', `load variables to process.env OFF${!loadProcess ? ' (default)' : ''}`).conflicts('loadProcess'))
413
- .addOption(new Option('-a, --exclude-all', `exclude all dotenv variables from loading ON${excludeAll ? ' (default)' : ''}`).conflicts('excludeAllOff'))
414
- .addOption(new Option('-A, --exclude-all-off', `exclude all dotenv variables from loading OFF${!excludeAll ? ' (default)' : ''}`).conflicts('excludeAll'))
415
- .addOption(new Option('-z, --exclude-dynamic', `exclude dynamic dotenv variables from loading ON${excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamicOff'))
416
- .addOption(new Option('-Z, --exclude-dynamic-off', `exclude dynamic dotenv variables from loading OFF${!excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamic'))
417
- .addOption(new Option('-n, --exclude-env', `exclude environment-specific dotenv variables from loading${excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnvOff'))
418
- .addOption(new Option('-N, --exclude-env-off', `exclude environment-specific dotenv variables from loading OFF${!excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnv'))
419
- .addOption(new Option('-g, --exclude-global', `exclude global dotenv variables from loading ON${excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobalOff'))
420
- .addOption(new Option('-G, --exclude-global-off', `exclude global dotenv variables from loading OFF${!excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobal'))
421
- .addOption(new Option('-r, --exclude-private', `exclude private dotenv variables from loading ON${excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivateOff'))
422
- .addOption(new Option('-R, --exclude-private-off', `exclude private dotenv variables from loading OFF${!excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivate'))
423
- .addOption(new Option('-u, --exclude-public', `exclude public dotenv variables from loading ON${excludePublic ? ' (default)' : ''}`).conflicts('excludePublicOff'))
424
- .addOption(new Option('-U, --exclude-public-off', `exclude public dotenv variables from loading OFF${!excludePublic ? ' (default)' : ''}`).conflicts('excludePublic'))
425
- .addOption(new Option('-l, --log', `console log loaded variables ON${log ? ' (default)' : ''}`).conflicts('logOff'))
426
- .addOption(new Option('-L, --log-off', `console log loaded variables OFF${!log ? ' (default)' : ''}`).conflicts('log'))
427
- .addOption(new Option('-d, --debug', `debug mode ON${debug ? ' (default)' : ''}`).conflicts('debugOff'))
428
- .addOption(new Option('-D, --debug-off', `debug mode OFF${!debug ? ' (default)' : ''}`).conflicts('debug'))
429
- .option('--default-env <string>', 'default target environment', dotenvExpandFromProcessEnv, defaultEnv)
430
- .option('--dotenv-token <string>', 'dotenv-expanded token indicating a dotenv file', dotenvExpandFromProcessEnv, dotenvToken)
431
- .option('--dynamic-path <string>', 'dynamic variables path (.js or .ts; .ts is auto-compiled when esbuild is available, otherwise precompile)', dotenvExpandFromProcessEnv, dynamicPath)
432
- .option('--paths <string>', 'dotenv-expanded delimited list of paths to dotenv directory', dotenvExpandFromProcessEnv, paths)
433
- .option('--paths-delimiter <string>', 'paths delimiter string', pathsDelimiter)
434
- .option('--paths-delimiter-pattern <string>', 'paths delimiter regex pattern', pathsDelimiterPattern)
435
- .option('--private-token <string>', 'dotenv-expanded token indicating private variables', dotenvExpandFromProcessEnv, privateToken)
436
- .option('--vars-delimiter <string>', 'vars delimiter string', varsDelimiter)
437
- .option('--vars-delimiter-pattern <string>', 'vars delimiter regex pattern', varsDelimiterPattern)
438
- .option('--vars-assignor <string>', 'vars assignment operator string', varsAssignor)
439
- .option('--vars-assignor-pattern <string>', 'vars assignment operator regex pattern', varsAssignorPattern)
440
- .addOption(new Option('--scripts <string>')
441
- .default(JSON.stringify(scripts))
442
- .hideHelp())
443
- .addCommand(batchCommand)
444
- .addCommand(cmdCommand, { isDefault: true });
607
+ const program = new Command().name(opts.alias).description(opts.description);
608
+ // Attach legacy root flags using shared cliCore builder to keep parity.
609
+ attachRootOptions(program, opts);
610
+ // Subcommands
611
+ program.addCommand(batchCommand).addCommand(cmdCommand, { isDefault: true });
445
612
  return program;
446
613
  };
447
614
 
615
+ // Base root CLI defaults (shared; kept untyped here to avoid cross-layer deps).
616
+ const baseRootOptionDefaults = {
617
+ dotenvToken: '.env',
618
+ loadProcess: true,
619
+ logger: console,
620
+ paths: './',
621
+ pathsDelimiter: ' ',
622
+ privateToken: 'local',
623
+ scripts: {
624
+ 'git-status': {
625
+ cmd: 'git branch --show-current && git status -s -u',
626
+ shell: true,
627
+ },
628
+ },
629
+ shell: true,
630
+ vars: '',
631
+ varsAssignor: '=',
632
+ varsDelimiter: ' ',
633
+ // tri-state flags default to unset unless explicitly provided
634
+ // (debug/log/exclude* resolved via flag utils)
635
+ };
636
+
637
+ const baseGetDotenvCliOptions = baseRootOptionDefaults;
638
+
448
639
  /** @internal */
449
640
  const isPlainObject = (value) => value !== null &&
450
641
  typeof value === 'object' &&
@@ -488,7 +679,10 @@ const defaultsDeep = (...layers) => {
488
679
  };
489
680
 
490
681
  // src/GetDotenvOptions.ts
491
- const getDotenvOptionsFilename = 'getdotenv.config.json';
682
+ const getDotenvOptionsFilename = 'getdotenv.config.json'; /**
683
+ * A minimal representation of an environment key/value mapping.
684
+ * Values may be `undefined` to represent "unset".
685
+ */
492
686
  /**
493
687
  * Helper to define a dynamic map with strong inference.
494
688
  *
@@ -497,8 +691,7 @@ const getDotenvOptionsFilename = 'getdotenv.config.json';
497
691
  */
498
692
  const defineDynamic = (d) => d;
499
693
  /**
500
- * Converts programmatic CLI options to `getDotenv` options.
501
- *
694
+ * Converts programmatic CLI options to `getDotenv` options. *
502
695
  * @param cliOptions - CLI options. Defaults to `{}`.
503
696
  *
504
697
  * @returns `getDotenv` options.
@@ -508,16 +701,18 @@ const getDotenvCliOptions2Options = ({ paths, pathsDelimiter, pathsDelimiterPatt
508
701
  * Convert CLI-facing string options into {@link GetDotenvOptions}.
509
702
  *
510
703
  * - Splits {@link GetDotenvCliOptions.paths} using either a delimiter
511
- * or a regular expression pattern into a string array.
512
- * - Parses {@link GetDotenvCliOptions.vars} as space-separated `KEY=VALUE`
704
+ * or a regular expression pattern into a string array. * - Parses {@link GetDotenvCliOptions.vars} as space-separated `KEY=VALUE`
513
705
  * pairs (configurable delimiters) into a {@link ProcessEnv}.
514
706
  * - Drops CLI-only keys that have no programmatic equivalent.
515
707
  *
516
708
  * @remarks
517
709
  * Follows exact-optional semantics by not emitting undefined-valued entries.
518
710
  */
519
- // Drop CLI-only keys
520
- const { debug, scripts, ...restFlags } = rest;
711
+ // Drop CLI-only keys (debug/scripts) without relying on Record casts.
712
+ // Create a shallow copy then delete optional CLI-only keys if present.
713
+ const restObj = { ...rest };
714
+ delete restObj.debug;
715
+ delete restObj.scripts;
521
716
  const splitBy = (value, delim, pattern) => (value ? value.split(pattern ? RegExp(pattern) : (delim ?? ' ')) : []);
522
717
  const kvPairs = (vars
523
718
  ? splitBy(vars, varsDelimiter, varsDelimiterPattern).map((v) => v.split(varsAssignorPattern
@@ -525,10 +720,15 @@ const getDotenvCliOptions2Options = ({ paths, pathsDelimiter, pathsDelimiterPatt
525
720
  : (varsAssignor ?? '=')))
526
721
  : []);
527
722
  const parsedVars = Object.fromEntries(kvPairs);
723
+ // Preserve exactOptionalPropertyTypes: only include keys when defined.
528
724
  return {
529
- ...restFlags,
530
- paths: splitBy(paths, pathsDelimiter, pathsDelimiterPattern),
531
- vars: parsedVars,
725
+ ...restObj,
726
+ ...(paths !== undefined
727
+ ? {
728
+ paths: splitBy(paths, pathsDelimiter, pathsDelimiterPattern),
729
+ }
730
+ : {}),
731
+ ...(vars !== undefined ? { vars: parsedVars } : {}),
532
732
  };
533
733
  };
534
734
  const resolveGetDotenvOptions = async (customOptions) => {
@@ -601,6 +801,169 @@ const resolveGetDotenvCliGenerateOptions = async ({ importMetaUrl, ...customOpti
601
801
  return merged;
602
802
  };
603
803
 
804
+ /**
805
+ * Resolve a tri-state optional boolean flag under exactOptionalPropertyTypes.
806
+ * - If the user explicitly enabled the flag, return true.
807
+ * - If the user explicitly disabled (the "...-off" variant), return undefined (unset).
808
+ * - Otherwise, adopt the default (true → set; false/undefined → unset).
809
+ *
810
+ * @param exclude - The "on" flag value as parsed by Commander.
811
+ * @param excludeOff - The "off" toggle (present when specified) as parsed by Commander.
812
+ * @param defaultValue - The generator default to adopt when no explicit toggle is present.
813
+ * @returns boolean | undefined — use `undefined` to indicate "unset" (do not emit).
814
+ *
815
+ * @example
816
+ * ```ts
817
+ * resolveExclusion(undefined, undefined, true); // => true
818
+ * ```
819
+ */
820
+ const resolveExclusion = (exclude, excludeOff, defaultValue) => exclude ? true : excludeOff ? undefined : defaultValue ? true : undefined;
821
+ /**
822
+ * Resolve an optional flag with "--exclude-all" overrides.
823
+ * If excludeAll is set and the individual "...-off" is not, force true.
824
+ * If excludeAllOff is set and the individual flag is not explicitly set, unset.
825
+ * Otherwise, adopt the default (true → set; false/undefined → unset).
826
+ *
827
+ * @param exclude - Individual include/exclude flag.
828
+ * @param excludeOff - Individual "...-off" flag.
829
+ * @param defaultValue - Default for the individual flag.
830
+ * @param excludeAll - Global "exclude-all" flag.
831
+ * @param excludeAllOff - Global "exclude-all-off" flag.
832
+ *
833
+ * @example
834
+ * resolveExclusionAll(undefined, undefined, false, true, undefined) =\> true
835
+ */
836
+ const resolveExclusionAll = (exclude, excludeOff, defaultValue, excludeAll, excludeAllOff) =>
837
+ // Order of precedence:
838
+ // 1) Individual explicit "on" wins outright.
839
+ // 2) Individual explicit "off" wins over any global.
840
+ // 3) Global exclude-all forces true when not explicitly turned off.
841
+ // 4) Global exclude-all-off unsets when the individual wasn't explicitly enabled.
842
+ // 5) Fall back to the default (true => set; false/undefined => unset).
843
+ (() => {
844
+ // Individual "on"
845
+ if (exclude === true)
846
+ return true;
847
+ // Individual "off"
848
+ if (excludeOff === true)
849
+ return undefined;
850
+ // Global "exclude-all" ON (unless explicitly turned off)
851
+ if (excludeAll === true)
852
+ return true;
853
+ // Global "exclude-all-off" (unless explicitly enabled)
854
+ if (excludeAllOff === true)
855
+ return undefined;
856
+ // Default
857
+ return defaultValue ? true : undefined;
858
+ })();
859
+ /**
860
+ * exactOptionalPropertyTypes-safe setter for optional boolean flags:
861
+ * delete when undefined; assign when defined — without requiring an index signature on T.
862
+ *
863
+ * @typeParam T - Target object type.
864
+ * @param obj - The object to write to.
865
+ * @param key - The optional boolean property key of {@link T}.
866
+ * @param value - The value to set or `undefined` to unset.
867
+ *
868
+ * @remarks
869
+ * Writes through a local `Record<string, unknown>` view to avoid requiring an index signature on {@link T}.
870
+ */
871
+ const setOptionalFlag = (obj, key, value) => {
872
+ const target = obj;
873
+ const k = key;
874
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
875
+ if (value === undefined)
876
+ delete target[k];
877
+ else
878
+ target[k] = value;
879
+ };
880
+
881
+ /**
882
+ * Merge and normalize raw Commander options (current + parent + defaults)
883
+ * into a GetDotenvCliOptions-like object. Types are intentionally wide to
884
+ * avoid cross-layer coupling; callers may cast as needed.
885
+ */
886
+ const resolveCliOptions = (rawCliOptions, defaults, parentJson) => {
887
+ const parent = typeof parentJson === 'string' && parentJson.length > 0
888
+ ? JSON.parse(parentJson)
889
+ : undefined;
890
+ const { command, debugOff, excludeAll, excludeAllOff, excludeDynamicOff, excludeEnvOff, excludeGlobalOff, excludePrivateOff, excludePublicOff, loadProcessOff, logOff, scripts, shellOff, ...rest } = rawCliOptions;
891
+ const current = { ...rest };
892
+ if (typeof scripts === 'string') {
893
+ try {
894
+ current.scripts = JSON.parse(scripts);
895
+ }
896
+ catch {
897
+ // ignore parse errors; leave scripts undefined
898
+ }
899
+ }
900
+ const merged = defaultsDeep({}, defaults, parent ?? {}, current);
901
+ const d = defaults;
902
+ setOptionalFlag(merged, 'debug', resolveExclusion(merged.debug, debugOff, d.debug));
903
+ setOptionalFlag(merged, 'excludeDynamic', resolveExclusionAll(merged.excludeDynamic, excludeDynamicOff, d.excludeDynamic, excludeAll, excludeAllOff));
904
+ setOptionalFlag(merged, 'excludeEnv', resolveExclusionAll(merged.excludeEnv, excludeEnvOff, d.excludeEnv, excludeAll, excludeAllOff));
905
+ setOptionalFlag(merged, 'excludeGlobal', resolveExclusionAll(merged.excludeGlobal, excludeGlobalOff, d.excludeGlobal, excludeAll, excludeAllOff));
906
+ setOptionalFlag(merged, 'excludePrivate', resolveExclusionAll(merged.excludePrivate, excludePrivateOff, d.excludePrivate, excludeAll, excludeAllOff));
907
+ setOptionalFlag(merged, 'excludePublic', resolveExclusionAll(merged.excludePublic, excludePublicOff, d.excludePublic, excludeAll, excludeAllOff));
908
+ setOptionalFlag(merged, 'log', resolveExclusion(merged.log, logOff, d.log));
909
+ setOptionalFlag(merged, 'loadProcess', resolveExclusion(merged.loadProcess, loadProcessOff, d.loadProcess));
910
+ // Normalize shell for predictability: explicit default shell per OS.
911
+ const defaultShell = process.platform === 'win32' ? 'powershell.exe' : '/bin/bash';
912
+ let resolvedShell = merged.shell;
913
+ if (shellOff)
914
+ resolvedShell = false;
915
+ else if (resolvedShell === true || resolvedShell === undefined) {
916
+ resolvedShell = defaultShell;
917
+ }
918
+ else if (typeof resolvedShell !== 'string' &&
919
+ typeof defaults.shell === 'string') {
920
+ resolvedShell = defaults.shell;
921
+ }
922
+ merged.shell = resolvedShell;
923
+ const cmd = typeof command === 'string' ? command : undefined;
924
+ return cmd !== undefined ? { merged, command: cmd } : { merged };
925
+ };
926
+
927
+ const applyKv = (current, kv) => {
928
+ if (!kv || Object.keys(kv).length === 0)
929
+ return current;
930
+ const expanded = dotenvExpandAll(kv, { ref: current, progressive: true });
931
+ return { ...current, ...expanded };
932
+ };
933
+ const applyConfigSlice = (current, cfg, env) => {
934
+ if (!cfg)
935
+ return current;
936
+ // kind axis: global then env (env overrides global)
937
+ const afterGlobal = applyKv(current, cfg.vars);
938
+ const envKv = env && cfg.envVars ? cfg.envVars[env] : undefined;
939
+ return applyKv(afterGlobal, envKv);
940
+ };
941
+ /**
942
+ * Overlay config-provided values onto a base ProcessEnv using precedence axes:
943
+ * - kind: env \> global
944
+ * - privacy: local \> public
945
+ * - source: project \> packaged \> base
946
+ *
947
+ * Programmatic explicit vars (if provided) override all config slices.
948
+ * Progressive expansion is applied within each slice.
949
+ */
950
+ const overlayEnv = ({ base, env, configs, programmaticVars, }) => {
951
+ let current = { ...base };
952
+ // Source: packaged (public -> local)
953
+ current = applyConfigSlice(current, configs.packaged, env);
954
+ // Packaged "local" is not expected by policy; if present, honor it.
955
+ // We do not have a separate object for packaged.local in sources, keep as-is.
956
+ // Source: project (public -> local)
957
+ current = applyConfigSlice(current, configs.project?.public, env);
958
+ current = applyConfigSlice(current, configs.project?.local, env);
959
+ // Programmatic explicit vars (top of static tier)
960
+ if (programmaticVars) {
961
+ const toApply = Object.fromEntries(Object.entries(programmaticVars).filter(([_k, v]) => typeof v === 'string'));
962
+ current = applyKv(current, toApply);
963
+ }
964
+ return current;
965
+ };
966
+
604
967
  /**
605
968
  * Asynchronously read a dotenv file & parse it into an object.
606
969
  *
@@ -616,65 +979,95 @@ const readDotenv = async (path) => {
616
979
  }
617
980
  };
618
981
 
619
- const importDefault = async (fileUrl) => {
982
+ const importDefault$1 = async (fileUrl) => {
620
983
  const mod = (await import(fileUrl));
621
984
  return mod.default;
622
985
  };
623
- /**
624
- * @internal Compute a short hash from path + mtime for cache filenames.
625
- */
626
986
  const cacheHash = (absPath, mtimeMs) => createHash('sha1')
627
987
  .update(absPath)
628
988
  .update(String(mtimeMs))
629
989
  .digest('hex')
630
990
  .slice(0, 12);
631
991
  /**
632
- * @internal Load a dynamic module from path. Supports .js/.mjs/.ts/.tsx:
633
- * - .js/.mjs: direct import
634
- * - .ts/.tsx: try direct import (in case a TS loader is active), otherwise:
635
- * - esbuild (if present): bundle to a temp ESM file and import it
636
- * - fallback: typescript.transpileModule (single-file), then import temp file
992
+ * Remove older compiled cache files for a given source base name, keeping
993
+ * at most `keep` most-recent files. Errors are ignored by design.
637
994
  */
638
- const loadDynamicFromPath = async (absPath) => {
639
- if (!(await fs.exists(absPath)))
640
- return undefined;
995
+ const cleanupOldCacheFiles = async (cacheDir, baseName, keep = Math.max(1, Number.parseInt(process.env.GETDOTENV_CACHE_KEEP ?? '2'))) => {
996
+ try {
997
+ const entries = await fs.readdir(cacheDir);
998
+ const mine = entries
999
+ .filter((f) => f.startsWith(`${baseName}.`) && f.endsWith('.mjs'))
1000
+ .map((f) => path.join(cacheDir, f));
1001
+ if (mine.length <= keep)
1002
+ return;
1003
+ const stats = await Promise.all(mine.map(async (p) => ({ p, mtimeMs: (await fs.stat(p)).mtimeMs })));
1004
+ stats.sort((a, b) => b.mtimeMs - a.mtimeMs);
1005
+ const toDelete = stats.slice(keep).map((s) => s.p);
1006
+ await Promise.all(toDelete.map(async (p) => {
1007
+ try {
1008
+ await fs.remove(p);
1009
+ }
1010
+ catch {
1011
+ // best-effort cleanup
1012
+ }
1013
+ }));
1014
+ }
1015
+ catch {
1016
+ // best-effort cleanup
1017
+ }
1018
+ };
1019
+ /**
1020
+ * Load a module default export from a JS/TS file with robust fallbacks:
1021
+ * - .js/.mjs/.cjs: direct import * - .ts/.mts/.cts/.tsx:
1022
+ * 1) try direct import (if a TS loader is active),
1023
+ * 2) esbuild bundle to a temp ESM file,
1024
+ * 3) typescript.transpileModule fallback for simple modules.
1025
+ *
1026
+ * @param absPath - absolute path to source file
1027
+ * @param cacheDirName - cache subfolder under .tsbuild
1028
+ */
1029
+ const loadModuleDefault = async (absPath, cacheDirName) => {
641
1030
  const ext = path.extname(absPath).toLowerCase();
642
1031
  const fileUrl = url.pathToFileURL(absPath).toString();
643
- if (ext !== '.ts' && ext !== '.tsx') {
644
- return importDefault(fileUrl);
1032
+ if (!['.ts', '.mts', '.cts', '.tsx'].includes(ext)) {
1033
+ return importDefault$1(fileUrl);
645
1034
  }
646
- // Try direct import (in case user started Node with a TS loader).
1035
+ // Try direct import first (TS loader active)
647
1036
  try {
648
- const dyn = await importDefault(fileUrl);
1037
+ const dyn = await importDefault$1(fileUrl);
649
1038
  if (dyn)
650
1039
  return dyn;
651
1040
  }
652
1041
  catch {
653
- // ignore; fall through to compile
1042
+ /* fall through */
654
1043
  }
655
1044
  const stat = await fs.stat(absPath);
656
1045
  const hash = cacheHash(absPath, stat.mtimeMs);
657
- const cacheDir = path.resolve('.tsbuild', 'getdotenv-dynamic');
1046
+ const cacheDir = path.resolve('.tsbuild', cacheDirName);
1047
+ await fs.ensureDir(cacheDir);
658
1048
  const cacheFile = path.join(cacheDir, `${path.basename(absPath)}.${hash}.mjs`);
659
- // Try esbuild first
1049
+ // Try esbuild
660
1050
  try {
661
1051
  const esbuild = (await import('esbuild'));
662
- await fs.ensureDir(cacheDir);
663
1052
  await esbuild.build({
664
1053
  entryPoints: [absPath],
665
1054
  bundle: true,
666
1055
  platform: 'node',
667
1056
  format: 'esm',
668
- target: 'node22',
1057
+ target: 'node20',
669
1058
  outfile: cacheFile,
670
1059
  sourcemap: false,
671
1060
  logLevel: 'silent',
672
1061
  });
673
- return await importDefault(url.pathToFileURL(cacheFile).toString());
1062
+ const result = await importDefault$1(url.pathToFileURL(cacheFile).toString());
1063
+ // Best-effort: trim older cache files for this source.
1064
+ await cleanupOldCacheFiles(cacheDir, path.basename(absPath));
1065
+ return result;
674
1066
  }
675
1067
  catch {
676
- // no esbuild; fall back to TS transpile for simple modules
1068
+ /* fall through to TS transpile */
677
1069
  }
1070
+ // TypeScript transpile fallback
678
1071
  try {
679
1072
  const ts = (await import('typescript'));
680
1073
  const code = await fs.readFile(absPath, 'utf-8');
@@ -684,16 +1077,19 @@ const loadDynamicFromPath = async (absPath) => {
684
1077
  target: 'ES2022',
685
1078
  moduleResolution: 'NodeNext',
686
1079
  },
687
- });
688
- await fs.ensureDir(cacheDir);
689
- await fs.writeFile(cacheFile, out.outputText, 'utf-8');
690
- return await importDefault(url.pathToFileURL(cacheFile).toString());
1080
+ }).outputText;
1081
+ await fs.writeFile(cacheFile, out, 'utf-8');
1082
+ const result = await importDefault$1(url.pathToFileURL(cacheFile).toString());
1083
+ // Best-effort: trim older cache files for this source.
1084
+ await cleanupOldCacheFiles(cacheDir, path.basename(absPath));
1085
+ return result;
691
1086
  }
692
1087
  catch {
693
- throw new Error(`Unable to load dynamic TypeScript file: ${absPath}. ` +
694
- `Install 'esbuild' (devDependency) to enable TypeScript dynamic modules.`);
1088
+ // Caller decides final error wording; rethrow for upstream mapping.
1089
+ throw new Error(`Unable to load JS/TS module: ${absPath}. Install 'esbuild' or ensure a TS loader.`);
695
1090
  }
696
1091
  };
1092
+
697
1093
  /**
698
1094
  * Asynchronously process dotenv files of the form `.env[.<ENV>][.<PRIVATE_TOKEN>]`
699
1095
  *
@@ -778,7 +1174,16 @@ const getDotenv = async (options = {}) => {
778
1174
  }
779
1175
  else if (dynamicPath) {
780
1176
  const absDynamicPath = path.resolve(dynamicPath);
781
- dynamic = await loadDynamicFromPath(absDynamicPath);
1177
+ if (await fs.exists(absDynamicPath)) {
1178
+ try {
1179
+ dynamic = await loadModuleDefault(absDynamicPath, 'getdotenv-dynamic');
1180
+ }
1181
+ catch {
1182
+ // Preserve legacy error text for compatibility with tests/docs.
1183
+ throw new Error(`Unable to load dynamic TypeScript file: ${absDynamicPath}. ` +
1184
+ `Install 'esbuild' (devDependency) to enable TypeScript dynamic modules.`);
1185
+ }
1186
+ }
782
1187
  }
783
1188
  if (dynamic) {
784
1189
  try {
@@ -817,72 +1222,320 @@ const getDotenv = async (options = {}) => {
817
1222
  };
818
1223
 
819
1224
  /**
820
- * Resolve a tri-state optional boolean flag under exactOptionalPropertyTypes.
821
- * - If the user explicitly enabled the flag, return true.
822
- * - If the user explicitly disabled (the "...-off" variant), return undefined (unset).
823
- * - Otherwise, adopt the default (true set; false/undefined → unset).
824
- *
825
- * @param exclude - The "on" flag value as parsed by Commander.
826
- * @param excludeOff - The "off" toggle (present when specified) as parsed by Commander.
827
- * @param defaultValue - The generator default to adopt when no explicit toggle is present.
828
- * @returns boolean | undefined — use `undefined` to indicate "unset" (do not emit).
829
- *
830
- * @example
831
- * ```ts
832
- * resolveExclusion(undefined, undefined, true); // => true
833
- * ```
1225
+ * Zod schemas for configuration files discovered by the new loader. *
1226
+ * Notes:
1227
+ * - RAW: all fields optional; shapes are stringly-friendly (paths may be string[] or string).
1228
+ * - RESOLVED: normalized shapes (paths always string[]).
1229
+ * - For this step (JSON/YAML only), any defined `dynamic` will be rejected by the loader.
834
1230
  */
835
- const resolveExclusion = (exclude, excludeOff, defaultValue) => exclude ? true : excludeOff ? undefined : defaultValue ? true : undefined;
1231
+ // String-only env value map
1232
+ const stringMap = z.record(z.string(), z.string());
1233
+ const envStringMap = z.record(z.string(), stringMap);
1234
+ // Allow string[] or single string for "paths" in RAW; normalize later.
1235
+ const rawPathsSchema = z.union([z.array(z.string()), z.string()]).optional();
1236
+ const getDotenvConfigSchemaRaw = z.object({
1237
+ dotenvToken: z.string().optional(),
1238
+ privateToken: z.string().optional(),
1239
+ paths: rawPathsSchema,
1240
+ loadProcess: z.boolean().optional(),
1241
+ log: z.boolean().optional(),
1242
+ shell: z.union([z.string(), z.boolean()]).optional(),
1243
+ scripts: z.record(z.string(), z.unknown()).optional(), // Scripts validation left wide; generator validates elsewhere
1244
+ vars: stringMap.optional(), // public, global
1245
+ envVars: envStringMap.optional(), // public, per-env
1246
+ // Dynamic in config (JS/TS only). JSON/YAML loader will reject if set.
1247
+ dynamic: z.unknown().optional(),
1248
+ // Per-plugin config bag; validated by plugins/host when used.
1249
+ plugins: z.record(z.string(), z.unknown()).optional(),
1250
+ });
1251
+ // Normalize paths to string[]
1252
+ const normalizePaths = (p) => p === undefined ? undefined : Array.isArray(p) ? p : [p];
1253
+ const getDotenvConfigSchemaResolved = getDotenvConfigSchemaRaw.transform((raw) => ({
1254
+ ...raw,
1255
+ paths: normalizePaths(raw.paths),
1256
+ }));
1257
+
1258
+ // Discovery candidates (first match wins per scope/privacy).
1259
+ // Order preserves historical JSON/YAML precedence; JS/TS added afterwards.
1260
+ const PUBLIC_FILENAMES = [
1261
+ 'getdotenv.config.json',
1262
+ 'getdotenv.config.yaml',
1263
+ 'getdotenv.config.yml',
1264
+ 'getdotenv.config.js',
1265
+ 'getdotenv.config.mjs',
1266
+ 'getdotenv.config.cjs',
1267
+ 'getdotenv.config.ts',
1268
+ 'getdotenv.config.mts',
1269
+ 'getdotenv.config.cts',
1270
+ ];
1271
+ const LOCAL_FILENAMES = [
1272
+ 'getdotenv.config.local.json',
1273
+ 'getdotenv.config.local.yaml',
1274
+ 'getdotenv.config.local.yml',
1275
+ 'getdotenv.config.local.js',
1276
+ 'getdotenv.config.local.mjs',
1277
+ 'getdotenv.config.local.cjs',
1278
+ 'getdotenv.config.local.ts',
1279
+ 'getdotenv.config.local.mts',
1280
+ 'getdotenv.config.local.cts',
1281
+ ];
1282
+ const isYaml = (p) => ['.yaml', '.yml'].includes(extname(p).toLowerCase());
1283
+ const isJson = (p) => extname(p).toLowerCase() === '.json';
1284
+ const isJsOrTs = (p) => ['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts'].includes(extname(p).toLowerCase());
1285
+ // --- Internal JS/TS module loader helpers (default export) ---
1286
+ const importDefault = async (fileUrl) => {
1287
+ const mod = (await import(fileUrl));
1288
+ return mod.default;
1289
+ };
1290
+ const cacheName = (absPath, suffix) => {
1291
+ // sanitized filename with suffix; recompile on mtime changes not tracked here (simplified)
1292
+ const base = path.basename(absPath).replace(/[^a-zA-Z0-9._-]/g, '_');
1293
+ return `${base}.${suffix}.mjs`;
1294
+ };
1295
+ const ensureDir = async (dir) => {
1296
+ await fs.ensureDir(dir);
1297
+ return dir;
1298
+ };
1299
+ const loadJsTsDefault = async (absPath) => {
1300
+ const fileUrl = pathToFileURL(absPath).toString();
1301
+ const ext = extname(absPath).toLowerCase();
1302
+ if (ext === '.js' || ext === '.mjs' || ext === '.cjs') {
1303
+ return importDefault(fileUrl);
1304
+ }
1305
+ // Try direct import first in case a TS loader is active.
1306
+ try {
1307
+ const val = await importDefault(fileUrl);
1308
+ if (val)
1309
+ return val;
1310
+ }
1311
+ catch {
1312
+ /* fallthrough */
1313
+ }
1314
+ // esbuild bundle to a temp ESM file
1315
+ try {
1316
+ const esbuild = (await import('esbuild'));
1317
+ const outDir = await ensureDir(path.resolve('.tsbuild', 'getdotenv-config'));
1318
+ const outfile = path.join(outDir, cacheName(absPath, 'bundle'));
1319
+ await esbuild.build({
1320
+ entryPoints: [absPath],
1321
+ bundle: true,
1322
+ platform: 'node',
1323
+ format: 'esm',
1324
+ target: 'node20',
1325
+ outfile,
1326
+ sourcemap: false,
1327
+ logLevel: 'silent',
1328
+ });
1329
+ return await importDefault(pathToFileURL(outfile).toString());
1330
+ }
1331
+ catch {
1332
+ /* fallthrough to TS transpile */
1333
+ }
1334
+ // typescript.transpileModule simple transpile (single-file)
1335
+ try {
1336
+ const ts = (await import('typescript'));
1337
+ const src = await fs.readFile(absPath, 'utf-8');
1338
+ const out = ts.transpileModule(src, {
1339
+ compilerOptions: {
1340
+ module: 'ESNext',
1341
+ target: 'ES2022',
1342
+ moduleResolution: 'NodeNext',
1343
+ },
1344
+ }).outputText;
1345
+ const outDir = await ensureDir(path.resolve('.tsbuild', 'getdotenv-config'));
1346
+ const outfile = path.join(outDir, cacheName(absPath, 'ts'));
1347
+ await fs.writeFile(outfile, out, 'utf-8');
1348
+ return await importDefault(pathToFileURL(outfile).toString());
1349
+ }
1350
+ catch {
1351
+ throw new Error(`Unable to load JS/TS config: ${absPath}. Install 'esbuild' for robust bundling or ensure a TS loader.`);
1352
+ }
1353
+ };
836
1354
  /**
837
- * Resolve an optional flag with "--exclude-all" overrides.
838
- * If excludeAll is set and the individual "...-off" is not, force true.
839
- * If excludeAllOff is set and the individual flag is not explicitly set, unset.
840
- * Otherwise, adopt the default (true → set; false/undefined → unset).
841
- *
842
- * @param exclude - Individual include/exclude flag.
843
- * @param excludeOff - Individual "...-off" flag.
844
- * @param defaultValue - Default for the individual flag.
845
- * @param excludeAll - Global "exclude-all" flag.
846
- * @param excludeAllOff - Global "exclude-all-off" flag.
1355
+ * Discover JSON/YAML config files in the packaged root and project root.
1356
+ * Order: packaged public project public project local. */
1357
+ const discoverConfigFiles = async (importMetaUrl) => {
1358
+ const files = [];
1359
+ // Packaged root via importMetaUrl (optional)
1360
+ if (importMetaUrl) {
1361
+ const fromUrl = fileURLToPath(importMetaUrl);
1362
+ const packagedRoot = await packageDirectory({ cwd: fromUrl });
1363
+ if (packagedRoot) {
1364
+ for (const name of PUBLIC_FILENAMES) {
1365
+ const p = join(packagedRoot, name);
1366
+ if (await fs.pathExists(p)) {
1367
+ files.push({ path: p, privacy: 'public', scope: 'packaged' });
1368
+ break; // only one public file expected per scope
1369
+ }
1370
+ }
1371
+ // By policy, packaged .local is not expected; skip even if present.
1372
+ }
1373
+ }
1374
+ // Project root (from current working directory)
1375
+ const projectRoot = await packageDirectory();
1376
+ if (projectRoot) {
1377
+ for (const name of PUBLIC_FILENAMES) {
1378
+ const p = join(projectRoot, name);
1379
+ if (await fs.pathExists(p)) {
1380
+ files.push({ path: p, privacy: 'public', scope: 'project' });
1381
+ break;
1382
+ }
1383
+ }
1384
+ for (const name of LOCAL_FILENAMES) {
1385
+ const p = join(projectRoot, name);
1386
+ if (await fs.pathExists(p)) {
1387
+ files.push({ path: p, privacy: 'local', scope: 'project' });
1388
+ break;
1389
+ }
1390
+ }
1391
+ }
1392
+ return files;
1393
+ };
1394
+ /**
1395
+ * Load a single config file (JSON/YAML). JS/TS is not supported in this step.
1396
+ * Validates with Zod RAW schema, then normalizes to RESOLVED.
847
1397
  *
848
- * @example
849
- * resolveExclusionAll(undefined, undefined, false, true, undefined) =\> true
1398
+ * For JSON/YAML: if a "dynamic" property is present, throws with guidance.
1399
+ * For JS/TS: default export is loaded; "dynamic" is allowed.
1400
+ */
1401
+ const loadConfigFile = async (filePath) => {
1402
+ let raw = {};
1403
+ try {
1404
+ const abs = path.resolve(filePath);
1405
+ if (isJsOrTs(abs)) {
1406
+ // JS/TS support: load default export via robust pipeline.
1407
+ const mod = await loadJsTsDefault(abs);
1408
+ raw = mod ?? {};
1409
+ }
1410
+ else {
1411
+ const txt = await fs.readFile(abs, 'utf-8');
1412
+ raw = isJson(abs) ? JSON.parse(txt) : isYaml(abs) ? YAML.parse(txt) : {};
1413
+ }
1414
+ }
1415
+ catch (err) {
1416
+ throw new Error(`Failed to read/parse config: ${filePath}. ${String(err)}`);
1417
+ }
1418
+ // Validate RAW
1419
+ const parsed = getDotenvConfigSchemaRaw.safeParse(raw);
1420
+ if (!parsed.success) {
1421
+ const msgs = parsed.error.issues
1422
+ .map((i) => `${i.path.join('.')}: ${i.message}`)
1423
+ .join('\n');
1424
+ throw new Error(`Invalid config ${filePath}:\n${msgs}`);
1425
+ }
1426
+ // Disallow dynamic in JSON/YAML; allow in JS/TS
1427
+ if (!isJsOrTs(filePath) && parsed.data.dynamic !== undefined) {
1428
+ throw new Error(`Config ${filePath} specifies "dynamic"; JSON/YAML configs cannot include dynamic in this step. Use JS/TS config.`);
1429
+ }
1430
+ return getDotenvConfigSchemaResolved.parse(parsed.data);
1431
+ };
1432
+ /**
1433
+ * Discover and load configs into resolved shapes, ordered by scope/privacy.
1434
+ * JSON/YAML/JS/TS supported; first match per scope/privacy applies.
850
1435
  */
851
- const resolveExclusionAll = (exclude, excludeOff, defaultValue, excludeAll, excludeAllOff) => excludeAll && !excludeOff
852
- ? true
853
- : excludeAllOff && !exclude
854
- ? undefined
855
- : defaultValue
856
- ? true
857
- : undefined;
1436
+ const resolveGetDotenvConfigSources = async (importMetaUrl) => {
1437
+ const discovered = await discoverConfigFiles(importMetaUrl);
1438
+ const result = {};
1439
+ for (const f of discovered) {
1440
+ const cfg = await loadConfigFile(f.path);
1441
+ if (f.scope === 'packaged') {
1442
+ // packaged public only
1443
+ result.packaged = cfg;
1444
+ }
1445
+ else {
1446
+ result.project ??= {};
1447
+ if (f.privacy === 'public')
1448
+ result.project.public = cfg;
1449
+ else
1450
+ result.project.local = cfg;
1451
+ }
1452
+ }
1453
+ return result;
1454
+ };
1455
+
858
1456
  /**
859
- * exactOptionalPropertyTypes-safe setter for optional boolean flags:
860
- * delete when undefined; assign when defined without requiring an index signature on T.
1457
+ * Resolve dotenv values using the config-loader/overlay path (always-on in
1458
+ * host/generator flows; no-op when no config files are present).
861
1459
  *
862
- * @typeParam T - Target object type.
863
- * @param obj - The object to write to.
864
- * @param key - The optional boolean property key of {@link T}.
865
- * @param value - The value to set or `undefined` to unset.
866
- *
867
- * @remarks
868
- * Writes through a local `Record<string, unknown>` view to avoid requiring an index signature on {@link T}.
1460
+ * Order:
1461
+ * 1) Compute base from files only (exclude dynamic; ignore programmatic vars).
1462
+ * 2) Discover packaged + project config sources and overlay onto base.
1463
+ * 3) Apply dynamics in order:
1464
+ * programmatic dynamic \> config dynamic (packaged → project public → project local)
1465
+ * \> file dynamicPath. * 4) Optionally write outputPath, log, and merge into process.env.
869
1466
  */
870
- const setOptionalFlag = (obj, key, value) => {
871
- const target = obj;
872
- const k = key;
873
- // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
874
- if (value === undefined)
875
- delete target[k];
876
- else
877
- target[k] = value;
1467
+ const resolveDotenvWithConfigLoader = async (validated) => {
1468
+ // 1) Base from files, no dynamic, no programmatic vars
1469
+ const base = await getDotenv({
1470
+ ...validated,
1471
+ // Build a pure base without side effects or logging.
1472
+ excludeDynamic: true,
1473
+ vars: {},
1474
+ log: false,
1475
+ loadProcess: false,
1476
+ outputPath: undefined,
1477
+ });
1478
+ // 2) Discover config sources (packaged via this module's import.meta.url)
1479
+ const sources = await resolveGetDotenvConfigSources(import.meta.url);
1480
+ const dotenv = overlayEnv({
1481
+ base,
1482
+ env: validated.env ?? validated.defaultEnv,
1483
+ configs: sources,
1484
+ ...(validated.vars ? { programmaticVars: validated.vars } : {}),
1485
+ });
1486
+ // Helper to apply a dynamic map progressively.
1487
+ const applyDynamic = (target, dynamic, env) => {
1488
+ if (!dynamic)
1489
+ return;
1490
+ for (const key of Object.keys(dynamic)) {
1491
+ const value = typeof dynamic[key] === 'function'
1492
+ ? dynamic[key](target, env)
1493
+ : dynamic[key];
1494
+ Object.assign(target, { [key]: value });
1495
+ }
1496
+ };
1497
+ // 3) Apply dynamics in order
1498
+ applyDynamic(dotenv, validated.dynamic, validated.env ?? validated.defaultEnv);
1499
+ applyDynamic(dotenv, (sources.packaged?.dynamic ?? undefined), validated.env ?? validated.defaultEnv);
1500
+ applyDynamic(dotenv, (sources.project?.public?.dynamic ?? undefined), validated.env ?? validated.defaultEnv);
1501
+ applyDynamic(dotenv, (sources.project?.local?.dynamic ?? undefined), validated.env ?? validated.defaultEnv);
1502
+ // file dynamicPath (lowest)
1503
+ if (validated.dynamicPath) {
1504
+ const absDynamicPath = path.resolve(validated.dynamicPath);
1505
+ try {
1506
+ const dyn = await loadModuleDefault(absDynamicPath, 'getdotenv-dynamic-host');
1507
+ applyDynamic(dotenv, dyn, validated.env ?? validated.defaultEnv);
1508
+ }
1509
+ catch {
1510
+ throw new Error(`Unable to load dynamic from ${validated.dynamicPath}`);
1511
+ }
1512
+ }
1513
+ // 4) Output/log/process merge
1514
+ if (validated.outputPath) {
1515
+ await fs.writeFile(validated.outputPath, Object.keys(dotenv).reduce((contents, key) => {
1516
+ const value = dotenv[key] ?? '';
1517
+ return `${contents}${key}=${value.includes('\n') ? `"${value}"` : value}\n`;
1518
+ }, ''), { encoding: 'utf-8' });
1519
+ }
1520
+ const logger = validated.logger ?? console;
1521
+ if (validated.log)
1522
+ logger.log(dotenv);
1523
+ if (validated.loadProcess)
1524
+ Object.assign(process.env, dotenv);
1525
+ return dotenv;
878
1526
  };
879
1527
 
1528
+ /**
1529
+ * Omit a "logger" key from an options object in a typed manner.
1530
+ */
1531
+ const omitLogger = (obj) => {
1532
+ const { logger: _omitted, ...rest } = obj;
1533
+ return rest;
1534
+ };
880
1535
  /**
881
1536
  * Build the Commander preSubcommand hook using the provided context.
882
- *
883
- * Responsibilities:
884
- * - Merge parent CLI options with current invocation (parent \< current).
885
- * - Resolve tri-state flags, including `--exclude-all` overrides.
1537
+ * * Responsibilities:
1538
+ * - Merge parent CLI options with current invocation (parent \< current). * - Resolve tri-state flags, including `--exclude-all` overrides.
886
1539
  * - Normalize the shell setting to a concrete value (string | boolean).
887
1540
  * - Persist merged options on the command instance and pass to subcommands.
888
1541
  * - Execute {@link getDotenv} and optional post-hook.
@@ -892,95 +1545,52 @@ const setOptionalFlag = (obj, key, value) => {
892
1545
  * @returns An async hook suitable for Commander’s `preSubcommand`.
893
1546
  *
894
1547
  * @example `program.hook('preSubcommand', makePreSubcommandHook(ctx));`
895
- */ const makePreSubcommandHook = ({ logger, preHook, postHook, defaults }) => async (thisCommand) => {
896
- // Get parent command GetDotenvCliOptions.
897
- const parentGetDotenvCliOptions = process.env.getDotenvCliOptions
898
- ? JSON.parse(process.env.getDotenvCliOptions)
899
- : undefined;
900
- // Get raw CLI options from commander.
901
- const rawCliOptions = thisCommand.opts();
902
- // Extract current GetDotenvCliOptions from raw CLI options.
903
- const { command, debugOff, excludeAll, excludeAllOff, excludeDynamicOff, excludeEnvOff, excludeGlobalOff, excludePrivateOff, excludePublicOff, loadProcessOff, logOff, scripts, shellOff, ...rawCliOptionsRest } = rawCliOptions;
904
- const currentGetDotenvCliOptions = rawCliOptionsRest;
905
- if (scripts)
906
- currentGetDotenvCliOptions.scripts = JSON.parse(scripts);
907
- // Merge current & parent GetDotenvCliOptions (parent < current).
908
- const mergedGetDotenvCliOptions = defaultsDeep((parentGetDotenvCliOptions ?? {}), currentGetDotenvCliOptions);
909
- // Resolve flags using defaults + current + exclude-all toggles.
910
- setOptionalFlag(mergedGetDotenvCliOptions, 'debug', resolveExclusion(mergedGetDotenvCliOptions.debug, debugOff, defaults.debug));
911
- setOptionalFlag(mergedGetDotenvCliOptions, 'excludeDynamic', resolveExclusionAll(mergedGetDotenvCliOptions.excludeDynamic, excludeDynamicOff, defaults.excludeDynamic, excludeAll, excludeAllOff));
912
- setOptionalFlag(mergedGetDotenvCliOptions, 'excludeEnv', resolveExclusionAll(mergedGetDotenvCliOptions.excludeEnv, excludeEnvOff, defaults.excludeEnv, excludeAll, excludeAllOff));
913
- setOptionalFlag(mergedGetDotenvCliOptions, 'excludeGlobal', resolveExclusionAll(mergedGetDotenvCliOptions.excludeGlobal, excludeGlobalOff, defaults.excludeGlobal, excludeAll, excludeAllOff));
914
- setOptionalFlag(mergedGetDotenvCliOptions, 'excludePrivate', resolveExclusionAll(mergedGetDotenvCliOptions.excludePrivate, excludePrivateOff, defaults.excludePrivate, excludeAll, excludeAllOff));
915
- setOptionalFlag(mergedGetDotenvCliOptions, 'excludePublic', resolveExclusionAll(mergedGetDotenvCliOptions.excludePublic, excludePublicOff, defaults.excludePublic, excludeAll, excludeAllOff));
916
- setOptionalFlag(mergedGetDotenvCliOptions, 'log', resolveExclusion(mergedGetDotenvCliOptions.log, logOff, defaults.log));
917
- setOptionalFlag(mergedGetDotenvCliOptions, 'loadProcess', resolveExclusion(mergedGetDotenvCliOptions.loadProcess, loadProcessOff, defaults.loadProcess));
918
- // Normalize shell for predictability: explicit default shell per OS.
919
- const defaultShell = process.platform === 'win32' ? 'powershell.exe' : '/bin/bash';
920
- let resolvedShell = mergedGetDotenvCliOptions.shell;
921
- if (shellOff)
922
- resolvedShell = false;
923
- else if (resolvedShell === true || resolvedShell === undefined) {
924
- resolvedShell = defaultShell;
925
- }
926
- else if (typeof resolvedShell !== 'string' &&
927
- typeof defaults.shell === 'string') {
928
- resolvedShell = defaults.shell;
929
- }
930
- mergedGetDotenvCliOptions.shell = resolvedShell;
931
- if (mergedGetDotenvCliOptions.debug && parentGetDotenvCliOptions) {
932
- logger.debug('\n*** parent command GetDotenvCliOptions ***\n', parentGetDotenvCliOptions);
933
- }
934
- if (mergedGetDotenvCliOptions.debug)
935
- logger.debug('\n*** current command raw options ***\n', rawCliOptions);
936
- if (mergedGetDotenvCliOptions.debug)
937
- logger.debug('\n*** merged GetDotenvCliOptions ***\n', {
938
- mergedGetDotenvCliOptions,
939
- });
940
- // Execute pre-hook.
941
- if (preHook) {
942
- await preHook(mergedGetDotenvCliOptions);
943
- if (mergedGetDotenvCliOptions.debug)
944
- logger.debug('\n*** GetDotenvCliOptions after pre-hook ***\n', mergedGetDotenvCliOptions);
945
- }
946
- // Persist GetDotenvCliOptions in command for subcommand access.
947
- thisCommand.getDotenvCliOptions = mergedGetDotenvCliOptions;
948
- // Execute getdotenv.
949
- const dotenv = await getDotenv(getDotenvCliOptions2Options(mergedGetDotenvCliOptions));
950
- if (mergedGetDotenvCliOptions.debug)
951
- logger.debug('\n*** getDotenv output ***\n', dotenv);
952
- // Execute post-hook.
953
- if (postHook)
954
- await postHook(dotenv);
955
- // Execute command.
956
- const args = thisCommand.args ?? [];
957
- const isCommand = typeof command === 'string' && command.length > 0;
958
- if (isCommand && args.length > 0) {
959
- const lr = logger;
960
- (lr.error ?? lr.log)(`--command option conflicts with cmd subcommand.`);
961
- process.exit(0);
962
- }
963
- if (typeof command === 'string' && command.length > 0) {
964
- const cmd = resolveCommand(mergedGetDotenvCliOptions.scripts, command);
965
- if (mergedGetDotenvCliOptions.debug)
966
- logger.debug('\n*** command ***\n', cmd);
967
- const envSafe = {
968
- ...mergedGetDotenvCliOptions,
969
- };
970
- delete envSafe.logger;
971
- await execaCommand(cmd, {
972
- env: {
973
- ...process.env,
974
- getDotenvCliOptions: JSON.stringify(envSafe),
975
- },
976
- shell: resolveShell(mergedGetDotenvCliOptions.scripts, command, mergedGetDotenvCliOptions.shell),
977
- stdio: 'inherit',
978
- });
979
- }
1548
+ */
1549
+ const makePreSubcommandHook = ({ logger, preHook, postHook, defaults, }) => {
1550
+ return async (thisCommand) => {
1551
+ // Get raw CLI options from commander.
1552
+ const rawCliOptions = thisCommand.opts();
1553
+ const { merged: mergedGetDotenvCliOptions, command: commandOpt } = resolveCliOptions(rawCliOptions, defaults, process.env.getDotenvCliOptions);
1554
+ // Optional debug logging retained via mergedGetDotenvCliOptions.debug if desired. // Execute pre-hook.
1555
+ if (preHook) {
1556
+ await preHook(mergedGetDotenvCliOptions);
1557
+ if (mergedGetDotenvCliOptions.debug)
1558
+ logger.debug('\n*** GetDotenvCliOptions after pre-hook ***\n', mergedGetDotenvCliOptions);
1559
+ }
1560
+ // Persist GetDotenvCliOptions in command for subcommand access.
1561
+ thisCommand.getDotenvCliOptions =
1562
+ mergedGetDotenvCliOptions;
1563
+ // Execute getdotenv via always-on config loader/overlay path.
1564
+ const serviceOptions = getDotenvCliOptions2Options(mergedGetDotenvCliOptions);
1565
+ const dotenv = await resolveDotenvWithConfigLoader(serviceOptions);
1566
+ // Execute post-hook.
1567
+ if (postHook)
1568
+ await postHook(dotenv); // Execute command.
1569
+ const args = thisCommand.args ?? [];
1570
+ const isCommand = typeof commandOpt === 'string' && commandOpt.length > 0;
1571
+ if (isCommand && args.length > 0) {
1572
+ const lr = logger;
1573
+ (lr.error ?? lr.log)(`--command option conflicts with cmd subcommand.`);
1574
+ process.exit(0);
1575
+ }
1576
+ if (typeof commandOpt === 'string' && commandOpt.length > 0) {
1577
+ const cmd = resolveCommand(mergedGetDotenvCliOptions.scripts, commandOpt);
1578
+ if (mergedGetDotenvCliOptions.debug)
1579
+ logger.debug('\n*** command ***\n', cmd);
1580
+ // Build a logger-free bag for env round-trip.
1581
+ const envSafe = omitLogger(mergedGetDotenvCliOptions);
1582
+ await execaCommand(cmd, {
1583
+ env: { ...process.env, getDotenvCliOptions: JSON.stringify(envSafe) },
1584
+ shell: resolveShell(mergedGetDotenvCliOptions.scripts, commandOpt, mergedGetDotenvCliOptions.shell),
1585
+ stdio: 'inherit',
1586
+ });
1587
+ }
1588
+ };
980
1589
  };
981
1590
 
982
1591
  /**
983
- * Generate a Commander CLI Command for get-dotenv. * Orchestration only: delegates building and lifecycle hooks.
1592
+ * Generate a Commander CLI Command for get-dotenv.
1593
+ * Orchestration only: delegates building and lifecycle hooks.
984
1594
  */
985
1595
  const generateGetDotenvCli = async (customOptions) => {
986
1596
  const options = await resolveGetDotenvCliGenerateOptions(customOptions);