@karmaniverous/get-dotenv 5.2.4 → 5.2.5

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/dist/cliHost.d.ts CHANGED
@@ -1,6 +1,47 @@
1
1
  import { Command } from 'commander';
2
2
  import { ZodType } from 'zod';
3
3
 
4
+ /**
5
+ * Minimal root options shape shared by CLI and generator layers.
6
+ * Keep keys optional to respect exactOptionalPropertyTypes semantics.
7
+ */
8
+ type RootOptionsShape = {
9
+ env?: string;
10
+ vars?: string;
11
+ command?: string;
12
+ outputPath?: string;
13
+ shell?: string | boolean;
14
+ loadProcess?: boolean;
15
+ excludeAll?: boolean;
16
+ excludeDynamic?: boolean;
17
+ excludeEnv?: boolean;
18
+ excludeGlobal?: boolean;
19
+ excludePrivate?: boolean;
20
+ excludePublic?: boolean;
21
+ log?: boolean;
22
+ debug?: boolean;
23
+ capture?: boolean;
24
+ strict?: boolean;
25
+ redact?: boolean;
26
+ warnEntropy?: boolean;
27
+ entropyThreshold?: number;
28
+ entropyMinLength?: number;
29
+ entropyWhitelist?: string[];
30
+ redactPatterns?: string[];
31
+ defaultEnv?: string;
32
+ dotenvToken?: string;
33
+ dynamicPath?: string;
34
+ trace?: boolean | string[];
35
+ paths?: string;
36
+ pathsDelimiter?: string;
37
+ pathsDelimiterPattern?: string;
38
+ privateToken?: string;
39
+ varsDelimiter?: string;
40
+ varsDelimiterPattern?: string;
41
+ varsAssignor?: string;
42
+ varsAssignorPattern?: string;
43
+ scripts?: ScriptsTable;
44
+ };
4
45
  /**
5
46
  * Scripts table shape (configurable shell type).
6
47
  */
@@ -9,6 +50,27 @@ type ScriptsTable<TShell extends string | boolean = string | boolean> = Record<s
9
50
  shell?: TShell;
10
51
  }>;
11
52
 
53
+ /**
54
+ * Adapter-layer augmentation: add chainable helpers to GetDotenvCli without
55
+ * coupling the core host to cliCore. Importing this module has side effects:
56
+ * it extends the prototype and merges types for consumers.
57
+ */
58
+ declare module '../cliHost/GetDotenvCli' {
59
+ interface GetDotenvCli {
60
+ /**
61
+ * Attach legacy root flags to this CLI instance. Defaults come from
62
+ * baseRootOptionDefaults when none are provided. */
63
+ attachRootOptions(defaults?: Partial<RootOptionsShape>, opts?: {
64
+ includeCommandOption?: boolean;
65
+ }): this;
66
+ /**
67
+ * Install a preSubcommand hook that merges CLI flags (including parent
68
+ * round-trip) and resolves the dotenv context before executing actions.
69
+ * Defaults come from baseRootOptionDefaults when none are provided.
70
+ */ passOptions(defaults?: Partial<RootOptionsShape>): this;
71
+ }
72
+ }
73
+
12
74
  /**
13
75
  * A minimal representation of an environment key/value mapping.
14
76
  * Values may be `undefined` to represent "unset". */ type ProcessEnv = Record<string, string | undefined>;
package/dist/cliHost.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { Command } from 'commander';
1
+ import { Command, Option } from 'commander';
2
2
  import fs from 'fs-extra';
3
3
  import { packageDirectory } from 'package-directory';
4
4
  import url, { fileURLToPath, pathToFileURL } from 'url';
@@ -9,34 +9,6 @@ import { nanoid } from 'nanoid';
9
9
  import { parse } from 'dotenv';
10
10
  import { createHash } from 'crypto';
11
11
 
12
- /** src/cliHost/definePlugin.ts
13
- * Plugin contracts for the GetDotenv CLI host.
14
- *
15
- * This module exposes a structural public interface for the host that plugins
16
- * should use (GetDotenvCliPublic). Using a structural type at the seam avoids
17
- * nominal class identity issues (private fields) in downstream consumers.
18
- */
19
- /**
20
- * Define a GetDotenv CLI plugin with compositional helpers.
21
- *
22
- * @example
23
- * const parent = definePlugin(\{ id: 'p', setup(cli) \{ /* ... *\/ \} \})
24
- * .use(childA)
25
- * .use(childB);
26
- */
27
- const definePlugin = (spec) => {
28
- const { children = [], ...rest } = spec;
29
- const plugin = {
30
- ...rest,
31
- children: [...children],
32
- use(child) {
33
- this.children.push(child);
34
- return this;
35
- },
36
- };
37
- return plugin;
38
- };
39
-
40
12
  // Base root CLI defaults (shared; kept untyped here to avoid cross-layer deps).
