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

@@ -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) },
),
});
}