@nmakarov/cli-toolkit 0.25.0 → 0.27.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.
@@ -1758,46 +1758,83 @@ var Params = class _Params {
1758
1758
  paramGetters = [];
1759
1759
  trackedParams = [];
1760
1760
  _currentModule = "script";
1761
- /** Resolved early in constructor so cleanup does not read params lazily */
1762
- _showUsedParams = false;
1761
+ /**
1762
+ * Resolved early in constructor so cleanup does not read params lazily.
1763
+ * One of: false (off) | "end" (print at exit) | "top" (print after init,
1764
+ * via context.showUsedParamsIfNeeded()).
1765
+ */
1766
+ _showUsedParamsMode = false;
1767
+ /** Guard so the dump prints at most once (top OR end, never both). */
1768
+ _usedParamsPrinted = false;
1763
1769
  constructor(context, options = {}) {
1764
1770
  this.context = context;
1765
1771
  this.args = context.args;
1766
1772
  if (Object.keys(options).length > 0) {
1767
1773
  this.configure(options);
1768
1774
  }
1769
- this._showUsedParams = this.get("showUsedParams", "boolean default false");
1775
+ this._resolveShowUsedParams();
1770
1776
  if (context && typeof context.registerCleanup === "function") {
1771
1777
  context.registerCleanup((ctx) => {
1772
- if (!ctx.params.getShowUsedParams()) return;
1773
- const byModule = ctx.params.getFiguredByModule();
1774
- const modules = Object.keys(byModule).sort();
1775
- if (modules.length === 0) return;
1776
- const logger = ctx.logger;
1777
- logger.debug("[Params]: list of used params:");
1778
- if (typeof logger.highlight !== "function") {
1779
- for (const mod of modules) {
1780
- logger.debug(` [${mod}]`);
1781
- for (const [key, entry] of Object.entries(byModule[mod])) {
1782
- logger.debug(` ${key}: ${JSON.stringify(entry.value)} (${entry.source})`);
1783
- }
1784
- }
1785
- return;
1786
- }
1787
- for (const mod of modules) {
1788
- logger.debug(` [${mod}]`);
1789
- for (const [key, entry] of Object.entries(byModule[mod])) {
1790
- const valueStr = JSON.stringify(entry.value);
1791
- const display = entry.source === "default" ? valueStr : logger.highlight(valueStr);
1792
- logger.debug(` ${key}: ${display} (${entry.source})`);
1793
- }
1794
- }
1778
+ if (!ctx.params.getShowUsedParamsMode()) return;
1779
+ ctx.params.printUsedParams(ctx.logger);
1795
1780
  });
1796
1781
  }
1797
1782
  }
1798
- /** Whether --showUsedParams was requested (resolved in constructor). */
1783
+ /**
1784
+ * Resolve the --showUsedParams mode. The flag is intentionally dual-typed:
1785
+ * (absent) / --no-showUsedParams / =false -> false (off)
1786
+ * --showUsedParams / =true -> "end" (print at exit)
1787
+ * --showUsedParams=top -> "top" (print after init)
1788
+ * Read raw (uncoerced) from args so the string "top" isn't forced to a
1789
+ * boolean, then track it under the "script" module for the dump itself.
1790
+ */
1791
+ _resolveShowUsedParams() {
1792
+ const raw = this.args.get("showUsedParams");
1793
+ const source = this.args.getSource?.("showUsedParams") ?? "default";
1794
+ let mode = false;
1795
+ if (raw === void 0 || raw === null) {
1796
+ mode = false;
1797
+ } else if (typeof raw === "string" && raw.trim().toLowerCase() === "top") {
1798
+ mode = "top";
1799
+ } else {
1800
+ const s = typeof raw === "string" ? raw.trim().toLowerCase() : raw;
1801
+ const falsey = s === false || s === "false" || s === "0" || s === "no" || s === "off";
1802
+ mode = falsey ? false : "end";
1803
+ }
1804
+ this._showUsedParamsMode = mode;
1805
+ this.trackParam("showUsedParams", "string", mode, raw === void 0 ? "default" : source, "script");
1806
+ return mode;
1807
+ }
1808
+ /** Whether --showUsedParams was requested in any mode (truthy = on). */
1799
1809
  getShowUsedParams() {
1800
- return this._showUsedParams;
1810
+ return this._showUsedParamsMode !== false;
1811
+ }
1812
+ /** Resolved mode: false | "end" | "top". */
1813
+ getShowUsedParamsMode() {
1814
+ return this._showUsedParamsMode;
1815
+ }
1816
+ /**
1817
+ * Print the module-grouped list of figured params (the --showUsedParams
1818
+ * dump). Idempotent: only the first call prints, so callers can invoke it
1819
+ * at the top (long-running services) without double-printing at exit.
1820
+ */
1821
+ printUsedParams(logger) {
1822
+ if (this._usedParamsPrinted) return;
1823
+ const byModule = this.getFiguredByModule();
1824
+ const modules = Object.keys(byModule).sort();
1825
+ if (modules.length === 0) return;
1826
+ this._usedParamsPrinted = true;
1827
+ logger = logger ?? this.context?.logger ?? console;
1828
+ const hasHighlight = typeof logger.highlight === "function";
1829
+ logger.debug("[Params]: list of used params:");
1830
+ for (const mod of modules) {
1831
+ logger.debug(` [${mod}]`);
1832
+ for (const [key, entry] of Object.entries(byModule[mod])) {
1833
+ const valueStr = JSON.stringify(entry.value);
1834
+ const display = hasHighlight && entry.source !== "default" ? logger.highlight(valueStr) : valueStr;
1835
+ logger.debug(` ${key}: ${display} (${entry.source})`);
1836
+ }
1837
+ }
1801
1838
  }
1802
1839
  /**
1803
1840
  * Configure parameters
@@ -2482,7 +2519,16 @@ function setup(opts = {}) {
2482
2519
  emitter: partialContext.emitter,
2483
2520
  isStop: partialContext.isStop,
2484
2521
  cleanupFunctions: partialContext.cleanupFunctions,
2485
- registerCleanup: partialContext.registerCleanup
2522
+ registerCleanup: partialContext.registerCleanup,
2523
+ // For long-running scripts (servers): with --showUsedParams=top, print
2524
+ // the used-params list now (after the script has initialized all its
2525
+ // own components), instead of at exit. No-op for the default mode,
2526
+ // which prints at exit via the cleanup registered by Params.
2527
+ showUsedParamsIfNeeded: () => {
2528
+ if (params.getShowUsedParamsMode?.() === "top") {
2529
+ params.printUsedParams(logger);
2530
+ }
2531
+ }
2486
2532
  };
2487
2533
  logger.debug("[setup] completed successfully");
2488
2534
  return context;
@@ -2549,15 +2595,19 @@ async function init(flow2, opts = {}) {
2549
2595
  process.exit(0);
2550
2596
  }
2551
2597
  let sigintCount = 0;
2598
+ let firstSigintAt = 0;
2552
2599
  process.on("SIGINT", async () => {
2553
2600
  if (!context) return;
2554
- sigintCount += 1;
2555
- if (sigintCount === 1) {
2601
+ const now = Date.now();
2602
+ if (sigintCount === 0) {
2603
+ sigintCount = 1;
2604
+ firstSigintAt = now;
2556
2605
  stop = true;
2557
2606
  context.logger.info(`>> emitting stop with allowance ${stopAllowance}`);
2558
2607
  context.emitter.emit("stop", stopAllowance);
2559
2608
  return;
2560
2609
  }
2610
+ if (now - firstSigintAt < 250) return;
2561
2611
  context.logger.warn("[process] second SIGINT: running cleanup then exit");
2562
2612
  await runRegisteredCleanups(context);
2563
2613
  process.exit(2);