From ae599243fd97249b59a52aa171ae7f537a0558f4 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 04:42:00 +0000 Subject: [PATCH] refactor(cli): dedupe configure section parsing --- src/cli/config-cli.ts | 15 +++++++-------- src/cli/program/register.configure.ts | 8 ++------ src/commands/configure.shared.ts | 18 ++++++++++++++++++ src/commands/configure.ts | 6 +++++- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/cli/config-cli.ts b/src/cli/config-cli.ts index af4819166..587d1be60 100644 --- a/src/cli/config-cli.ts +++ b/src/cli/config-cli.ts @@ -293,19 +293,18 @@ export function registerConfigCli(program: Command) { [] as string[], ) .action(async (opts) => { - const { CONFIGURE_WIZARD_SECTIONS, configureCommand, configureCommandWithSections } = - await import("../commands/configure.js"); - const sections: string[] = Array.isArray(opts.section) - ? opts.section - .map((value: unknown) => (typeof value === "string" ? value.trim() : "")) - .filter(Boolean) - : []; + const { + CONFIGURE_WIZARD_SECTIONS, + configureCommand, + configureCommandWithSections, + parseConfigureWizardSections, + } = await import("../commands/configure.js"); + const { sections, invalid } = parseConfigureWizardSections(opts.section); if (sections.length === 0) { await configureCommand(defaultRuntime); return; } - const invalid = sections.filter((s) => !CONFIGURE_WIZARD_SECTIONS.includes(s as never)); if (invalid.length > 0) { defaultRuntime.error( `Invalid --section: ${invalid.join(", ")}. Expected one of: ${CONFIGURE_WIZARD_SECTIONS.join(", ")}.`, diff --git a/src/cli/program/register.configure.ts b/src/cli/program/register.configure.ts index 223975689..62ba0e2b5 100644 --- a/src/cli/program/register.configure.ts +++ b/src/cli/program/register.configure.ts @@ -3,6 +3,7 @@ import { CONFIGURE_WIZARD_SECTIONS, configureCommand, configureCommandWithSections, + parseConfigureWizardSections, } from "../../commands/configure.js"; import { defaultRuntime } from "../../runtime.js"; import { formatDocsLink } from "../../terminal/links.js"; @@ -26,17 +27,12 @@ export function registerConfigureCommand(program: Command) { ) .action(async (opts) => { await runCommandWithRuntime(defaultRuntime, async () => { - const sections: string[] = Array.isArray(opts.section) - ? opts.section - .map((value: unknown) => (typeof value === "string" ? value.trim() : "")) - .filter(Boolean) - : []; + const { sections, invalid } = parseConfigureWizardSections(opts.section); if (sections.length === 0) { await configureCommand(defaultRuntime); return; } - const invalid = sections.filter((s) => !CONFIGURE_WIZARD_SECTIONS.includes(s as never)); if (invalid.length > 0) { defaultRuntime.error( `Invalid --section: ${invalid.join(", ")}. Expected one of: ${CONFIGURE_WIZARD_SECTIONS.join(", ")}.`, diff --git a/src/commands/configure.shared.ts b/src/commands/configure.shared.ts index 9377c06f4..4b74bc5c3 100644 --- a/src/commands/configure.shared.ts +++ b/src/commands/configure.shared.ts @@ -20,6 +20,24 @@ export const CONFIGURE_WIZARD_SECTIONS = [ export type WizardSection = (typeof CONFIGURE_WIZARD_SECTIONS)[number]; +export function parseConfigureWizardSections(raw: unknown): { + sections: WizardSection[]; + invalid: string[]; +} { + const sectionsRaw: string[] = Array.isArray(raw) + ? raw.map((value: unknown) => (typeof value === "string" ? value.trim() : "")).filter(Boolean) + : []; + if (sectionsRaw.length === 0) { + return { sections: [], invalid: [] }; + } + + const invalid = sectionsRaw.filter((s) => !CONFIGURE_WIZARD_SECTIONS.includes(s as never)); + const sections = sectionsRaw.filter((s): s is WizardSection => + CONFIGURE_WIZARD_SECTIONS.includes(s as never), + ); + return { sections, invalid }; +} + export type ChannelsWizardMode = "configure" | "remove"; export type ConfigureWizardParams = { diff --git a/src/commands/configure.ts b/src/commands/configure.ts index 5381e272a..86c539865 100644 --- a/src/commands/configure.ts +++ b/src/commands/configure.ts @@ -1,4 +1,8 @@ export { configureCommand, configureCommandWithSections } from "./configure.commands.js"; export { buildGatewayAuthConfig } from "./configure.gateway-auth.js"; -export { CONFIGURE_WIZARD_SECTIONS, type WizardSection } from "./configure.shared.js"; +export { + CONFIGURE_WIZARD_SECTIONS, + parseConfigureWizardSections, + type WizardSection, +} from "./configure.shared.js"; export { runConfigureWizard } from "./configure.wizard.js";