Skip to content

fix: prompt for values when auto is off, regardless of a default value #1107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/shared/options/getPrefillOrPromptedOption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ vi.mock("@clack/prompts", () => ({
}));

describe("getPrefillOrPromptedValue", () => {
it("returns the placeholder when auto is true and it exists", async () => {
it("returns the provided when it exists", async () => {
const value = "Test Value";

const actual = await getPrefillOrPromptedOption({
auto: true,
getDefaultValue: vi.fn().mockResolvedValue("default value"),
message: "Input message.",
name: "field",
provided: value,
});

expect(actual).toEqual({ error: undefined, value });
});

it("returns the default value when auto is true and it exists", async () => {
const value = "Test Value";

const actual = await getPrefillOrPromptedOption({
Expand All @@ -26,7 +40,7 @@ describe("getPrefillOrPromptedValue", () => {
expect(actual).toEqual({ error: undefined, value });
});

it("returns an error when auto is true and no placeholder exists", async () => {
it("returns an error when auto is true and no default value exists", async () => {
const actual = await getPrefillOrPromptedOption({
auto: true,
getDefaultValue: vi.fn().mockResolvedValue(undefined),
Expand All @@ -52,22 +66,22 @@ describe("getPrefillOrPromptedValue", () => {
});
});

it("provides the placeholder's awaited return when a placeholder function is provided and auto is false", async () => {
it("prompts with the default value as a placeholder when a placeholder function is provided and auto is false", async () => {
const message = "Test message";
const placeholder = "Test placeholder";

const actual = await getPrefillOrPromptedOption({
await getPrefillOrPromptedOption({
auto: false,
getDefaultValue: vi.fn().mockResolvedValue(placeholder),
message,
name: "field",
});

expect(actual).toEqual({
error: undefined,
value: placeholder,
expect(mockText).toHaveBeenCalledWith({
message,
placeholder,
validate: expect.any(Function),
});
expect(mockText).not.toHaveBeenCalled();
});

it("validates entered text when it's not blank and auto is false", async () => {
Expand Down
8 changes: 7 additions & 1 deletion src/shared/options/getPrefillOrPromptedOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ export interface GetPrefillOrPromptedOptionOptions {
getDefaultValue?: () => Promise<string | undefined>;
message: string;
name: string;
provided?: string | undefined;
}

export async function getPrefillOrPromptedOption({
auto,
getDefaultValue,
message,
name,
provided,
}: GetPrefillOrPromptedOptionOptions) {
if (provided) {
return { value: provided };
}

const defaultValue = await getDefaultValue?.();

if (auto || defaultValue) {
if (auto) {
return {
error: defaultValue
? undefined
Expand Down
17 changes: 8 additions & 9 deletions src/shared/options/readOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ export async function readOptions(

const ownerOption = await getPrefillOrPromptedOption({
auto: !!mappedOptions.auto,
getDefaultValue: async () => options.owner ?? (await defaults.owner()),
getDefaultValue: defaults.owner,
message: "What organization or user will the repository be under?",
name: "owner",
provided: options.owner,
});

options.owner ??= ownerOption.value;
Expand All @@ -141,10 +142,10 @@ export async function readOptions(

const repositoryOption = await getPrefillOrPromptedOption({
auto: !!mappedOptions.auto,
getDefaultValue: async () =>
options.repository ?? (await defaults.repository()),
getDefaultValue: defaults.repository,
message: "What will the kebab-case name of the repository be?",
name: "repository",
provided: options.repository,
});

options.repository ??= repositoryOption.value;
Expand Down Expand Up @@ -175,11 +176,10 @@ export async function readOptions(
const descriptionOption = await getPrefillOrPromptedOption({
auto: !!mappedOptions.auto,
getDefaultValue: async () =>
options.description ??
(await defaults.description()) ??
"A very lovely package. Hooray!",
(await defaults.description()) ?? "A very lovely package. Hooray!",
message: "How would you describe the new package?",
name: "description",
provided: options.description,
});

options.description ??= descriptionOption.value;
Expand All @@ -191,11 +191,10 @@ export async function readOptions(
const titleOption = await getPrefillOrPromptedOption({
auto: !!mappedOptions.auto,
getDefaultValue: async () =>
options.title ??
(await defaults.title()) ??
titleCase(repository).replaceAll("-", " "),
(await defaults.title()) ?? titleCase(repository).replaceAll("-", " "),
message: "What will the Title Case title of the repository be?",
name: "title",
provided: options.title,
});

options.title ??= titleOption.value;
Expand Down