Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/big-pants-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/prompts": patch
---

Fix the `path` prompt so `directory: true` correctly enforces directory-only selection while still allowing directory navigation, and add regression tests for both directory and default file selection behavior.
2 changes: 1 addition & 1 deletion packages/prompts/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const path = (opts: PathOptions) => {
})
.filter(
({ path, isDirectory }) =>
path.startsWith(userInput) && (opts.directory || !isDirectory)
path.startsWith(userInput) && (isDirectory || !opts.directory)
);
return items.map((item) => ({
value: item.path,
Expand Down
80 changes: 60 additions & 20 deletions packages/prompts/test/__snapshots__/path.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ exports[`text (isCI = false) > can cancel 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=1>",
"<erase.down>",
"■ foo
Expand All @@ -30,10 +32,12 @@ exports[`text (isCI = false) > cannot submit unknown value 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/_█
Expand All @@ -57,10 +61,12 @@ exports[`text (isCI = false) > cannot submit unknown value 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/b█
Expand Down Expand Up @@ -107,10 +113,12 @@ exports[`text (isCI = false) > renders cancelled value if one set 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/x█
Expand Down Expand Up @@ -141,10 +149,12 @@ exports[`text (isCI = false) > renders message 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo
Expand All @@ -163,10 +173,12 @@ exports[`text (isCI = false) > renders submitted value 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/b█
Expand Down Expand Up @@ -197,10 +209,12 @@ exports[`text (isCI = false) > validation errors render and clear (using Error)
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/r█
Expand All @@ -224,10 +238,12 @@ exports[`text (isCI = false) > validation errors render and clear (using Error)
│
│ Search: /tmp/█
│ ○ /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ● /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/b█
Expand All @@ -253,10 +269,12 @@ exports[`text (isCI = false) > validation errors render and clear 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/r█
Expand All @@ -280,10 +298,12 @@ exports[`text (isCI = false) > validation errors render and clear 1`] = `
│
│ Search: /tmp/█
│ ○ /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ● /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/b█
Expand All @@ -309,10 +329,12 @@ exports[`text (isCI = true) > can cancel 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=1>",
"<erase.down>",
"■ foo
Expand All @@ -331,10 +353,12 @@ exports[`text (isCI = true) > cannot submit unknown value 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/_█
Expand All @@ -358,10 +382,12 @@ exports[`text (isCI = true) > cannot submit unknown value 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/b█
Expand Down Expand Up @@ -408,10 +434,12 @@ exports[`text (isCI = true) > renders cancelled value if one set 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/x█
Expand Down Expand Up @@ -442,10 +470,12 @@ exports[`text (isCI = true) > renders message 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo
Expand All @@ -464,10 +494,12 @@ exports[`text (isCI = true) > renders submitted value 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/b█
Expand Down Expand Up @@ -498,10 +530,12 @@ exports[`text (isCI = true) > validation errors render and clear (using Error) 1
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/r█
Expand All @@ -525,10 +559,12 @@ exports[`text (isCI = true) > validation errors render and clear (using Error) 1
│
│ Search: /tmp/█
│ ○ /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ● /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/b█
Expand All @@ -554,10 +590,12 @@ exports[`text (isCI = true) > validation errors render and clear 1`] = `
│
│ Search: /tmp/█
│ ● /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ○ /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/r█
Expand All @@ -581,10 +619,12 @@ exports[`text (isCI = true) > validation errors render and clear 1`] = `
│
│ Search: /tmp/█
│ ○ /tmp/bar
│ ○ /tmp/foo
│ ○ /tmp/hello
│ ● /tmp/root.zip
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: /tmp/b█
Expand Down
36 changes: 36 additions & 0 deletions packages/prompts/test/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,42 @@ describe.each(['true', 'false'])('text (isCI = %s)', (isCI) => {
expect(output.buffer).toMatchSnapshot();
});

test('directory mode only allows selecting directories', async () => {
const result = prompts.path({
message: 'foo',
root: '/tmp/',
directory: true,
input,
output,
});

input.emit('keypress', 'f', { name: 'f' });
input.emit('keypress', '', { name: 'return' });

const value = await result;

expect(value).toBe('/tmp/foo');
});

test('default mode allows selecting files', async () => {
const result = prompts.path({
message: 'foo',
root: '/tmp/',
input,
output,
});

input.emit('keypress', 'r', { name: 'r' });
input.emit('keypress', 'o', { name: 'o' });
input.emit('keypress', 'o', { name: 'o' });
input.emit('keypress', 't', { name: 't' });
input.emit('keypress', '', { name: 'return' });

const value = await result;

expect(value).toBe('/tmp/root.zip');
});

test('validation errors render and clear', async () => {
const result = prompts.path({
message: 'foo',
Expand Down