41
13
  const baseRootOptionDefaults = {
42
14
  dotenvToken: '.env',
@@ -593,6 +565,22 @@ const dotenvExpandAll = (values = {}, options = {}) => Object.keys(values).reduc
593
565
  });
594
566
  return acc;
595
567
  }, {});
568
+ /**
569
+ * Recursively expands environment variables in a string using `process.env` as
570
+ * the expansion reference. Variables may be presented with optional default as
571
+ * `$VAR[:default]` or `${VAR[:default]}`. Unknown variables will expand to an
572
+ * empty string.
573
+ *
574
+ * @param value - The string to expand.
575
+ * @returns The expanded string.
576
+ *
577
+ * @example
578
+ * ```ts
579
+ * process.env.FOO = 'bar';
580
+ * dotenvExpandFromProcessEnv('Hello $FOO'); // "Hello bar"
581
+ * ```
582
+ */
583
+ const dotenvExpandFromProcessEnv = (value) => dotenvExpand(value, process.env);
596
584
 
597
585
  const applyKv = (current, kv) => {
598
586
  if (!kv || Object.keys(kv).length === 0)
@@ -1443,6 +1431,412 @@ class GetDotenvCli extends Command {
1443
1431
  }
1444
1432
  }
1445
1433
 
1434
+ /**
1435
+ * Validate a composed env against config-provided validation surfaces.
1436
+ * Precedence for validation definitions:
1437
+ * project.local -\> project.public -\> packaged
1438
+ *
1439
+ * Behavior:
1440
+ * - If a JS/TS `schema` is present, use schema.safeParse(finalEnv).
1441
+ * - Else if `requiredKeys` is present, check presence (value !== undefined).
1442
+ * - Returns a flat list of issue strings; caller decides warn vs fail.
1443
+ */
1444
+ const validateEnvAgainstSources = (finalEnv, sources) => {
1445
+ const pick = (getter) => {
1446
+ const pl = sources.project?.local;
1447
+ const pp = sources.project?.public;
1448
+ const pk = sources.packaged;
1449
+ return ((pl && getter(pl)) ||
1450
+ (pp && getter(pp)) ||
1451
+ (pk && getter(pk)) ||
1452
+ undefined);
1453
+ };
1454
+ const schema = pick((cfg) => cfg['schema']);
1455
+ if (schema &&
1456
+ typeof schema.safeParse === 'function') {
1457
+ try {
1458
+ const parsed = schema.safeParse(finalEnv);
1459
+ if (!parsed.success) {
1460
+ // Try to render zod-style issues when available.
1461
+ const err = parsed.error;
1462
+ const issues = Array.isArray(err.issues) && err.issues.length > 0
1463
+ ? err.issues.map((i) => {
1464
+ const path = Array.isArray(i.path) ? i.path.join('.') : '';
1465
+ const msg = i.message ?? 'Invalid value';
1466
+ return path ? `[schema] ${path}: ${msg}` : `[schema] ${msg}`;
1467
+ })
1468
+ : ['[schema] validation failed'];
1469
+ return issues;
1470
+ }
1471
+ return [];
1472
+ }
1473
+ catch {
1474
+ // If schema invocation fails, surface a single diagnostic.
1475
+ return [
1476
+ '[schema] validation failed (unable to execute schema.safeParse)',
1477
+ ];
1478
+ }
1479
+ }
1480
+ const requiredKeys = pick((cfg) => cfg['requiredKeys']);
1481
+ if (Array.isArray(requiredKeys) && requiredKeys.length > 0) {
1482
+ const missing = requiredKeys.filter((k) => finalEnv[k] === undefined);
1483
+ if (missing.length > 0) {
1484
+ return missing.map((k) => `[requiredKeys] missing: ${k}`);
1485
+ }
1486
+ }
1487
+ return [];
1488
+ };
1489
+
1490
+ /**
1491
+ * Attach legacy root flags to a Commander program.
1492
+ * Uses provided defaults to render help labels without coupling to generators.
1493
+ */
1494
+ const attachRootOptions = (program, defaults, opts) => {
1495
+ // Install temporary wrappers to tag all options added here as "base".
1496
+ const GROUP = 'base';
1497
+ const tagLatest = (cmd, group) => {
1498
+ const optsArr = cmd.options;
1499
+ if (Array.isArray(optsArr) && optsArr.length > 0) {
1500
+ const last = optsArr[optsArr.length - 1];
1501
+ last.__group = group;
1502
+ }
1503
+ };
1504
+ const originalAddOption = program.addOption.bind(program);
1505
+ const originalOption = program.option.bind(program);
1506
+ program.addOption = function patchedAdd(opt) {
1507
+ // Tag before adding, in case consumers inspect the Option directly.
1508
+ opt.__group = GROUP;
1509
+ const ret = originalAddOption(opt);
1510
+ return ret;
1511
+ };
1512
+ program.option = function patchedOption(...args) {
1513
+ const ret = originalOption(...args);
1514
+ tagLatest(this, GROUP);
1515
+ return ret;
1516
+ };
1517
+ const { defaultEnv, dotenvToken, dynamicPath, env, excludeDynamic, excludeEnv, excludeGlobal, excludePrivate, excludePublic, loadProcess, log, outputPath, paths, pathsDelimiter, pathsDelimiterPattern, privateToken, scripts, shell, varsAssignor, varsAssignorPattern, varsDelimiter, varsDelimiterPattern, } = defaults ?? {};
1518
+ const va = typeof defaults?.varsAssignor === 'string' ? defaults.varsAssignor : '=';
1519
+ const vd = typeof defaults?.varsDelimiter === 'string' ? defaults.varsDelimiter : ' ';
1520
+ // Build initial chain.
1521
+ let p = program
1522
+ .enablePositionalOptions()
1523
+ .passThroughOptions()
1524
+ .option('-e, --env <string>', `target environment (dotenv-expanded)`, dotenvExpandFromProcessEnv, env);
1525
+ p = p.option('-v, --vars <string>', `extra variables expressed as delimited key-value pairs (dotenv-expanded): ${[
1526
+ ['KEY1', 'VAL1'],
1527
+ ['KEY2', 'VAL2'],
1528
+ ]
1529
+ .map((v) => v.join(va))
1530
+ .join(vd)}`, dotenvExpandFromProcessEnv);
1531
+ // Optional legacy root command flag (kept for generated CLI compatibility).
1532
+ // Default is OFF; the generator opts in explicitly.
1533
+ if (opts?.includeCommandOption === true) {
1534
+ p = p.option('-c, --command <string>', 'command executed according to the --shell option, conflicts with cmd subcommand (dotenv-expanded)', dotenvExpandFromProcessEnv);
1535
+ }
1536
+ p = p
1537
+ .option('-o, --output-path <string>', 'consolidated output file (dotenv-expanded)', dotenvExpandFromProcessEnv, outputPath)
1538
+ .addOption(new Option('-s, --shell [string]', (() => {
1539
+ let defaultLabel = '';
1540
+ if (shell !== undefined) {
1541
+ if (typeof shell === 'boolean') {
1542
+ defaultLabel = ' (default OS shell)';
1543
+ }
1544
+ else if (typeof shell === 'string') {
1545
+ // Safe string interpolation
1546
+ defaultLabel = ` (default ${shell})`;
1547
+ }
1548
+ }
1549
+ return `command execution shell, no argument for default OS shell or provide shell string${defaultLabel}`;
1550
+ })()).conflicts('shellOff'))
1551
+ .addOption(new Option('-S, --shell-off', `command execution shell OFF${!shell ? ' (default)' : ''}`).conflicts('shell'))
1552
+ .addOption(new Option('-p, --load-process', `load variables to process.env ON${loadProcess ? ' (default)' : ''}`).conflicts('loadProcessOff'))
1553
+ .addOption(new Option('-P, --load-process-off', `load variables to process.env OFF${!loadProcess ? ' (default)' : ''}`).conflicts('loadProcess'))
1554
+ .addOption(new Option('-a, --exclude-all', `exclude all dotenv variables from loading ON${excludeDynamic &&
1555
+ ((excludeEnv && excludeGlobal) || (excludePrivate && excludePublic))
1556
+ ? ' (default)'
1557
+ : ''}`).conflicts('excludeAllOff'))
1558
+ .addOption(new Option('-A, --exclude-all-off', `exclude all dotenv variables from loading OFF (default)`).conflicts('excludeAll'))
1559
+ .addOption(new Option('-z, --exclude-dynamic', `exclude dynamic dotenv variables from loading ON${excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamicOff'))
1560
+ .addOption(new Option('-Z, --exclude-dynamic-off', `exclude dynamic dotenv variables from loading OFF${!excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamic'))
1561
+ .addOption(new Option('-n, --exclude-env', `exclude environment-specific dotenv variables from loading${excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnvOff'))
1562
+ .addOption(new Option('-N, --exclude-env-off', `exclude environment-specific dotenv variables from loading OFF${!excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnv'))
1563
+ .addOption(new Option('-g, --exclude-global', `exclude global dotenv variables from loading ON${excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobalOff'))
1564
+ .addOption(new Option('-G, --exclude-global-off', `exclude global dotenv variables from loading OFF${!excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobal'))
1565
+ .addOption(new Option('-r, --exclude-private', `exclude private dotenv variables from loading ON${excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivateOff'))
1566
+ .addOption(new Option('-R, --exclude-private-off', `exclude private dotenv variables from loading OFF${!excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivate'))
1567
+ .addOption(new Option('-u, --exclude-public', `exclude public dotenv variables from loading ON${excludePublic ? ' (default)' : ''}`).conflicts('excludePublicOff'))
1568
+ .addOption(new Option('-U, --exclude-public-off', `exclude public dotenv variables from loading OFF${!excludePublic ? ' (default)' : ''}`).conflicts('excludePublic'))
1569
+ .addOption(new Option('-l, --log', `console log loaded variables ON${log ? ' (default)' : ''}`).conflicts('logOff'))
1570
+ .addOption(new Option('-L, --log-off', `console log loaded variables OFF${!log ? ' (default)' : ''}`).conflicts('log'))
1571
+ .option('--capture', 'capture child process stdio for commands (tests/CI)')
1572
+ .option('--redact', 'mask secret-like values in logs/trace (presentation-only)')
1573
+ .option('--default-env <string>', 'default target environment', dotenvExpandFromProcessEnv, defaultEnv)
1574
+ .option('--dotenv-token <string>', 'dotenv-expanded token indicating a dotenv file', dotenvExpandFromProcessEnv, dotenvToken)
1575
+ .option('--dynamic-path <string>', 'dynamic variables path (.js or .ts; .ts is auto-compiled when esbuild is available, otherwise precompile)', dotenvExpandFromProcessEnv, dynamicPath)
1576
+ .option('--paths <string>', 'dotenv-expanded delimited list of paths to dotenv directory', dotenvExpandFromProcessEnv, paths)
1577
+ .option('--paths-delimiter <string>', 'paths delimiter string', pathsDelimiter)
1578
+ .option('--paths-delimiter-pattern <string>', 'paths delimiter regex pattern', pathsDelimiterPattern)
1579
+ .option('--private-token <string>', 'dotenv-expanded token indicating private variables', dotenvExpandFromProcessEnv, privateToken)
1580
+ .option('--vars-delimiter <string>', 'vars delimiter string', varsDelimiter)
1581
+ .option('--vars-delimiter-pattern <string>', 'vars delimiter regex pattern', varsDelimiterPattern)
1582
+ .option('--vars-assignor <string>', 'vars assignment operator string', varsAssignor)
1583
+ .option('--vars-assignor-pattern <string>', 'vars assignment operator regex pattern', varsAssignorPattern)
1584
+ // Hidden scripts pipe-through (stringified)
1585
+ .addOption(new Option('--scripts <string>')
1586
+ .default(JSON.stringify(scripts))
1587
+ .hideHelp());
1588
+ // Diagnostics: opt-in tracing; optional variadic keys after the flag.
1589
+ p = p.option('--trace [keys...]', 'emit diagnostics for child env composition (optional keys)');
1590
+ // Validation: strict mode fails on env validation issues (warn by default).
1591
+ p = p.option('--strict', 'fail on env validation errors (schema/requiredKeys)');
1592
+ // Entropy diagnostics (presentation-only)
1593
+ p = p
1594
+ .addOption(new Option('--entropy-warn', 'enable entropy warnings (default on)').conflicts('entropyWarnOff'))
1595
+ .addOption(new Option('--entropy-warn-off', 'disable entropy warnings').conflicts('entropyWarn'))
1596
+ .option('--entropy-threshold <number>', 'entropy bits/char threshold (default 3.8)')
1597
+ .option('--entropy-min-length <number>', 'min length to examine for entropy (default 16)')
1598
+ .option('--entropy-whitelist <pattern...>', 'suppress entropy warnings when key matches any regex pattern')
1599
+ .option('--redact-pattern <pattern...>', 'additional key-match regex patterns to trigger redaction');
1600
+ // Restore original methods to avoid tagging future additions outside base.
1601
+ program.addOption = originalAddOption;
1602
+ program.option = originalOption;
1603
+ return p;
1604
+ };
1605
+
1606
+ /**
1607
+ * Resolve a tri-state optional boolean flag under exactOptionalPropertyTypes.
1608
+ * - If the user explicitly enabled the flag, return true.
1609
+ * - If the user explicitly disabled (the "...-off" variant), return undefined (unset).
1610
+ * - Otherwise, adopt the default (true → set; false/undefined → unset).
1611
+ *
1612
+ * @param exclude - The "on" flag value as parsed by Commander.
1613
+ * @param excludeOff - The "off" toggle (present when specified) as parsed by Commander.
1614
+ * @param defaultValue - The generator default to adopt when no explicit toggle is present.
1615
+ * @returns boolean | undefined — use `undefined` to indicate "unset" (do not emit).
1616
+ *
1617
+ * @example
1618
+ * ```ts
1619
+ * resolveExclusion(undefined, undefined, true); // => true
1620
+ * ```
1621
+ */
1622
+ const resolveExclusion = (exclude, excludeOff, defaultValue) => exclude ? true : excludeOff ? undefined : defaultValue ? true : undefined;
1623
+ /**
1624
+ * Resolve an optional flag with "--exclude-all" overrides.
1625
+ * If excludeAll is set and the individual "...-off" is not, force true.
1626
+ * If excludeAllOff is set and the individual flag is not explicitly set, unset.
1627
+ * Otherwise, adopt the default (true → set; false/undefined → unset).
1628
+ *
1629
+ * @param exclude - Individual include/exclude flag.
1630
+ * @param excludeOff - Individual "...-off" flag.
1631
+ * @param defaultValue - Default for the individual flag.
1632
+ * @param excludeAll - Global "exclude-all" flag.
1633
+ * @param excludeAllOff - Global "exclude-all-off" flag.
1634
+ *
1635
+ * @example
1636
+ * resolveExclusionAll(undefined, undefined, false, true, undefined) =\> true
1637
+ */
1638
+ const resolveExclusionAll = (exclude, excludeOff, defaultValue, excludeAll, excludeAllOff) =>
1639
+ // Order of precedence:
1640
+ // 1) Individual explicit "on" wins outright.
1641
+ // 2) Individual explicit "off" wins over any global.
1642
+ // 3) Global exclude-all forces true when not explicitly turned off.
1643
+ // 4) Global exclude-all-off unsets when the individual wasn't explicitly enabled.
1644
+ // 5) Fall back to the default (true => set; false/undefined => unset).
1645
+ (() => {
1646
+ // Individual "on"
1647
+ if (exclude === true)
1648
+ return true;
1649
+ // Individual "off"
1650
+ if (excludeOff === true)
1651
+ return undefined;
1652
+ // Global "exclude-all" ON (unless explicitly turned off)
1653
+ if (excludeAll === true)
1654
+ return true;
1655
+ // Global "exclude-all-off" (unless explicitly enabled)
1656
+ if (excludeAllOff === true)
1657
+ return undefined;
1658
+ // Default
1659
+ return defaultValue ? true : undefined;
1660
+ })();
1661
+ /**
1662
+ * exactOptionalPropertyTypes-safe setter for optional boolean flags:
1663
+ * delete when undefined; assign when defined — without requiring an index signature on T.
1664
+ *
1665
+ * @typeParam T - Target object type.
1666
+ * @param obj - The object to write to.
1667
+ * @param key - The optional boolean property key of {@link T}.
1668
+ * @param value - The value to set or `undefined` to unset.
1669
+ *
1670
+ * @remarks
1671
+ * Writes through a local `Record<string, unknown>` view to avoid requiring an index signature on {@link T}.
1672
+ */
1673
+ const setOptionalFlag = (obj, key, value) => {
1674
+ const target = obj;
1675
+ const k = key;
1676
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
1677
+ if (value === undefined)
1678
+ delete target[k];
1679
+ else
1680
+ target[k] = value;
1681
+ };
1682
+
1683
+ /**
1684
+ * Merge and normalize raw Commander options (current + parent + defaults)
1685
+ * into a GetDotenvCliOptions-like object. Types are intentionally wide to
1686
+ * avoid cross-layer coupling; callers may cast as needed.
1687
+ */
1688
+ const resolveCliOptions = (rawCliOptions, defaults, parentJson) => {
1689
+ const parent = typeof parentJson === 'string' && parentJson.length > 0
1690
+ ? JSON.parse(parentJson)
1691
+ : undefined;
1692
+ const { command, debugOff, excludeAll, excludeAllOff, excludeDynamicOff, excludeEnvOff, excludeGlobalOff, excludePrivateOff, excludePublicOff, loadProcessOff, logOff, entropyWarn, entropyWarnOff, scripts, shellOff, ...rest } = rawCliOptions;
1693
+ const current = { ...rest };
1694
+ if (typeof scripts === 'string') {
1695
+ try {
1696
+ current.scripts = JSON.parse(scripts);
1697
+ }
1698
+ catch {
1699
+ // ignore parse errors; leave scripts undefined
1700
+ }
1701
+ }
1702
+ const merged = defaultsDeep({}, defaults, parent ?? {}, current);
1703
+ const d = defaults;
1704
+ setOptionalFlag(merged, 'debug', resolveExclusion(merged.debug, debugOff, d.debug));
1705
+ setOptionalFlag(merged, 'excludeDynamic', resolveExclusionAll(merged.excludeDynamic, excludeDynamicOff, d.excludeDynamic, excludeAll, excludeAllOff));
1706
+ setOptionalFlag(merged, 'excludeEnv', resolveExclusionAll(merged.excludeEnv, excludeEnvOff, d.excludeEnv, excludeAll, excludeAllOff));
1707
+ setOptionalFlag(merged, 'excludeGlobal', resolveExclusionAll(merged.excludeGlobal, excludeGlobalOff, d.excludeGlobal, excludeAll, excludeAllOff));
1708
+ setOptionalFlag(merged, 'excludePrivate', resolveExclusionAll(merged.excludePrivate, excludePrivateOff, d.excludePrivate, excludeAll, excludeAllOff));
1709
+ setOptionalFlag(merged, 'excludePublic', resolveExclusionAll(merged.excludePublic, excludePublicOff, d.excludePublic, excludeAll, excludeAllOff));
1710
+ setOptionalFlag(merged, 'log', resolveExclusion(merged.log, logOff, d.log));
1711
+ setOptionalFlag(merged, 'loadProcess', resolveExclusion(merged.loadProcess, loadProcessOff, d.loadProcess));
1712
+ // warnEntropy (tri-state)
1713
+ setOptionalFlag(merged, 'warnEntropy', resolveExclusion(merged.warnEntropy, entropyWarnOff, d.warnEntropy));
1714
+ // Normalize shell for predictability: explicit default shell per OS.
1715
+ const defaultShell = process.platform === 'win32' ? 'powershell.exe' : '/bin/bash';
1716
+ let resolvedShell = merged.shell;
1717
+ if (shellOff)
1718
+ resolvedShell = false;
1719
+ else if (resolvedShell === true || resolvedShell === undefined) {
1720
+ resolvedShell = defaultShell;
1721
+ }
1722
+ else if (typeof resolvedShell !== 'string' &&
1723
+ typeof defaults.shell === 'string') {
1724
+ resolvedShell = defaults.shell;
1725
+ }
1726
+ merged.shell = resolvedShell;
1727
+ const cmd = typeof command === 'string' ? command : undefined;
1728
+ return cmd !== undefined ? { merged, command: cmd } : { merged };
1729
+ };
1730
+
1731
+ GetDotenvCli.prototype.attachRootOptions = function (defaults, opts) {
1732
+ const d = (defaults ?? baseRootOptionDefaults);
1733
+ attachRootOptions(this, d, opts);
1734
+ return this;
1735
+ };
1736
+ GetDotenvCli.prototype.passOptions = function (defaults) {
1737
+ const d = (defaults ?? baseRootOptionDefaults);
1738
+ this.hook('preSubcommand', async (thisCommand) => {
1739
+ const raw = thisCommand.opts();
1740
+ const { merged } = resolveCliOptions(raw, d, process.env.getDotenvCliOptions);
1741
+ // Persist merged options for nested invocations (batch exec).
1742
+ thisCommand.getDotenvCliOptions =
1743
+ merged;
1744
+ // Also store on the host for downstream ergonomic accessors.
1745
+ this._setOptionsBag(merged);
1746
+ // Build service options and compute context (always-on config loader path).
1747
+ const serviceOptions = getDotenvCliOptions2Options(merged);
1748
+ await this.resolveAndLoad(serviceOptions);
1749
+ // Global validation: once after Phase C using config sources.
1750
+ try {
1751
+ const ctx = this.getCtx();
1752
+ const dotenv = (ctx?.dotenv ?? {});
1753
+ const sources = await resolveGetDotenvConfigSources(import.meta.url);
1754
+ const issues = validateEnvAgainstSources(dotenv, sources);
1755
+ if (Array.isArray(issues) && issues.length > 0) {
1756
+ const logger = (merged.logger ??
1757
+ console);
1758
+ const emit = logger.error ?? logger.log;
1759
+ issues.forEach((m) => {
1760
+ emit(m);
1761
+ });
1762
+ if (merged.strict) {
1763
+ // Deterministic failure under strict mode
1764
+ process.exit(1);
1765
+ }
1766
+ }
1767
+ }
1768
+ catch {
1769
+ // Be tolerant: validation errors reported above; unexpected failures here
1770
+ // should not crash non-strict flows.
1771
+ }
1772
+ });
1773
+ // Also handle root-level flows (no subcommand) so option-aliases can run
1774
+ // with the same merged options and context without duplicating logic.
1775
+ this.hook('preAction', async (thisCommand) => {
1776
+ const raw = thisCommand.opts();
1777
+ const { merged } = resolveCliOptions(raw, d, process.env.getDotenvCliOptions);
1778
+ thisCommand.getDotenvCliOptions =
1779
+ merged;
1780
+ this._setOptionsBag(merged);
1781
+ // Avoid duplicate heavy work if a context is already present.
1782
+ if (!this.getCtx()) {
1783
+ const serviceOptions = getDotenvCliOptions2Options(merged);
1784
+ await this.resolveAndLoad(serviceOptions);
1785
+ try {
1786
+ const ctx = this.getCtx();
1787
+ const dotenv = (ctx?.dotenv ?? {});
1788
+ const sources = await resolveGetDotenvConfigSources(import.meta.url);
1789
+ const issues = validateEnvAgainstSources(dotenv, sources);
1790
+ if (Array.isArray(issues) && issues.length > 0) {
1791
+ const logger = (merged
1792
+ .logger ?? console);
1793
+ const emit = logger.error ?? logger.log;
1794
+ issues.forEach((m) => {
1795
+ emit(m);
1796
+ });
1797
+ if (merged.strict) {
1798
+ process.exit(1);
1799
+ }
1800
+ }
1801
+ }
1802
+ catch {
1803
+ // Tolerate validation side-effects in non-strict mode
1804
+ }
1805
+ }
1806
+ });
1807
+ return this;
1808
+ };
1809
+
1810
+ /** src/cliHost/definePlugin.ts
1811
+ * Plugin contracts for the GetDotenv CLI host.
1812
+ *
1813
+ * This module exposes a structural public interface for the host that plugins
1814
+ * should use (GetDotenvCliPublic). Using a structural type at the seam avoids
1815
+ * nominal class identity issues (private fields) in downstream consumers.
1816
+ */
1817
+ /**
1818
+ * Define a GetDotenv CLI plugin with compositional helpers.
1819
+ *
1820
+ * @example
1821
+ * const parent = definePlugin(\{ id: 'p', setup(cli) \{ /* ... *\/ \} \})
1822
+ * .use(childA)
1823
+ * .use(childB);
1824
+ */
1825
+ const definePlugin = (spec) => {
1826
+ const { children = [], ...rest } = spec;
1827
+ const plugin = {
1828
+ ...rest,
1829
+ children: [...children],
1830
+ use(child) {
1831
+ this.children.push(child);
1832
+ return this;
1833
+ },
1834
+ };
1835
+ return plugin;
1836
+ };
1837
+
1838
+ // Ensure attachRootOptions() and passOptions() are available whenever the
1839
+ // /cliHost subpath is imported (unconditional for downstream hosts).
1446
1840
  /**
1447
1841
  * Helper to retrieve the merged root options bag from any action handler
1448
1842
  * that only has access to thisCommand. Avoids structural casts.
@@ -3752,7 +3752,7 @@ new Command()
3752
3752
 
3753
3753
  new Command()
3754
3754
  .name('cmd')
3755
- .description('Batch execute command according to the --shell option, conflicts with --command option (default subcommand)')
3755
+ .description('Execute command according to the --shell option, conflicts with --command option (default subcommand)')
3756
3756
  .configureHelp({ showGlobalOptions: true })
3757
3757
  .enablePositionalOptions()
3758
3758
  .passThroughOptions()
package/dist/index.cjs CHANGED
@@ -3761,7 +3761,7 @@ const batchCommand = new commander.Command()
3761
3761
 
3762
3762
  const cmdCommand = new commander.Command()
3763
3763
  .name('cmd')
3764
- .description('Batch execute command according to the --shell option, conflicts with --command option (default subcommand)')
3764
+ .description('Execute command according to the --shell option, conflicts with --command option (default subcommand)')
3765
3765
  .configureHelp({ showGlobalOptions: true })
3766
3766
  .enablePositionalOptions()
3767
3767
  .passThroughOptions()
package/dist/index.mjs CHANGED
@@ -3758,7 +3758,7 @@ const batchCommand = new Command()
3758
3758
 
3759
3759
  const cmdCommand = new Command()
3760
3760
  .name('cmd')
3761
- .description('Batch execute command according to the --shell option, conflicts with --command option (default subcommand)')
3761
+ .description('Execute command according to the --shell option, conflicts with --command option (default subcommand)')
3762
3762
  .configureHelp({ showGlobalOptions: true })
3763
3763
  .enablePositionalOptions()
3764
3764
  .passThroughOptions()
@@ -1,6 +1,76 @@
1
1
  import { Command } from 'commander';
2
2
  import { ZodType } from 'zod';
3
3
 
4
+ /**
5
+ * Minimal root options shape shared by CLI and generator layers.
6
+ * Keep keys optional to respect exactOptionalPropertyTypes semantics.
7
+ */
8
+ type RootOptionsShape = {
9
+ env?: string;
10
+ vars?: string;
11
+ command?: string;
12
+ outputPath?: string;
13
+ shell?: string | boolean;
14
+ loadProcess?: boolean;
15
+ excludeAll?: boolean;
16
+ excludeDynamic?: boolean;
17
+ excludeEnv?: boolean;
18
+ excludeGlobal?: boolean;
19
+ excludePrivate?: boolean;
20
+ excludePublic?: boolean;
21
+ log?: boolean;
22
+ debug?: boolean;
23
+ capture?: boolean;
24
+ strict?: boolean;
25
+ redact?: boolean;
26
+ warnEntropy?: boolean;
27
+ entropyThreshold?: number;
28
+ entropyMinLength?: number;
29
+ entropyWhitelist?: string[];
30
+ redactPatterns?: string[];
31
+ defaultEnv?: string;
32
+ dotenvToken?: string;
33
+ dynamicPath?: string;
34
+ trace?: boolean | string[];
35
+ paths?: string;
36
+ pathsDelimiter?: string;
37
+ pathsDelimiterPattern?: string;
38
+ privateToken?: string;
39
+ varsDelimiter?: string;
40
+ varsDelimiterPattern?: string;
41
+ varsAssignor?: string;
42
+ varsAssignorPattern?: string;
43
+ scripts?: ScriptsTable;
44
+ };
45
+ /**
46
+ * Scripts table shape (configurable shell type).
47
+ */
48
+ type ScriptsTable<TShell extends string | boolean = string | boolean> = Record<string, string | {
49
+ cmd: string;
50
+ shell?: TShell;
51
+ }>;
52
+
53
+ /**
54
+ * Adapter-layer augmentation: add chainable helpers to GetDotenvCli without
55
+ * coupling the core host to cliCore. Importing this module has side effects:
56
+ * it extends the prototype and merges types for consumers.
57
+ */
58
+ declare module '../cliHost/GetDotenvCli' {
59
+ interface GetDotenvCli {
60
+ /**
61
+ * Attach legacy root flags to this CLI instance. Defaults come from
62
+ * baseRootOptionDefaults when none are provided. */
63
+ attachRootOptions(defaults?: Partial<RootOptionsShape>, opts?: {
64
+ includeCommandOption?: boolean;
65
+ }): this;
66
+ /**
67
+ * Install a preSubcommand hook that merges CLI flags (including parent
68
+ * round-trip) and resolves the dotenv context before executing actions.
69
+ * Defaults come from baseRootOptionDefaults when none are provided.
70
+ */ passOptions(defaults?: Partial<RootOptionsShape>): this;
71
+ }
72
+ }
73
+
4
74
  /**
5
75
  * A minimal representation of an environment key/value mapping.
6
76
  * Values may be `undefined` to represent "unset". */ type ProcessEnv = Record<string, string | undefined>;