Skip to content

Commit 0ae2e1d

Browse files
authored
refactor(shortcuts)!: tweak shortcuts api (#14749)
1 parent c3622d7 commit 0ae2e1d

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

packages/vite/src/node/cli.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { cac } from 'cac'
55
import colors from 'picocolors'
66
import type { BuildOptions } from './build'
77
import type { ServerOptions } from './server'
8+
import type { CLIShortcut } from './shortcuts'
89
import type { LogLevel } from './logger'
910
import { createLogger } from './logger'
1011
import { VERSION } from './constants'
@@ -192,34 +193,33 @@ cli
192193
)
193194

194195
server.printUrls()
195-
server.bindCLIShortcuts({
196-
print: true,
197-
customShortcuts: [
198-
profileSession && {
199-
key: 'p',
200-
description: 'start/stop the profiler',
201-
async action(server) {
202-
if (profileSession) {
203-
await stopProfiler(server.config.logger.info)
204-
} else {
205-
const inspector = await import('node:inspector').then(
206-
(r) => r.default,
207-
)
208-
await new Promise<void>((res) => {
209-
profileSession = new inspector.Session()
210-
profileSession.connect()
211-
profileSession.post('Profiler.enable', () => {
212-
profileSession!.post('Profiler.start', () => {
213-
server.config.logger.info('Profiler started')
214-
res()
215-
})
196+
const customShortcuts: CLIShortcut<typeof server>[] = []
197+
if (profileSession) {
198+
customShortcuts.push({
199+
key: 'p',
200+
description: 'start/stop the profiler',
201+
async action(server) {
202+
if (profileSession) {
203+
await stopProfiler(server.config.logger.info)
204+
} else {
205+
const inspector = await import('node:inspector').then(
206+
(r) => r.default,
207+
)
208+
await new Promise<void>((res) => {
209+
profileSession = new inspector.Session()
210+
profileSession.connect()
211+
profileSession.post('Profiler.enable', () => {
212+
profileSession!.post('Profiler.start', () => {
213+
server.config.logger.info('Profiler started')
214+
res()
216215
})
217216
})
218-
}
219-
},
217+
})
218+
}
220219
},
221-
],
222-
})
220+
})
221+
}
222+
server.bindCLIShortcuts({ print: true, customShortcuts })
223223
} catch (e) {
224224
const logger = createLogger(options.logLevel)
225225
logger.error(colors.red(`error when starting dev server:\n${e.stack}`), {

packages/vite/src/node/shortcuts.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import readline from 'node:readline'
22
import colors from 'picocolors'
33
import type { ViteDevServer } from './server'
4-
import { isDefined } from './utils'
54
import type { PreviewServer } from './preview'
65
import { openBrowser } from './server/openBrowser'
76

87
export 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

1619
export 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

Comments
 (0)