@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.js CHANGED
@@ -1731,46 +1731,87 @@ var Params = class _Params {
1731
1731
  paramGetters = [];
1732
1732
  trackedParams = [];
1733
1733
  _currentModule = "script";
1734
- /** Resolved early in constructor so cleanup does not read params lazily */
1735
- _showUsedParams = false;
1734
+ /**
1735
+ * Resolved early in constructor so cleanup does not read params lazily.
1736
+ * One of: false (off) | "end" (print at exit) | "top" (print after init,
1737
+ * via context.showUsedParamsIfNeeded()) | "stop" (print after init, then
1738
+ * exit the process — also via context.showUsedParamsIfNeeded()).
1739
+ */
1740
+ _showUsedParamsMode = false;
1741
+ /** Guard so the dump prints at most once (top OR end, never both). */
1742
+ _usedParamsPrinted = false;
1736
1743
  constructor(context, options = {}) {
1737
1744
  this.context = context;
1738
1745
  this.args = context.args;
1739
1746
  if (Object.keys(options).length > 0) {
1740
1747
  this.configure(options);
1741
1748
  }
1742
- this._showUsedParams = this.get("showUsedParams", "boolean default false");
1749
+ this._resolveShowUsedParams();
1743
1750
  if (context && typeof context.registerCleanup === "function") {
1744
1751
  context.registerCleanup((ctx) => {
1745
- if (!ctx.params.getShowUsedParams()) return;
1746
- const byModule = ctx.params.getFiguredByModule();
1747
- const modules = Object.keys(byModule).sort();
1748
- if (modules.length === 0) return;
1749
- const logger = ctx.logger;
1750
- logger.debug("[Params]: list of used params:");
1751
- if (typeof logger.highlight !== "function") {
1752
- for (const mod of modules) {
1753
- logger.debug(` [${mod}]`);
1754
- for (const [key, entry] of Object.entries(byModule[mod])) {
1755
- logger.debug(` ${key}: ${JSON.stringify(entry.value)} (${entry.source})`);
1756
- }
1757
- }
1758
- return;
1759
- }
1760
- for (const mod of modules) {
1761
- logger.debug(` [${mod}]`);
1762
- for (const [key, entry] of Object.entries(byModule[mod])) {
1763
- const valueStr = JSON.stringify(entry.value);
1764
- const display = entry.source === "default" ? valueStr : logger.highlight(valueStr);
1765
- logger.debug(` ${key}: ${display} (${entry.source})`);
1766
- }
1767
- }
1752
+ if (!ctx.params.getShowUsedParamsMode()) return;
1753
+ ctx.params.printUsedParams(ctx.logger);
1768
1754
  });
1769
1755
  }
1770
1756
  }
1771
- /** Whether --showUsedParams was requested (resolved in constructor). */
1757
+ /**
1758
+ * Resolve the --showUsedParams mode. The flag is intentionally dual-typed:
1759
+ * (absent) / --no-showUsedParams / =false -> false (off)
1760
+ * --showUsedParams / =true -> "end" (print at exit)
1761
+ * --showUsedParams=top -> "top" (print after init)
1762
+ * --showUsedParams=stop -> "stop" (print after init, then exit)
1763
+ * Read raw (uncoerced) from args so the string "top"/"stop" isn't forced to
1764
+ * a boolean, then track it under the "script" module for the dump itself.
1765
+ */
1766
+ _resolveShowUsedParams() {
1767
+ const raw = this.args.get("showUsedParams");
1768
+ const source = this.args.getSource?.("showUsedParams") ?? "default";
1769
+ let mode = false;
1770
+ if (raw === void 0 || raw === null) {
1771
+ mode = false;
1772
+ } else if (typeof raw === "string" && raw.trim().toLowerCase() === "top") {
1773
+ mode = "top";
1774
+ } else if (typeof raw === "string" && raw.trim().toLowerCase() === "stop") {
1775
+ mode = "stop";
1776
+ } else {
1777
+ const s = typeof raw === "string" ? raw.trim().toLowerCase() : raw;
1778
+ const falsey = s === false || s === "false" || s === "0" || s === "no" || s === "off";
1779
+ mode = falsey ? false : "end";
1780
+ }
1781
+ this._showUsedParamsMode = mode;
1782
+ this.trackParam("showUsedParams", "string", mode, raw === void 0 ? "default" : source, "script");
1783
+ return mode;
1784
+ }
1785
+ /** Whether --showUsedParams was requested in any mode (truthy = on). */
1772
1786
  getShowUsedParams() {
1773
- return this._showUsedParams;
1787
+ return this._showUsedParamsMode !== false;
1788
+ }
1789
+ /** Resolved mode: false | "end" | "top" | "stop". */
1790
+ getShowUsedParamsMode() {
1791
+ return this._showUsedParamsMode;
1792
+ }
1793
+ /**
1794
+ * Print the module-grouped list of figured params (the --showUsedParams
1795
+ * dump). Idempotent: only the first call prints, so callers can invoke it
1796
+ * at the top (long-running services) without double-printing at exit.
1797
+ */
1798
+ printUsedParams(logger) {
1799
+ if (this._usedParamsPrinted) return;
1800
+ const byModule = this.getFiguredByModule();
1801
+ const modules = Object.keys(byModule).sort();
1802
+ if (modules.length === 0) return;
1803
+ this._usedParamsPrinted = true;
1804
+ logger = logger ?? this.context?.logger ?? console;
1805
+ const hasHighlight = typeof logger.highlight === "function";
1806
+ logger.debug("[Params]: list of used params:");
1807
+ for (const mod of modules) {
1808
+ logger.debug(` [${mod}]`);
1809
+ for (const [key, entry] of Object.entries(byModule[mod])) {
1810
+ const valueStr = JSON.stringify(entry.value);
1811
+ const display = hasHighlight && entry.source !== "default" ? logger.highlight(valueStr) : valueStr;
1812
+ logger.debug(` ${key}: ${display} (${entry.source})`);
1813
+ }
1814
+ }
1774
1815
  }
1775
1816
  /**
1776
1817
  * Configure parameters
@@ -2455,7 +2496,25 @@ function setup(opts = {}) {
2455
2496
  emitter: partialContext.emitter,
2456
2497
  isStop: partialContext.isStop,
2457
2498
  cleanupFunctions: partialContext.cleanupFunctions,
2458
- registerCleanup: partialContext.registerCleanup
2499
+ registerCleanup: partialContext.registerCleanup,
2500
+ // For long-running scripts (servers): with --showUsedParams=top, print
2501
+ // the used-params list now (after the script has initialized all its
2502
+ // own components), instead of at exit. No-op for the default mode,
2503
+ // which prints at exit via the cleanup registered by Params.
2504
+ //
2505
+ // --showUsedParams=stop behaves like "top" but then exits immediately —
2506
+ // a quick "show me the figured params and quit" that skips the flow's
2507
+ // actual work. Like --stopAfter=init, this is a hard exit(0) (registered
2508
+ // cleanups are skipped); call it once components/params are resolved.
2509
+ showUsedParamsIfNeeded: () => {
2510
+ const mode = params.getShowUsedParamsMode?.();
2511
+ if (mode !== "top" && mode !== "stop") return;
2512
+ params.printUsedParams(logger);
2513
+ if (mode === "stop") {
2514
+ logger.debug?.("[showUsedParams=stop] params printed \u2014 exiting");
2515
+ process.exit(0);
2516
+ }
2517
+ }
2459
2518
  };
2460
2519
  logger.debug("[setup] completed successfully");
2461
2520
  return context;
@@ -2522,15 +2581,19 @@ async function init(flow, opts = {}) {
2522
2581
  process.exit(0);
2523
2582
  }
2524
2583
  let sigintCount = 0;
2584
+ let firstSigintAt = 0;
2525
2585
  process.on("SIGINT", async () => {
2526
2586
  if (!context) return;
2527
- sigintCount += 1;
2528
- if (sigintCount === 1) {
2587
+ const now = Date.now();
2588
+ if (sigintCount === 0) {
2589
+ sigintCount = 1;
2590
+ firstSigintAt = now;
2529
2591
  stop = true;
2530
2592
  context.logger.info(`>> emitting stop with allowance ${stopAllowance}`);
2531
2593
  context.emitter.emit("stop", stopAllowance);
2532
2594
  return;
2533
2595
  }
2596
+ if (now - firstSigintAt < 250) return;
2534
2597
  context.logger.warn("[process] second SIGINT: running cleanup then exit");
2535
2598
  await runRegisteredCleanups(context);
2536
2599
  process.exit(2);