refactor: consolidate duplicate utility functions (#12439)
* refactor: consolidate duplicate utility functions - Add escapeRegExp to src/utils.ts and remove 10 local duplicates - Rename bash-tools clampNumber to clampWithDefault (different signature) - Centralize formatError calls to use formatErrorMessage from infra/errors.ts - Re-export formatErrorMessage from cli/cli-utils.ts to preserve API * refactor: consolidate remaining escapeRegExp duplicates * refactor: consolidate sleep, stripAnsi, and clamp duplicates
This commit is contained in:
@@ -8,6 +8,7 @@ import path from "node:path";
|
||||
import { afterAll, describe, expect, it } from "vitest";
|
||||
import { GatewayClient } from "../src/gateway/client.js";
|
||||
import { loadOrCreateDeviceIdentity } from "../src/infra/device-identity.js";
|
||||
import { sleep } from "../src/utils.js";
|
||||
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../src/utils/message-channel.js";
|
||||
|
||||
type GatewayInstance = {
|
||||
@@ -32,8 +33,6 @@ type HealthPayload = { ok?: boolean };
|
||||
const GATEWAY_START_TIMEOUT_MS = 45_000;
|
||||
const E2E_TIMEOUT_MS = 120_000;
|
||||
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
const getFreePort = async () => {
|
||||
const srv = net.createServer();
|
||||
await new Promise<void>((resolve) => srv.listen(0, "127.0.0.1", resolve));
|
||||
|
||||
@@ -3,6 +3,8 @@ import {
|
||||
formatZonedTimestamp,
|
||||
} from "../../src/infra/format-time/format-datetime.js";
|
||||
|
||||
export { escapeRegExp } from "../../src/utils.js";
|
||||
|
||||
type EnvelopeTimestampZone = string;
|
||||
|
||||
export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone = "utc"): string {
|
||||
@@ -36,7 +38,3 @@ export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone
|
||||
export function formatLocalEnvelopeTimestamp(date: Date): string {
|
||||
return formatEnvelopeTimestamp(date, "local");
|
||||
}
|
||||
|
||||
export function escapeRegExp(value: string): string {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
@@ -1,32 +1,4 @@
|
||||
function stripAnsi(input: string): string {
|
||||
let out = "";
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const code = input.charCodeAt(i);
|
||||
if (code !== 27) {
|
||||
out += input[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
const next = input[i + 1];
|
||||
if (next !== "[") {
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
|
||||
while (i + 1 < input.length) {
|
||||
i += 1;
|
||||
const c = input[i];
|
||||
if (!c) {
|
||||
break;
|
||||
}
|
||||
const isLetter = (c >= "A" && c <= "Z") || (c >= "a" && c <= "z") || c === "~";
|
||||
if (isLetter) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
import { stripAnsi } from "../../src/terminal/ansi.js";
|
||||
|
||||
export function normalizeTestText(input: string): string {
|
||||
return stripAnsi(input)
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { sleep } from "../../src/utils.js";
|
||||
|
||||
export type PollOptions = {
|
||||
timeoutMs?: number;
|
||||
intervalMs?: number;
|
||||
};
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise<void>((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export async function pollUntil<T>(
|
||||
fn: () => Promise<T | null | undefined>,
|
||||
opts: PollOptions = {},
|
||||
|
||||
Reference in New Issue
Block a user