@nmakarov/cli-toolkit 0.14.0 → 0.16.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.
Files changed (49) hide show
  1. package/dist/args.cjs +49 -9
  2. package/dist/args.cjs.map +1 -1
  3. package/dist/args.js +49 -9
  4. package/dist/args.js.map +1 -1
  5. package/dist/db.cjs +9 -2
  6. package/dist/db.cjs.map +1 -1
  7. package/dist/db.js +9 -2
  8. package/dist/db.js.map +1 -1
  9. package/dist/errors.cjs +17 -0
  10. package/dist/errors.cjs.map +1 -1
  11. package/dist/errors.js +15 -0
  12. package/dist/errors.js.map +1 -1
  13. package/dist/filedatabase.cjs +16 -24
  14. package/dist/filedatabase.cjs.map +1 -1
  15. package/dist/filedatabase.js +16 -23
  16. package/dist/filedatabase.js.map +1 -1
  17. package/dist/http-client.cjs +44 -34
  18. package/dist/http-client.cjs.map +1 -1
  19. package/dist/http-client.js +44 -34
  20. package/dist/http-client.js.map +1 -1
  21. package/dist/http-client2.cjs +368 -0
  22. package/dist/http-client2.cjs.map +1 -0
  23. package/dist/http-client2.js +340 -0
  24. package/dist/http-client2.js.map +1 -0
  25. package/dist/index.cjs +214 -91
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.js +214 -90
  28. package/dist/index.js.map +1 -1
  29. package/dist/init.cjs +192 -76
  30. package/dist/init.cjs.map +1 -1
  31. package/dist/init.js +192 -76
  32. package/dist/init.js.map +1 -1
  33. package/dist/logger.cjs +19 -36
  34. package/dist/logger.cjs.map +1 -1
  35. package/dist/logger.js +19 -36
  36. package/dist/logger.js.map +1 -1
  37. package/dist/mock-server.cjs +13 -16
  38. package/dist/mock-server.cjs.map +1 -1
  39. package/dist/mock-server.js +13 -16
  40. package/dist/mock-server.js.map +1 -1
  41. package/dist/params.cjs +114 -15
  42. package/dist/params.cjs.map +1 -1
  43. package/dist/params.js +114 -15
  44. package/dist/params.js.map +1 -1
  45. package/dist/screen.cjs +2 -0
  46. package/dist/screen.cjs.map +1 -1
  47. package/dist/screen.js +2 -0
  48. package/dist/screen.js.map +1 -1
  49. package/package.json +9 -2
package/dist/init.cjs CHANGED
@@ -676,6 +676,8 @@ async function showScreen(config2) {
676
676
  keyMatches = true;
677
677
  } else if (input === binding.key) {
678
678
  keyMatches = true;
679
+ } else if (key?.name === binding.key) {
680
+ keyMatches = true;
679
681
  }
