Skip to content

Commit

Permalink
feat(server): allow disabling built-in shortcuts (#15218)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Dec 2, 2023
1 parent 73e971f commit 7fd7c6c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/vite/src/node/shortcuts.ts
Expand Up @@ -14,14 +14,15 @@ export type BindCLIShortcutsOptions<Server = ViteDevServer | PreviewServer> = {
/**
* Custom shortcuts to run when a key is pressed. These shortcuts take priority
* over the default shortcuts if they have the same keys (except the `h` key).
* To disable a default shortcut, define the same key but with `action: undefined`.
*/
customShortcuts?: CLIShortcut<Server>[]
}

export type CLIShortcut<Server = ViteDevServer | PreviewServer> = {
key: string
description: string
action(server: Server): void | Promise<void>
action?(server: Server): void | Promise<void>
}

export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
Expand Down Expand Up @@ -66,6 +67,8 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
if (loggedKeys.has(shortcut.key)) continue
loggedKeys.add(shortcut.key)

if (shortcut.action == null) continue

server.config.logger.info(
colors.dim(' press ') +
colors.bold(`${shortcut.key} + enter`) +
Expand All @@ -77,7 +80,7 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
}

const shortcut = shortcuts.find((shortcut) => shortcut.key === input)
if (!shortcut) return
if (!shortcut || shortcut.action == null) return

actionRunning = true
await shortcut.action(server)
Expand Down

0 comments on commit 7fd7c6c

Please sign in to comment.