11import readline from 'node:readline'
22import colors from 'picocolors'
33import type { ViteDevServer } from './server'
4- import { isDefined } from './utils'
54import type { PreviewServer } from './preview'
65import { openBrowser } from './server/openBrowser'
76
87export type BindCLIShortcutsOptions < Server = ViteDevServer | PreviewServer > = {
98 /**
10- * Print a one line hint to the terminal.
9+ * Print a one- line shortcuts "help" hint to the terminal
1110 */
1211 print ?: boolean
13- customShortcuts ?: ( CLIShortcut < Server > | undefined | null ) [ ]
12+ /**
13+ * Custom shortcuts to run when a key is pressed. These shortcuts take priority
14+ * over the default shortcuts if they have the same keys (except the `h` key).
15+ */
16+ customShortcuts ?: CLIShortcut < Server > [ ]
1417}
1518
1619export type CLIShortcut < Server = ViteDevServer | PreviewServer > = {
@@ -43,7 +46,6 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
4346 }
4447
4548 const shortcuts = ( opts ?. customShortcuts ?? [ ] )
46- . filter ( isDefined )
4749 // @ts -expect-error passing the right types, but typescript can't detect it
4850 . concat ( isDev ? BASE_DEV_SHORTCUTS : BASE_PREVIEW_SHORTCUTS )
4951
@@ -53,18 +55,21 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
5355 if ( actionRunning ) return
5456
5557 if ( input === 'h' ) {
56- server . config . logger . info (
57- [
58- '' ,
59- colors . bold ( ' Shortcuts' ) ,
60- ...shortcuts . map (
61- ( shortcut ) =>
62- colors . dim ( ' press ' ) +
63- colors . bold ( `${ shortcut . key } + enter` ) +
64- colors . dim ( ` to ${ shortcut . description } ` ) ,
65- ) ,
66- ] . join ( '\n' ) ,
67- )
58+ const loggedKeys = new Set < string > ( )
59+ server . config . logger . info ( '\n Shortcuts' )
60+
61+ for ( const shortcut of shortcuts ) {
62+ if ( loggedKeys . has ( shortcut . key ) ) continue
63+ loggedKeys . add ( shortcut . key )
64+
65+ server . config . logger . info (
66+ colors . dim ( ' press ' ) +
67+ colors . bold ( `${ shortcut . key } + enter` ) +
68+ colors . dim ( ` to ${ shortcut . description } ` ) ,
69+ )
70+ }
71+
72+ return
6873 }
6974
7075 const shortcut = shortcuts . find ( ( shortcut ) => shortcut . key === input )
0 commit comments