680
682
  if (keyMatches) {
681
683
  if (binding.enabled === false) {
@@ -1141,20 +1143,31 @@ var Args = class _Args {
1141
1143
  configValues = {};
1142
1144
  configsLoaded = [];
1143
1145
  env = "local";
1144
- constructor(config2 = {}) {
1146
+ constructor(contextOrConfig = {}, config2) {
1147
+ const hasContext = config2 !== void 0;
1148
+ const configToUse = hasContext ? config2 ?? {} : contextOrConfig ?? {};
1149
+ const context = hasContext ? contextOrConfig : void 0;
1145
1150
  this.aliases = {};
1146
1151
  this.overrides = {};
1147
1152
  this.defaults = {};
1148
1153
  this.prefixes = ["not", "no"];
1149
- if (Object.keys(config2).length > 0) {
1150
- this.configure(config2);
1154
+ if (Object.keys(configToUse).length > 0) {
1155
+ this.configure(configToUse);
1151
1156
  }
1152
- const args = config2.args || process.argv.slice(2);
1157
+ const args = configToUse.args || process.argv.slice(2);
1153
1158
  this.parseArgs(args);
1154
1159
  this.env = this.get("env")?.toLowerCase() || "local";
1155
1160
  this.loadDotEnv();
1156
1161
  this.loadConfigFiles();
1157
1162
  this.checkConflicts();
1163
+ if (context && typeof context.registerCleanup === "function") {
1164
+ context.registerCleanup((ctx) => {
1165
+ const unusedArgs = ctx.args.getUnused();
1166
+ if (unusedArgs.length > 0) {
1167
+ ctx.logger.warn("Unused CLI args:", unusedArgs.join(", "));
1168
+ }
1169
+ });
1170
+ }
1158
1171
  }
1159
1172
  /**
1160
1173
  * Configure Args options
@@ -1176,12 +1189,15 @@ var Args = class _Args {
1176
1189
  }
1177
1190
  }
1178
1191
  /**
1179
- * Initialize Args instance
1180
- * Note: Args is special - it's initialized first, so it can't take context
1181
- * This static method is for consistency with other components
1192
+ * Initialize Args instance.
1193
+ * Args.init(context, config) when used from init/setup: context has registerCleanup, Args registers unused-args cleanup.
1194
+ * Args.init(config) for standalone use (no cleanup).
1182
1195
  */
1183
- static init(config2 = {}) {
1184
- return new _Args(config2);
1196
+ static init(contextOrConfig, config2) {
1197
+ if (config2 !== void 0) {
1198
+ return new _Args(contextOrConfig, config2);
1199
+ }
1200
+ return new _Args(contextOrConfig ?? {});
1185
1201
  }
1186
1202
  /**
1187
1203
  * Parse command line arguments
@@ -1361,6 +1377,32 @@ var Args = class _Args {
1361
1377
  }
1362
1378
  return void 0;
1363
1379
  }
1380
+ /**
1381
+ * Return which layer provided the value for get(key): overrides, cli, config, env, or default.
1382
+ * Does not add key to usedKeys. Use after get(key) when you need the origin.
1383
+ */
1384
+ getSource(key) {
1385
+ const resolvedKey = this.aliases[key] || key;
1386
+ const lcKey = resolvedKey.toLowerCase();
1387
+ const overrideKey = Object.keys(this.overrides).find((k) => k.toLowerCase() === lcKey);
1388
+ if (overrideKey !== void 0) return "overrides";
1389
+ const lcKeyWithEnv = `${lcKey}${this.env ? `_${this.env.toLowerCase()}` : ""}`;
1390
+ if (this.env && this.args[lcKeyWithEnv] !== void 0) return "cli";
1391
+ if (this.args[lcKey] !== void 0) return "cli";
1392
+ const configKey = Object.keys(this.configValues).find((k) => k.toLowerCase() === lcKey);
1393
+ if (configKey !== void 0) return "config";
1394
+ const envKey = this.toEnvKey(resolvedKey);
1395
+ const envKeyWithEnv = `${envKey}${this.env ? `_${this.env.toUpperCase()}` : ""}`;
1396
+ const envSpecificKey = Object.keys(process.env).find((k) => this.env && k.toUpperCase() === envKeyWithEnv);
1397
+ const envKeyFound = Object.keys(process.env).find((k) => k.toUpperCase() === envKey);
1398
+ const envKeyAlt = envKey.replace(/_([0-9])/g, "$1");
1399
+ const envKeyAltFound = !envKeyFound ? Object.keys(process.env).find((k) => k.toUpperCase() === envKeyAlt) : null;
1400
+ if (envSpecificKey || envKeyFound || envKeyAltFound) return "env";
1401
+ const defaultKey = Object.keys(this.defaults).find((k) => k.toLowerCase() === lcKey);
1402
+ if (defaultKey !== void 0) return "default";
1403
+ if (lcKey === "env" && process.env.NODE_ENV !== void 0) return "env";
1404
+ return void 0;
1405
+ }
1364
1406
  /**
1365
1407
  * Set a value (for testing/internal use)
1366
1408
  */
@@ -1713,17 +1755,53 @@ var Params = class _Params {
1713
1755
  context;
1714
1756
  // Partial context during initialization
1715
1757
  params = {};
1758
+ paramSources = {};
1716
1759
  definitions = {};
1717
1760
  args;
1718
1761
  paramSetters = [];
1719
1762
  paramGetters = [];
1720
1763
  trackedParams = [];
1764
+ _currentModule = "script";
1765
+ /** Resolved early in constructor so cleanup does not read params lazily */
1766
+ _showUsedParams = false;
1721
1767
  constructor(context, options = {}) {
1722
1768
  this.context = context;
1723
1769
  this.args = context.args;
1724
1770
  if (Object.keys(options).length > 0) {
1725
1771
  this.configure(options);
1726
1772
  }
1773
+ this._showUsedParams = this.get("showUsedParams", "boolean default false");
1774
+ if (context && typeof context.registerCleanup === "function") {
1775
+ context.registerCleanup((ctx) => {
1776
+ if (!ctx.params.getShowUsedParams()) return;
1777
+ const byModule = ctx.params.getFiguredByModule();
1778
+ const modules = Object.keys(byModule).sort();
1779
+ if (modules.length === 0) return;
1780
+ const logger = ctx.logger;
1781
+ logger.debug("[Params]: list of used params:");
1782
+ if (typeof logger.highlight !== "function") {
1783
+ for (const mod of modules) {
1784
+ logger.debug(` [${mod}]`);
1785
+ for (const [key, entry] of Object.entries(byModule[mod])) {
1786
+ logger.debug(` ${key}: ${JSON.stringify(entry.value)} (${entry.source})`);
1787
+ }
1788
+ }
1789
+ return;
1790
+ }
1791
+ for (const mod of modules) {
1792
+ logger.debug(` [${mod}]`);
1793
+ for (const [key, entry] of Object.entries(byModule[mod])) {
1794
+ const valueStr = JSON.stringify(entry.value);
1795
+ const display = entry.source === "default" ? valueStr : logger.highlight(valueStr);
1796
+ logger.debug(` ${key}: ${display} (${entry.source})`);
1797
+ }
1798
+ }
1799
+ });
1800
+ }
1801
+ }
1802
+ /** Whether --showUsedParams was requested (resolved in constructor). */
1803
+ getShowUsedParams() {
1804
+ return this._showUsedParams;
1727
1805
  }
1728
1806
  /**
1729
1807
  * Configure parameters
@@ -1742,14 +1820,15 @@ var Params = class _Params {
1742
1820
  return new _Params(context, options || {});
1743
1821
  }
1744
1822
  /**
1745
- * Track a parameter request for --stopAfter=init feature
1823
+ * Track a parameter request for --stopAfter=init and --showUsedParams
1746
1824
  */
1747
- trackParam(key, definition, value, source) {
1825
+ trackParam(key, definition, value, source, moduleName) {
1748
1826
  this.trackedParams.push({
1749
1827
  key,
1750
1828
  definition,
1751
1829
  value,
1752
- source
1830
+ source,
1831
+ module: moduleName ?? this._currentModule
1753
1832
  });
1754
1833
  }
1755
1834
  /**
@@ -1759,7 +1838,7 @@ var Params = class _Params {
1759
1838
  return [...this.trackedParams];
1760
1839
  }
1761
1840
  /**
1762
- * Get all figured parameters as a record
1841
+ * Get all figured parameters as a record (flat, last occurrence per key)
1763
1842
  * Returns all parameters that were collected during initialization,
1764
1843
  * whether from CLI args, options, or defaults
1765
1844
  */
@@ -1773,6 +1852,19 @@ var Params = class _Params {
1773
1852
  }
1774
1853
  return result;
1775
1854
  }
1855
+ /**
1856
+ * Get figured parameters grouped by module name.
1857
+ * Same param can appear in multiple modules (e.g. source, resource).
1858
+ */
1859
+ getFiguredByModule() {
1860
+ const byModule = {};
1861
+ for (const param of this.trackedParams) {
1862
+ const mod = param.module;
1863
+ if (!byModule[mod]) byModule[mod] = {};
1864
+ byModule[mod][param.key] = { value: param.value, source: param.source };
1865
+ }
1866
+ return byModule;
1867
+ }
1776
1868
  /**
1777
1869
  * Clear tracked parameters
1778
1870
  */
@@ -1891,14 +1983,19 @@ var Params = class _Params {
1891
1983
  source = "options";
1892
1984
  } else if (valFromArgs !== void 0 && valFromArgs !== null) {
1893
1985
  value = this.validate(key, valFromArgs, def);
1894
- source = "cli";
1986
+ const argsSource = this.args.getSource?.(key);
1987
+ if (argsSource === "overrides") source = "options";
1988
+ else if (argsSource === "cli" || argsSource === "env" || argsSource === "config") source = argsSource;
1989
+ else if (argsSource === "default") source = "default";
1990
+ else source = "cli";
1895
1991
  } else if (valFromParams !== void 0 && valFromParams !== null) {
1896
1992
  value = this.validate(key, valFromParams, def);
1897
- source = "options";
1993
+ source = this.paramSources[key] ?? "options";
1898
1994
  } else {
1899
1995
  value = this.validate(key, void 0, def);
1900
1996
  source = "default";
1901
1997
  }
1998
+ this.paramSources[key] = source;
1902
1999
  this.trackParam(key, definition || "string", value, source);
1903
2000
  if (value !== void 0 && def.values && !def.values.includes(value)) {
1904
2001
  throw new ParamError(`key ${key} should be one of ${def.values}`);
@@ -1919,19 +2016,63 @@ var Params = class _Params {
1919
2016
  }
1920
2017
  }
1921
2018
  /**
1922
- * Get all parameters from definitions
1923
- * Processes parameters left-to-right to support cross-parameter references
2019
+ * Get all parameters from definitions (main script).
2020
+ * Same as getAllForModule("script", defs). Processes left-to-right for cross-parameter references.
1924
2021
  */
1925
2022
  getAll(defs) {
1926
- const res = {};
1927
- for (const [k, def] of Object.entries(defs)) {
1928
- const value = this.get(k, def);
1929
- res[k] = value;
1930
- if (value !== void 0) {
1931
- this.params[k] = value;
2023
+ return this.getAllForModule("script", defs);
2024
+ }
2025
+ /**
2026
+ * Get all parameters from definitions for a given module name.
2027
+ * Figured params are grouped by module when using --showUsedParams.
2028
+ * Processes parameters left-to-right to support cross-parameter references.
2029
+ * If moduleName is omitted, it is inferred from the caller's file path (directory name under src/).
2030
+ */
2031
+ getAllForModule(moduleNameOrDefs, defs) {
2032
+ let moduleName;
2033
+ let definitions;
2034
+ if (defs !== void 0) {
2035
+ moduleName = moduleNameOrDefs;
2036
+ definitions = defs;
2037
+ } else {
2038
+ definitions = moduleNameOrDefs;
2039
+ moduleName = this._inferModuleNameFromStack();
2040
+ }
2041
+ const prev = this._currentModule;
2042
+ this._currentModule = moduleName;
2043
+ try {
2044
+ const res = {};
2045
+ for (const [k, def] of Object.entries(definitions)) {
2046
+ const value = this.get(k, def);
2047
+ res[k] = value;
2048
+ if (value !== void 0) {
2049
+ this.params[k] = value;
2050
+ }
1932
2051
  }
2052
+ return res;
2053
+ } finally {
2054
+ this._currentModule = prev;
1933
2055
  }
1934
- return res;
2056
+ }
2057
+ /**
2058
+ * Infer module name from call stack: first caller outside params/index gives path like .../src/<moduleName>/...
2059
+ */
2060
+ _inferModuleNameFromStack() {
2061
+ const stack = new Error().stack;
2062
+ if (!stack) return "script";
2063
+ const lines = stack.split("\n");
2064
+ const paramsIndexPath = "params" + (typeof process !== "undefined" && process.platform === "win32" ? "\\" : "/") + "index.";
2065
+ for (const line of lines) {
2066
+ const parenMatch = line.match(/\(([^)]+)\)/);
2067
+ if (!parenMatch) continue;
2068
+ const parts = parenMatch[1].split(":");
2069
+ if (parts.length < 3) continue;
2070
+ const path = parts.slice(0, -2).join(":").replace(/^file:\/\//, "");
2071
+ if (!path || path.includes(paramsIndexPath)) continue;
2072
+ const srcMatch = path.match(/[/\\]src[/\\]([^/\\]+)(?:[/\\]|$)/);
2073
+ if (srcMatch) return srcMatch[1];
2074
+ }
2075
+ return "script";
1935
2076
  }
1936
2077
  /**
1937
2078
  * Run all registered getters for a key
@@ -2041,8 +2182,7 @@ var Logger = class _Logger {
2041
2182
  this.updateTransport();
2042
2183
  }
2043
2184
  /**
2044
- * Configure logger options
2045
- * Only parameters present in options are updated
2185
+ * Configure logger options. Accepts both LoggerOptions shape and flat param names (levels string, progressWithTimes, progressThrottleMs).
2046
2186
  */
2047
2187
  configure(options) {
2048
2188
  if (options.mode !== void 0) {
@@ -2052,32 +2192,24 @@ var Logger = class _Logger {
2052
2192
  this.options.route = options.route;
2053
2193
  this.updateTransport();
2054
2194
  }
2055
- if (options.prefix !== void 0) {
2056
- this.options.prefix = options.prefix;
2057
- }
2058
- if (options.silent !== void 0) {
2059
- this.options.silent = options.silent;
2060
- }
2061
- if (options.showLevel !== void 0) {
2062
- this.options.showLevel = options.showLevel;
2063
- }
2064
- if (options.timestamp !== void 0) {
2065
- this.options.timestamp = options.timestamp;
2066
- }
2195
+ if (options.prefix !== void 0) this.options.prefix = options.prefix;
2196
+ if (options.silent !== void 0) this.options.silent = options.silent;
2197
+ if (options.showLevel !== void 0) this.options.showLevel = options.showLevel;
2198
+ if (options.timestamp !== void 0) this.options.timestamp = options.timestamp;
2067
2199
  if (options.levels !== void 0) {
2068
- this.options.levels = this.normalizeLevels(options.levels);
2200
+ const levels = typeof options.levels === "string" ? options.levels.split(",") : options.levels;
2201
+ this.options.levels = this.normalizeLevels(levels);
2069
2202
  }
2070
2203
  if (options.progress !== void 0) {
2071
- if (options.progress.withTimes !== void 0) {
2072
- this.options.progressTimes = options.progress.withTimes;
2073
- }
2074
- if (options.progress.throttleMs !== void 0) {
2075
- this.options.progressThrottle = options.progress.throttleMs;
2076
- }
2204
+ if (options.progress.withTimes !== void 0) this.options.progressTimes = options.progress.withTimes;
2205
+ if (options.progress.throttleMs !== void 0) this.options.progressThrottle = options.progress.throttleMs;
2077
2206
  }
2207
+ const flat = options;
2208
+ if (flat.progressWithTimes !== void 0) this.options.progressTimes = flat.progressWithTimes;
2209
+ if (flat.progressThrottleMs !== void 0) this.options.progressThrottle = flat.progressThrottleMs;
2078
2210
  }
2079
2211
  /**
2080
- * Initialize logger from context and CLI parameters
2212
+ * Initialize logger from context and CLI parameters. Whatever is in options goes (after discovered params).
2081
2213
  */
2082
2214
  static init(context, options) {
2083
2215
  const paramDefs = {
@@ -2091,20 +2223,8 @@ var Logger = class _Logger {
2091
2223
  progressWithTimes: "boolean default false",
2092
2224
  progressThrottleMs: "number"
2093
2225
  };
2094
- const cliParams = context.params.getAll(paramDefs);
2095
- const config2 = {
2096
- mode: options?.mode ?? cliParams.mode,
2097
- route: options?.route ?? cliParams.route,
2098
- prefix: options?.prefix ?? cliParams.prefix,
2099
- silent: options?.silent ?? cliParams.silent,
2100
- showLevel: options?.showLevel ?? cliParams.showLevel,
2101
- timestamp: options?.timestamp ?? cliParams.timestamp,
2102
- levels: options?.levels ?? (cliParams.levels ? cliParams.levels.split(",") : void 0),
2103
- progress: options?.progress ?? {
2104
- withTimes: cliParams.progressWithTimes,
2105
- throttleMs: cliParams.progressThrottleMs
2106
- }
2107
- };
2226
+ const discovered = context.params.getAllForModule(paramDefs);
2227
+ const config2 = { ...discovered, ...options };
2108
2228
  const logger = new _Logger(context, config2);
2109
2229
  context.logger = logger;
2110
2230
  return logger;
@@ -2131,6 +2251,10 @@ var Logger = class _Logger {
2131
2251
  }
2132
2252
  this.options.mode = mode;
2133
2253
  }
2254
+ /** Returns a styled string (bright white) for highlighting; keeps chalk inside logger. */
2255
+ highlight(text) {
2256
+ return import_chalk.default.whiteBright(text);
2257
+ }
2134
2258
  debug(message, ...chunks) {
2135
2259
  this.out({ level: "debug", message, chunks });
2136
2260
  }
@@ -2306,12 +2430,7 @@ function extractComponentOptions(opts, componentName) {
2306
2430
  return componentOptions;
2307
2431
  }
2308
2432
  function setup(opts = {}) {
2309
- const args = Args.init({
2310
- overrides: opts.overrides || {},
2311
- defaults: opts.defaults || {}
2312
- });
2313
2433
  const partialContext = {
2314
- args,
2315
2434
  emitter: new import_events.EventEmitter(),
2316
2435
  isStop: () => false,
2317
2436
  cleanupFunctions: [],
@@ -2319,6 +2438,11 @@ function setup(opts = {}) {
2319
2438
  partialContext.cleanupFunctions.push(fn);
2320
2439
  }
2321
2440
  };
2441
+ const args = Args.init(partialContext, {
2442
+ overrides: opts.overrides || {},
2443
+ defaults: opts.defaults || {}
2444
+ });
2445
+ partialContext.args = args;
2322
2446
  const params = Params.init(partialContext, opts.overrides || {});
2323
2447
  partialContext.params = params;
2324
2448
  const loggerOptions = extractComponentOptions(opts, "logger");
@@ -2379,6 +2503,7 @@ async function init(flow, opts = {}) {
2379
2503
  context.isStop = () => stop;
2380
2504
  context = await setupModules(context, opts);
2381
2505
  const stopAfter = context.args.get("stopAfter");
2506
+ const stopAllowance = context.params.get("stopAllowance", "number default 5");
2382
2507
  if (stopAfter === "init") {
2383
2508
  printAllParameters(context);
2384
2509
  process.exit(0);
@@ -2389,13 +2514,8 @@ async function init(flow, opts = {}) {
2389
2514
  process.exit(2);
2390
2515
  }
2391
2516
  stop = true;
2392
- let allowance = 5;
2393
- try {
2394
- allowance = context.params.get("stopAllowance", "number default 5");
2395
- } catch {
2396
- }
2397
- context.logger.info(`>> emitting stop with allowance ${allowance}`);
2398
- context.emitter.emit("stop", allowance);
2517
+ context.logger.info(`>> emitting stop with allowance ${stopAllowance}`);
2518
+ context.emitter.emit("stop", stopAllowance);
2399
2519
  });
2400
2520
  await flow(context);
2401
2521
  } catch (error) {
@@ -2426,10 +2546,6 @@ async function init(flow, opts = {}) {
2426
2546
  context.logger.warn("[cleanup] error in cleanup function:", error);
2427
2547
  }
2428
2548
  }
2429
- const unusedArgs = context.args.getUnused();
2430
- if (unusedArgs.length > 0) {
2431
- context.logger.warn("Unused CLI args:", unusedArgs.join(", "));
2432
- }
2433
2549
  }
2434
2550
  }
2435
2551
  }