1
1
import readline from 'node:readline'
2
2
import colors from 'picocolors'
3
3
import type { ViteDevServer } from './server'
4
- import { isDefined } from './utils'
5
4
import type { PreviewServer } from './preview'
6
5
import { openBrowser } from './server/openBrowser'
7
6
8
7
export type BindCLIShortcutsOptions < Server = ViteDevServer | PreviewServer > = {
9
8
/**
10
- * Print a one line hint to the terminal.
9
+ * Print a one- line shortcuts "help" hint to the terminal
11
10
*/
12
11
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 > [ ]
14
17
}
15
18
16
19
export type CLIShortcut < Server = ViteDevServer | PreviewServer > = {
@@ -43,7 +46,6 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
43
46
}
44
47
45
48
const shortcuts = ( opts ?. customShortcuts ?? [ ] )
46
- . filter ( isDefined )
47
49
// @ts -expect-error passing the right types, but typescript can't detect it
48
50
. concat ( isDev ? BASE_DEV_SHORTCUTS : BASE_PREVIEW_SHORTCUTS )
49
51
@@ -53,18 +55,21 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
53
55
if ( actionRunning ) return
54
56
55
57
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
68
73
}
69
74
70
75
const shortcut = shortcuts . find ( ( shortcut ) => shortcut . key === input )
0 commit comments