@nmakarov/cli-toolkit 0.25.0 → 0.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/init.cjs CHANGED
@@ -1756,46 +1756,87 @@ var Params = class _Params {
1756
1756
  paramGetters = [];
1757
1757
  trackedParams = [];
1758
1758
  _currentModule = "script";
1759
- /** Resolved early in constructor so cleanup does not read params lazily */
1760
- _showUsedParams = false;
1759
+ /**
1760
+ * Resolved early in constructor so cleanup does not read params lazily.
1761
+ * One of: false (off) | "end" (print at exit) | "top" (print after init,
1762
+ * via context.showUsedParamsIfNeeded()) | "stop" (print after init, then
1763
+ * exit the process — also via context.showUsedParamsIfNeeded()).
1764
+ */
1765
+ _showUsedParamsMode = false;
1766
+ /** Guard so the dump prints at most once (top OR end, never both). */
1767
+ _usedParamsPrinted = false;
1761
1768
  constructor(context, options = {}) {
1762
1769
  this.context = context;
1763
1770
  this.args = context.args;
1764
1771
  if (Object.keys(options).length > 0) {
1765
1772
  this.configure(options);
1766
1773
  }
1767
- this._showUsedParams = this.get("showUsedParams", "boolean default false");
1774
+ this._resolveShowUsedParams();
1768
1775
  if (context && typeof context.registerCleanup === "function") {
1769
1776
  context.registerCleanup((ctx) => {
1770
- if (!ctx.params.getShowUsedParams()) return;
1771
- const byModule = ctx.params.getFiguredByModule();
1772
- const modules = Object.keys(byModule).sort();
1773
- if (modules.length === 0) return;
1774
- const logger = ctx.logger;
1775
- logger.debug("[Params]: list of used params:");
1776
- if (typeof logger.highlight !== "function") {
1777
- for (const mod of modules) {
1778
- logger.debug(` [${mod}]`);
1779
- for (const [key, entry] of Object.entries(byModule[mod])) {
1780
- logger.debug(` ${key}: ${JSON.stringify(entry.value)} (${entry.source})`);
1781
- }
1782
- }
1783
- return;
1784
- }
1785
- for (const mod of modules) {
1786
- logger.debug(` [${mod}]`);
1787
- for (const [key, entry] of Object.entries(byModule[mod])) {
1788
- const valueStr = JSON.stringify(entry.value);
1789
- const display = entry.source === "default" ? valueStr : logger.highlight(valueStr);
1790
- logger.debug(` ${key}: ${display} (${entry.source})`);
1791
- }
1792
- }
1777
+ if (!ctx.params.getShowUsedParamsMode()) return;
1778
+ ctx.params.printUsedParams(ctx.logger);
1793
1779
  });
1794
1780
  }
1795
1781
  }
1796
- /** Whether --showUsedParams was requested (resolved in constructor). */
1782
+ /**
1783
+ * Resolve the --showUsedParams mode. The flag is intentionally dual-typed:
1784
+ * (absent) / --no-showUsedParams / =false -> false (off)
1785
+ * --showUsedParams / =true -> "end" (print at exit)
1786
+ * --showUsedParams=top -> "top" (print after init)
1787
+ * --showUsedParams=stop -> "stop" (print after init, then exit)
1788
+ * Read raw (uncoerced) from args so the string "top"/"stop" isn't forced to
1789
+ * a 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 if (typeof raw === "string" && raw.trim().toLowerCase() === "stop") {
1800
+ mode = "stop";
1801
+ } else {
1802
+ const s = typeof raw === "string" ? raw.trim().toLowerCase() : raw;
1803
+ const falsey = s === false || s === "false" || s === "0" || s === "no" || s === "off";
1804
+ mode = falsey ? false : "end";
1805
+ }
1806
+ this._showUsedParamsMode = mode;
1807
+ this.trackParam("showUsedParams", "string", mode, raw === void 0 ? "default" : source, "script");
1808
+ return mode;
1809
+ }
1810
+ /** Whether --showUsedParams was requested in any mode (truthy = on). */
1797
1811
  getShowUsedParams() {
1798
- return this._showUsedParams;
1812
+ return this._showUsedParamsMode !== false;
1813
+ }
1814
+ /** Resolved mode: false | "end" | "top" | "stop". */
1815
+ getShowUsedParamsMode() {
1816
+ return this._showUsedParamsMode;
1817
+ }
1818
+ /**
1819
+ * Print the module-grouped list of figured params (the --showUsedParams
1820
+ * dump). Idempotent: only the first call prints, so callers can invoke it
1821
+ * at the top (long-running services) without double-printing at exit.
1822
+ */
1823
+ printUsedParams(logger) {
1824
+ if (this._usedParamsPrinted) return;
1825
+ const byModule = this.getFiguredByModule();
1826
+ const modules = Object.keys(byModule).sort();
1827
+ if (modules.length === 0) return;
1828
+ this._usedParamsPrinted = true;
1829
+ logger = logger ?? this.context?.logger ?? console;
1830
+ const hasHighlight = typeof logger.highlight === "function";
1831
+ logger.debug("[Params]: list of used params:");
1832
+ for (const mod of modules) {
1833
+ logger.debug(` [${mod}]`);
1834
+ for (const [key, entry] of Object.entries(byModule[mod])) {
1835
+ const valueStr = JSON.stringify(entry.value);
1836
+ const display = hasHighlight && entry.source !== "default" ? logger.highlight(valueStr) : valueStr;
1837
+ logger.debug(` ${key}: ${display} (${entry.source})`);
1838
+ }
1839
+ }
1799
1840
  }
1800
1841
  /**
1801
1842
  * Configure parameters
@@ -2480,7 +2521,25 @@ function setup(opts = {}) {
2480
2521
  emitter: partialContext.emitter,
2481
2522
  isStop: partialContext.isStop,
2482
2523
  cleanupFunctions: partialContext.cleanupFunctions,
2483
- registerCleanup: partialContext.registerCleanup
2524
+ registerCleanup: partialContext.registerCleanup,
2525
+ // For long-running scripts (servers): with --showUsedParams=top, print
2526
+ // the used-params list now (after the script has initialized all its
2527
+ // own components), instead of at exit. No-op for the default mode,
2528
+ // which prints at exit via the cleanup registered by Params.
2529
+ //
2530
+ // --showUsedParams=stop behaves like "top" but then exits immediately —
2531
+ // a quick "show me the figured params and quit" that skips the flow's
2532
+ // actual work. Like --stopAfter=init, this is a hard exit(0) (registered
2533
+ // cleanups are skipped); call it once components/params are resolved.
2534
+ showUsedParamsIfNeeded: () => {
2535
+ const mode = params.getShowUsedParamsMode?.();
2536
+ if (mode !== "top" && mode !== "stop") return;
2537
+ params.printUsedParams(logger);
2538
+ if (mode === "stop") {
2539
+ logger.debug?.("[showUsedParams=stop] params printed \u2014 exiting");
2540
+ process.exit(0);
2541
+ }
2542
+ }
2484
2543
  };
2485
2544
  logger.debug("[setup] completed successfully");
2486
2545
  return context;
@@ -2547,15 +2606,19 @@ async function init(flow, opts = {}) {
2547
2606
  process.exit(0);
2548
2607
  }
2549
2608
  let sigintCount = 0;
2609
+ let firstSigintAt = 0;
2550
2610
  process.on("SIGINT", async () => {
2551
2611
  if (!context) return;
2552
- sigintCount += 1;
2553
- if (sigintCount === 1) {
2612
+ const now = Date.now();
2613
+ if (sigintCount === 0) {
2614
+ sigintCount = 1;
2615
+ firstSigintAt = now;
2554
2616
  stop = true;
2555
2617
  context.logger.info(`>> emitting stop with allowance ${stopAllowance}`);
2556
2618
  context.emitter.emit("stop", stopAllowance);
2557
2619
  return;
2558
2620
  }
2621
+ if (now - firstSigintAt < 250) return;
2559
2622
  context.logger.warn("[process] second SIGINT: running cleanup then exit");
2560
2623
  await runRegisteredCleanups(context);
2561
2624
  process.exit(2);