Skip to content

Commit 127108f

Browse files
fix: prompt for values when auto is off, regardless of a default value (#1107)
## PR Checklist - [x] Addresses an existing open issue: fixes #1106 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview A default value being present for `getPrefillOrPromptedValue` doesn't mean it should be used. Only a _provided_ value does. This adds an explicit `provided?: string` to the function's options.
1 parent e7198e6 commit 127108f

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/shared/options/getPrefillOrPromptedOption.test.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ vi.mock("@clack/prompts", () => ({
1313
}));
1414

1515
describe("getPrefillOrPromptedValue", () => {
16-
it("returns the placeholder when auto is true and it exists", async () => {
16+
it("returns the provided when it exists", async () => {
17+
const value = "Test Value";
18+
19+
const actual = await getPrefillOrPromptedOption({
20+
auto: true,
21+
getDefaultValue: vi.fn().mockResolvedValue("default value"),
22+
message: "Input message.",
23+
name: "field",
24+
provided: value,
25+
});
26+
27+
expect(actual).toEqual({ error: undefined, value });
28+
});
29+
30+
it("returns the default value when auto is true and it exists", async () => {
1731
const value = "Test Value";
1832

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

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

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

59-
const actual = await getPrefillOrPromptedOption({
73+
await getPrefillOrPromptedOption({
6074
auto: false,
6175
getDefaultValue: vi.fn().mockResolvedValue(placeholder),
6276
message,
6377
name: "field",
6478
});
6579

66-
expect(actual).toEqual({
67-
error: undefined,
68-
value: placeholder,
80+
expect(mockText).toHaveBeenCalledWith({
81+
message,
82+
placeholder,
83+
validate: expect.any(Function),
6984
});
70-
expect(mockText).not.toHaveBeenCalled();
7185
});
7286

7387
it("validates entered text when it's not blank and auto is false", async () => {

src/shared/options/getPrefillOrPromptedOption.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ export interface GetPrefillOrPromptedOptionOptions {
77
getDefaultValue?: () => Promise<string | undefined>;
88
message: string;
99
name: string;
10+
provided?: string | undefined;
1011
}
1112

1213
export async function getPrefillOrPromptedOption({
1314
auto,
1415
getDefaultValue,
1516
message,
1617
name,
18+
provided,
1719
}: GetPrefillOrPromptedOptionOptions) {
20+
if (provided) {
21+
return { value: provided };
22+
}
23+
1824
const defaultValue = await getDefaultValue?.();
1925

20-
if (auto || defaultValue) {
26+
if (auto) {
2127
return {
2228
error: defaultValue
2329
? undefined

src/shared/options/readOptions.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ export async function readOptions(
124124

125125
const ownerOption = await getPrefillOrPromptedOption({
126126
auto: !!mappedOptions.auto,
127-
getDefaultValue: async () => options.owner ?? (await defaults.owner()),
127+
getDefaultValue: defaults.owner,
128128
message: "What organization or user will the repository be under?",
129129
name: "owner",
130+
provided: options.owner,
130131
});
131132

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

142143
const repositoryOption = await getPrefillOrPromptedOption({
143144
auto: !!mappedOptions.auto,
144-
getDefaultValue: async () =>
145-
options.repository ?? (await defaults.repository()),
145+
getDefaultValue: defaults.repository,
146146
message: "What will the kebab-case name of the repository be?",
147147
name: "repository",
148+
provided: options.repository,
148149
});
149150

150151
options.repository ??= repositoryOption.value;
@@ -175,11 +176,10 @@ export async function readOptions(
175176
const descriptionOption = await getPrefillOrPromptedOption({
176177
auto: !!mappedOptions.auto,
177178
getDefaultValue: async () =>
178-
options.description ??
179-
(await defaults.description()) ??
180-
"A very lovely package. Hooray!",
179+
(await defaults.description()) ?? "A very lovely package. Hooray!",
181180
message: "How would you describe the new package?",
182181
name: "description",
182+
provided: options.description,
183183
});
184184

185185
options.description ??= descriptionOption.value;
@@ -191,11 +191,10 @@ export async function readOptions(
191191
const titleOption = await getPrefillOrPromptedOption({
192192
auto: !!mappedOptions.auto,
193193
getDefaultValue: async () =>
194-
options.title ??
195-
(await defaults.title()) ??
196-
titleCase(repository).replaceAll("-", " "),
194+
(await defaults.title()) ?? titleCase(repository).replaceAll("-", " "),
197195
message: "What will the Title Case title of the repository be?",
198196
name: "title",
197+
provided: options.title,
199198
});
200199

201200
options.title ??= titleOption.value;

0 commit comments

Comments
 (0)