diff --git a/packages/core/src/server/cliShortcuts.ts b/packages/core/src/server/cliShortcuts.ts index 3b8c502125..9a70ecf6ef 100644 --- a/packages/core/src/server/cliShortcuts.ts +++ b/packages/core/src/server/cliShortcuts.ts @@ -5,6 +5,11 @@ import type { CliShortcut, NormalizedConfig } from '../types/config'; export const isCliShortcutsEnabled = (config: NormalizedConfig): boolean => config.dev.cliShortcuts && isTTY('stdin'); +// Normalize user input so shortcuts are case-insensitive +// and still work with accidental surrounding whitespace. +export const normalizeShortcutInput = (input: string): string => + input.trim().toLowerCase(); + export async function setupCliShortcuts({ help = true, openPage, @@ -84,6 +89,8 @@ export async function setupCliShortcuts({ }); rl.on('line', (input) => { + input = normalizeShortcutInput(input); + if (input === 'h') { let message = `\n ${color.bold(color.blue('Shortcuts:'))}\n`; for (const shortcut of shortcuts) { diff --git a/packages/core/tests/cliShortcuts.test.ts b/packages/core/tests/cliShortcuts.test.ts new file mode 100644 index 0000000000..6cdf31bfa9 --- /dev/null +++ b/packages/core/tests/cliShortcuts.test.ts @@ -0,0 +1,8 @@ +import { normalizeShortcutInput } from '../src/server/cliShortcuts'; + +test('normalizeShortcutInput', () => { + expect(normalizeShortcutInput('h')).toBe('h'); + expect(normalizeShortcutInput(' H ')).toBe('h'); + expect(normalizeShortcutInput('\to\t')).toBe('o'); + expect(normalizeShortcutInput(' Q')).toBe('q'); +});