Files
hyperframes/packages/cli/src/utils/timeoutArg.ts
T
Miguel Ángel b7d72c9478 feat(cli): check --budget stops the whole check in time and reports what it found (#7245)
* feat(cli): check --budget stops the whole check in time and reports what it found

* fix(cli): keep check's comment share where it was

* fix(cli): a --budget that runs out before any check step is waiting is not an unhandled rejection
2026-11-05 14:45:30 +00:00

30 lines
1.1 KiB
TypeScript

const UNIT_MS = { ms: 1, s: 1_000, m: 60_000 } as const;
/** Reads a `--timeout` as milliseconds, raised to `floorMs`; `45s`, `2m` and `45000ms` carry a unit.
* A bare value under 1000 is kept but warned about: it is usually seconds. */
export function parseTimeoutMs(
raw: unknown,
fallback: number,
floorMs = 0,
warn: (message: string) => void = console.error,
flag = "--timeout",
): number {
if (raw === undefined || raw === null || raw === "") return fallback;
const text = String(raw).trim();
const match = /^(\d+(?:\.\d+)?)\s*(ms|s|m)?$/i.exec(text);
if (!match) {
warn(`[hyperframes] ${flag} "${text}" is not a duration; using ${fallback} ms.`);
return fallback;
}
const unit = match[2]?.toLowerCase() as keyof typeof UNIT_MS | undefined;
const ms = Math.round(Number(match[1]) * UNIT_MS[unit ?? "ms"]);
if (ms <= 0) return fallback;
const used = Math.max(ms, floorMs);
if (!unit && ms < 1_000) {
warn(
`[hyperframes] ${flag} ${text} is ${ms} ms, so ${used} ms is used. Write ${text}s for seconds.`,
);
}
return used;
}