@nmakarov/cli-toolkit 0.78.0 → 0.80.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
@@ -2643,13 +2643,14 @@ var Logger = class _Logger {
2643
2643
  */
2644
2644
  progress(message, opts) {
2645
2645
  const { prefix, count, total } = opts;
2646
- const paddedTotal = String(total).length;
2646
+ const displayTotal = Math.max(Number(total) || 0, Number(count) || 0);
2647
+ const paddedTotal = String(displayTotal).length;
2647
2648
  const paddedCount = String(count).padStart(paddedTotal, " ");
2648
2649
  const payload = {
2649
2650
  level: "progress",
2650
2651
  message,
2651
2652
  count: paddedCount,
2652
- total,
2653
+ total: displayTotal,
2653
2654
  prefix
2654
2655
  };
2655
2656
  const key = prefix ?? "";
@@ -2666,7 +2667,7 @@ var Logger = class _Logger {
2666
2667
  if (wantTimes) {
2667
2668
  let remaining = -1;
2668
2669
  if (itemsPerSec > 0) {
2669
- remaining = (total - count) / itemsPerSec;
2670
+ remaining = Math.max(0, (displayTotal - count) / itemsPerSec);
2670
2671
  }
2671
2672
  payload.elapsed = this.round(elapsedSeconds, 2);
2672
2673
  payload.remaining = remaining >= 0 ? this.round(remaining, 2) : remaining;
@@ -2675,12 +2676,12 @@ var Logger = class _Logger {
2675
2676
  payload.rate = itemsPerSec >= 0 ? this.round(itemsPerSec, 2) : itemsPerSec;
2676
2677
  }
2677
2678
  }
2678
- if (count >= total) {
2679
+ if (count === total) {
2679
2680
  delete this.startTimes[key];
2680
2681
  delete this.startCounts[key];
2681
2682
  delete this.lastProgressTimes[key];
2682
2683
  }
2683
- if (this.shouldOutputProgress(prefix ?? "", count, total)) {
2684
+ if (this.shouldOutputProgress(prefix ?? "", count, displayTotal)) {
2684
2685
  this.out(payload);
2685
2686
  if (this.options.progressThrottle && prefix) {
2686
2687
  this.lastProgressTimes[prefix] = Date.now();
@@ -2839,6 +2840,8 @@ function setup(opts = {}) {
2839
2840
  // a quick "show me the figured params and quit" that skips the flow's
2840
2841
  // actual work. Like --stopAfter=init, this is a hard exit(0) (registered
2841
2842
  // cleanups are skipped); call it once components/params are resolved.
2843
+ _requestExitCode: null,
2844
+ requestExit: null,
2842
2845
  showUsedParamsIfNeeded: () => {
2843
2846
  const mode = params.getShowUsedParamsMode?.();
2844
2847
  if (mode !== "top" && mode !== "stop") return;
@@ -2849,6 +2852,9 @@ function setup(opts = {}) {
2849
2852
  }
2850
2853
  }
2851
2854
  };
2855
+ context.requestExit = (code = 0) => {
2856
+ context._requestExitCode = code;
2857
+ };
2852
2858
  logger.debug("[setup] completed successfully");
2853
2859
  return context;
2854
2860
  }
@@ -2884,9 +2890,25 @@ async function init(flow, opts = {}) {
2884
2890
  if (cleanupRan) return;
2885
2891
  cleanupRan = true;
2886
2892
  const fns = [...ctx.cleanupFunctions].reverse();
2893
+ const budgetMs = 5e3;
2894
+ const started = Date.now();
2887
2895
  for (const fn of fns) {
2896
+ const left = budgetMs - (Date.now() - started);
2897
+ if (left <= 0) {
2898
+ ctx.logger.warn("[cleanup] budget exhausted \u2014 skipping remaining cleanup");
2899
+ break;
2900
+ }
2888
2901
  try {
2889
- await fn(ctx);
2902
+ await Promise.race([
2903
+ Promise.resolve(fn(ctx)),
2904
+ new Promise((_, reject) => {
2905
+ const t = setTimeout(
2906
+ () => reject(new Error(`cleanup timed out after ${left}ms`)),
2907
+ left
2908
+ );
2909
+ t.unref?.();
2910
+ })
2911
+ ]);
2890
2912
  } catch (error) {
2891
2913
  ctx.logger.warn("[cleanup] error in cleanup function:", error);
2892
2914
  }
@@ -2982,6 +3004,8 @@ async function init(flow, opts = {}) {
2982
3004
  process.exit(process.exitCode);
2983
3005
  } else if (stop || kill) {
2984
3006
  process.exit(0);
3007
+ } else if (context._requestExitCode != null) {
3008
+ process.exit(context._requestExitCode);
2985
3009
  }
2986
3010
  }
2987
3011
  }