Skip to content

Commit 7d313e1

Browse files
Fix blank project name prompt submit (#472)
1 parent 6ea4076 commit 7d313e1

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

.changeset/quick-humans-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/cli': patch
3+
---
4+
5+
Fix blank project name submissions in interactive create prompts.

packages/cli/src/ui-prompts.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,16 @@ export async function selectInstall(): Promise<boolean> {
6767
export async function getProjectName(): Promise<string> {
6868
const value = await text({
6969
message: 'Project name (leave empty to use current directory)',
70+
// Clack prints `undefined` on blank submit when placeholder is omitted.
71+
placeholder: '',
7072
validate(value) {
71-
if (isCurrentDirectoryProjectNameInput(value)) {
73+
const projectName = value ?? ''
74+
75+
if (isCurrentDirectoryProjectNameInput(projectName)) {
7276
return
7377
}
7478

75-
const { valid, error } = validateProjectName(value)
79+
const { valid, error } = validateProjectName(projectName)
7680
if (!valid) {
7781
return error
7882
}
@@ -84,7 +88,7 @@ export async function getProjectName(): Promise<string> {
8488
process.exit(0)
8589
}
8690

87-
return value.trim()
91+
return (value ?? '').trim()
8892
}
8993

9094
export async function selectPackageManager(): Promise<PackageManager> {

packages/cli/tests/ui-prompts.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,26 @@ describe('getProjectName', () => {
4343
expect(textOptions.message).toBe(
4444
'Project name (leave empty to use current directory)',
4545
)
46-
expect(textOptions.placeholder).toBeUndefined()
46+
expect(textOptions.placeholder).toBe('')
4747
expect(textOptions.validate?.('')).toBeUndefined()
4848
expect(textOptions.validate?.('.')).toBeUndefined()
4949
})
5050

51+
it('should handle undefined project name values as current directory creation', async () => {
52+
const textSpy = vi
53+
.spyOn(clack, 'text')
54+
.mockImplementation(async () => undefined as unknown as string)
55+
vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
56+
57+
const projectName = await getProjectName()
58+
const textOptions = textSpy.mock.calls[0]![0] as {
59+
validate?: (value?: string) => string | undefined
60+
}
61+
62+
expect(projectName).toBe('')
63+
expect(textOptions.validate?.(undefined)).toBeUndefined()
64+
})
65+
5166
it('should exit on cancel', async () => {
5267
vi.spyOn(clack, 'text').mockImplementation(async () => 'Cancelled')
5368
vi.spyOn(clack, 'isCancel').mockImplementation(() => true)

0 commit comments

Comments
 (0)