From ab45b409b8f0a03cd98d3befaf576e0bf93fae12 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 04:02:05 +0000 Subject: [PATCH] refactor(cli): dedupe parsePort --- src/cli/daemon-cli/shared.ts | 22 ++-------------------- src/cli/gateway-cli/shared.ts | 21 ++------------------- src/cli/shared/parse-port.ts | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 39 deletions(-) create mode 100644 src/cli/shared/parse-port.ts diff --git a/src/cli/daemon-cli/shared.ts b/src/cli/daemon-cli/shared.ts index a2c587697d..4c07458fb2 100644 --- a/src/cli/daemon-cli/shared.ts +++ b/src/cli/daemon-cli/shared.ts @@ -8,28 +8,10 @@ import { formatRuntimeStatus } from "../../daemon/runtime-format.js"; import { pickPrimaryLanIPv4 } from "../../gateway/net.js"; import { getResolvedLoggerSettings } from "../../logging.js"; import { formatCliCommand } from "../command-format.js"; +import { parsePort } from "../shared/parse-port.js"; export { formatRuntimeStatus }; - -export function parsePort(raw: unknown): number | null { - if (raw === undefined || raw === null) { - return null; - } - const value = - typeof raw === "string" - ? raw - : typeof raw === "number" || typeof raw === "bigint" - ? raw.toString() - : null; - if (value === null) { - return null; - } - const parsed = Number.parseInt(value, 10); - if (!Number.isFinite(parsed) || parsed <= 0) { - return null; - } - return parsed; -} +export { parsePort }; export function parsePortFromArgs(programArguments: string[] | undefined): number | null { if (!programArguments?.length) { diff --git a/src/cli/gateway-cli/shared.ts b/src/cli/gateway-cli/shared.ts index 1beef3e206..224cce8bda 100644 --- a/src/cli/gateway-cli/shared.ts +++ b/src/cli/gateway-cli/shared.ts @@ -6,26 +6,9 @@ import { import { resolveGatewayService } from "../../daemon/service.js"; import { defaultRuntime } from "../../runtime.js"; import { formatCliCommand } from "../command-format.js"; +import { parsePort } from "../shared/parse-port.js"; -export function parsePort(raw: unknown): number | null { - if (raw === undefined || raw === null) { - return null; - } - const value = - typeof raw === "string" - ? raw - : typeof raw === "number" || typeof raw === "bigint" - ? raw.toString() - : null; - if (value === null) { - return null; - } - const parsed = Number.parseInt(value, 10); - if (!Number.isFinite(parsed) || parsed <= 0) { - return null; - } - return parsed; -} +export { parsePort }; export const toOptionString = (value: unknown): string | undefined => { if (typeof value === "string") { diff --git a/src/cli/shared/parse-port.ts b/src/cli/shared/parse-port.ts new file mode 100644 index 0000000000..003fb9ea36 --- /dev/null +++ b/src/cli/shared/parse-port.ts @@ -0,0 +1,19 @@ +export function parsePort(raw: unknown): number | null { + if (raw === undefined || raw === null) { + return null; + } + const value = + typeof raw === "string" + ? raw + : typeof raw === "number" || typeof raw === "bigint" + ? raw.toString() + : null; + if (value === null) { + return null; + } + const parsed = Number.parseInt(value, 10); + if (!Number.isFinite(parsed) || parsed <= 0) { + return null; + } + return parsed; +}