@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.
package/dist/index.cjs CHANGED
@@ -1810,46 +1810,83 @@ var Params = class _Params {
1810
1810
  paramGetters = [];
1811
1811
  trackedParams = [];
1812
1812
  _currentModule = "script";
1813
- /** Resolved early in constructor so cleanup does not read params lazily */
1814
- _showUsedParams = false;
1813
+ /**
1814
+ * Resolved early in constructor so cleanup does not read params lazily.
1815
+ * One of: false (off) | "end" (print at exit) | "top" (print after init,
1816
+ * via context.showUsedParamsIfNeeded()).
1817
+ */
1818
+ _showUsedParamsMode = false;
1819
+ /** Guard so the dump prints at most once (top OR end, never both). */
1820
+ _usedParamsPrinted = false;
1815
1821
  constructor(context, options = {}) {
1816
1822
  this.context = context;
1817
1823
  this.args = context.args;
1818
1824
  if (Object.keys(options).length > 0) {
1819
1825
  this.configure(options);
1820
1826
  }
1821
- this._showUsedParams = this.get("showUsedParams", "boolean default false");
1827
+ this._resolveShowUsedParams();
1822
1828
  if (context && typeof context.registerCleanup === "function") {
1823
1829
  context.registerCleanup((ctx) => {
1824
- if (!ctx.params.getShowUsedParams()) return;
1825
- const byModule = ctx.params.getFiguredByModule();
1826
- const modules = Object.keys(byModule).sort();
1827
- if (modules.length === 0) return;
1828
- const logger = ctx.logger;
1829
- logger.debug("[Params]: list of used params:");
1830
- if (typeof logger.highlight !== "function") {
1831
- for (const mod of modules) {
1832
- logger.debug(` [${mod}]`);
1833
- for (const [key, entry] of Object.entries(byModule[mod])) {
1834
- logger.debug(` ${key}: ${JSON.stringify(entry.value)} (${entry.source})`);
1835
- }
1836
- }
1837
- return;
1838
- }
1839
- for (const mod of modules) {
1840
- logger.debug(` [${mod}]`);
1841
- for (const [key, entry] of Object.entries(byModule[mod])) {
1842
- const valueStr = JSON.stringify(entry.value);
1843
- const display = entry.source === "default" ? valueStr : logger.highlight(valueStr);
1844
- logger.debug(` ${key}: ${display} (${entry.source})`);
1845
- }
1846
- }
1830
+ if (!ctx.params.getShowUsedParamsMode()) return;
1831
+ ctx.params.printUsedParams(ctx.logger);
1847
1832
  });
1848
1833
  }
1849
1834
  }
1850
- /** Whether --showUsedParams was requested (resolved in constructor). */
1835
+ /**
1836
+ * Resolve the --showUsedParams mode. The flag is intentionally dual-typed:
1837
+ * (absent) / --no-showUsedParams / =false -> false (off)
1838
+ * --showUsedParams / =true -> "end" (print at exit)
1839
+ * --showUsedParams=top -> "top" (print after init)
1840
+ * Read raw (uncoerced) from args so the string "top" isn't forced to a
1841
+ * boolean, then track it under the "script" module for the dump itself.
1842
+ */
1843
+ _resolveShowUsedParams() {
1844
+ const raw = this.args.get("showUsedParams");
1845
+ const source = this.args.getSource?.("showUsedParams") ?? "default";
1846
+ let mode = false;
1847
+ if (raw === void 0 || raw === null) {
1848
+ mode = false;
1849
+ } else if (typeof raw === "string" && raw.trim().toLowerCase() === "top") {
1850
+ mode = "top";
1851
+ } else {
1852
+ const s = typeof raw === "string" ? raw.trim().toLowerCase() : raw;
1853
+ const falsey = s === false || s === "false" || s === "0" || s === "no" || s === "off";
1854
+ mode = falsey ? false : "end";
1855
+ }
1856
+ this._showUsedParamsMode = mode;
1857
+ this.trackParam("showUsedParams", "string", mode, raw === void 0 ? "default" : source, "script");
1858
+ return mode;
1859
+ }
1860
+ /** Whether --showUsedParams was requested in any mode (truthy = on). */
1851
1861
  getShowUsedParams() {
1852
- return this._showUsedParams;
1862
+ return this._showUsedParamsMode !== false;
1863
+ }
1864
+ /** Resolved mode: false | "end" | "top". */
1865
+ getShowUsedParamsMode() {
1866
+ return this._showUsedParamsMode;
1867
+ }
1868
+ /**
1869
+ * Print the module-grouped list of figured params (the --showUsedParams
1870
+ * dump). Idempotent: only the first call prints, so callers can invoke it
1871
+ * at the top (long-running services) without double-printing at exit.
1872
+ */
1873
+ printUsedParams(logger) {
1874
+ if (this._usedParamsPrinted) return;
1875
+ const byModule = this.getFiguredByModule();
1876
+ const modules = Object.keys(byModule).sort();
1877
+ if (modules.length === 0) return;
1878
+ this._usedParamsPrinted = true;
1879
+ logger = logger ?? this.context?.logger ?? console;
1880
+ const hasHighlight = typeof logger.highlight === "function";
1881
+ logger.debug("[Params]: list of used params:");
1882
+ for (const mod of modules) {
1883
+ logger.debug(` [${mod}]`);
1884
+ for (const [key, entry] of Object.entries(byModule[mod])) {
1885
+ const valueStr = JSON.stringify(entry.value);
1886
+ const display = hasHighlight && entry.source !== "default" ? logger.highlight(valueStr) : valueStr;
1887
+ logger.debug(` ${key}: ${display} (${entry.source})`);
1888
+ }
1889
+ }
1853
1890
  }
1854
1891
  /**
1855
1892
  * Configure parameters
@@ -4385,7 +4422,16 @@ function setup(opts = {}) {
4385
4422
  emitter: partialContext.emitter,
4386
4423
  isStop: partialContext.isStop,
4387
4424
  cleanupFunctions: partialContext.cleanupFunctions,
4388
- registerCleanup: partialContext.registerCleanup
4425
+ registerCleanup: partialContext.registerCleanup,
4426
+ // For long-running scripts (servers): with --showUsedParams=top, print
4427
+ // the used-params list now (after the script has initialized all its
4428
+ // own components), instead of at exit. No-op for the default mode,
4429
+ // which prints at exit via the cleanup registered by Params.
4430
+ showUsedParamsIfNeeded: () => {
4431
+ if (params.getShowUsedParamsMode?.() === "top") {
4432
+ params.printUsedParams(logger);
4433
+ }
4434
+ }
4389
4435
  };
4390
4436
  logger.debug("[setup] completed successfully");
4391
4437
  return context;