refactor(cli): share styled select prompt helper

This commit is contained in:
Peter Steinberger
2026-02-18 17:34:38 +00:00
parent 8b48e0c615
commit 005e1d5fd1
4 changed files with 68 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
import { confirm, isCancel, select } from "@clack/prompts";
import { confirm, isCancel } from "@clack/prompts";
import { readConfigFileSnapshot } from "../../config/config.js";
import {
formatUpdateChannelLabel,
@@ -7,7 +7,8 @@ import {
} from "../../infra/update-channels.js";
import { checkUpdateStatus } from "../../infra/update-check.js";
import { defaultRuntime } from "../../runtime.js";
import { stylePromptHint, stylePromptMessage } from "../../terminal/prompt-style.js";
import { selectStyled } from "../../terminal/prompt-select-styled.js";
import { stylePromptMessage } from "../../terminal/prompt-style.js";
import { theme } from "../../terminal/theme.js";
import { pathExists } from "../../utils.js";
import {
@@ -19,15 +20,6 @@ import {
} from "./shared.js";
import { updateCommand } from "./update-command.js";
const selectStyled = <T>(params: Parameters<typeof select<T>>[0]) =>
select({
...params,
message: stylePromptMessage(params.message),
options: params.options.map((opt) =>
opt.hint === undefined ? opt : { ...opt, hint: stylePromptHint(opt.hint) },
),
});
export async function updateWizardCommand(opts: UpdateWizardOptions = {}): Promise<void> {
if (!process.stdin.isTTY) {
defaultRuntime.error(

View File

@@ -1,9 +1,10 @@
import { cancel, confirm, isCancel, select } from "@clack/prompts";
import { cancel, confirm, isCancel } from "@clack/prompts";
import { formatCliCommand } from "../cli/command-format.js";
import { isNixMode } from "../config/config.js";
import { resolveGatewayService } from "../daemon/service.js";
import type { RuntimeEnv } from "../runtime.js";
import { stylePromptHint, stylePromptMessage, stylePromptTitle } from "../terminal/prompt-style.js";
import { selectStyled } from "../terminal/prompt-select-styled.js";
import { stylePromptMessage, stylePromptTitle } from "../terminal/prompt-style.js";
import { resolveCleanupPlanFromDisk } from "./cleanup-plan.js";
import { listAgentSessionDirs, removePath } from "./cleanup-utils.js";
@@ -16,15 +17,6 @@ export type ResetOptions = {
dryRun?: boolean;
};
const selectStyled = <T>(params: Parameters<typeof select<T>>[0]) =>
select({
...params,
message: stylePromptMessage(params.message),
options: params.options.map((opt) =>
opt.hint === undefined ? opt : { ...opt, hint: stylePromptHint(opt.hint) },
),
});
async function stopGatewayIfRunning(runtime: RuntimeEnv) {
if (isNixMode) {
return;

View File

@@ -0,0 +1,50 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const { selectMock, stylePromptMessageMock, stylePromptHintMock } = vi.hoisted(() => ({
selectMock: vi.fn(),
stylePromptMessageMock: vi.fn((value: string) => `msg:${value}`),
stylePromptHintMock: vi.fn((value: string) => `hint:${value}`),
}));
vi.mock("@clack/prompts", () => ({
select: selectMock,
}));
vi.mock("./prompt-style.js", () => ({
stylePromptMessage: stylePromptMessageMock,
stylePromptHint: stylePromptHintMock,
}));
import { selectStyled } from "./prompt-select-styled.js";
describe("selectStyled", () => {
beforeEach(() => {
selectMock.mockReset();
stylePromptMessageMock.mockClear();
stylePromptHintMock.mockClear();
});
it("styles message and option hints before delegating to clack select", () => {
const expected = Symbol("selected");
selectMock.mockReturnValue(expected);
const result = selectStyled({
message: "Pick channel",
options: [
{ value: "stable", label: "Stable", hint: "Tagged releases" },
{ value: "dev", label: "Dev" },
],
});
expect(result).toBe(expected);
expect(stylePromptMessageMock).toHaveBeenCalledWith("Pick channel");
expect(stylePromptHintMock).toHaveBeenCalledWith("Tagged releases");
expect(selectMock).toHaveBeenCalledWith({
message: "msg:Pick channel",
options: [
{ value: "stable", label: "Stable", hint: "hint:Tagged releases" },
{ value: "dev", label: "Dev" },
],
});
});
});

View File

@@ -0,0 +1,12 @@
import { select } from "@clack/prompts";
import { stylePromptHint, stylePromptMessage } from "./prompt-style.js";
export function selectStyled<T>(params: Parameters<typeof select<T>>[0]) {
return select({
...params,
message: stylePromptMessage(params.message),
options: params.options.map((opt) =>
opt.hint === undefined ? opt : { ...opt, hint: stylePromptHint(opt.hint) },
),
});
}