Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(shortcuts)!: tweak shortcuts api #14749

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { cac } from 'cac'
import colors from 'picocolors'
import type { BuildOptions } from './build'
import type { ServerOptions } from './server'
import type { CLIShortcut } from './shortcuts'
import type { LogLevel } from './logger'
import { createLogger } from './logger'
import { VERSION } from './constants'
Expand Down Expand Up @@ -192,34 +193,33 @@ cli
)

server.printUrls()
server.bindCLIShortcuts({
print: true,
customShortcuts: [
profileSession && {
key: 'p',
description: 'start/stop the profiler',
async action(server) {
if (profileSession) {
await stopProfiler(server.config.logger.info)
} else {
const inspector = await import('node:inspector').then(
(r) => r.default,
)
await new Promise<void>((res) => {
profileSession = new inspector.Session()
profileSession.connect()
profileSession.post('Profiler.enable', () => {
profileSession!.post('Profiler.start', () => {
server.config.logger.info('Profiler started')
res()
})
const customShortcuts: CLIShortcut<typeof server>[] = []
if (profileSession) {
customShortcuts.push({
key: 'p',
description: 'start/stop the profiler',
async action(server) {
if (profileSession) {
await stopProfiler(server.config.logger.info)
} else {
const inspector = await import('node:inspector').then(
(r) => r.default,
)
await new Promise<void>((res) => {
profileSession = new inspector.Session()
profileSession.connect()
profileSession.post('Profiler.enable', () => {
profileSession!.post('Profiler.start', () => {
server.config.logger.info('Profiler started')
res()
})
})
}
},
})
}
},
],
})
})
}
server.bindCLIShortcuts({ print: true, customShortcuts })
} catch (e) {
const logger = createLogger(options.logLevel)
logger.error(colors.red(`error when starting dev server:\n${e.stack}`), {
Expand Down
37 changes: 21 additions & 16 deletions packages/vite/src/node/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import readline from 'node:readline'
import colors from 'picocolors'
import type { ViteDevServer } from './server'
import { isDefined } from './utils'
import type { PreviewServer } from './preview'
import { openBrowser } from './server/openBrowser'

export type BindCLIShortcutsOptions<Server = ViteDevServer | PreviewServer> = {
/**
* Print a one line hint to the terminal.
* Print a one-line shortcuts "help" hint to the terminal
*/
print?: boolean
customShortcuts?: (CLIShortcut<Server> | undefined | null)[]
/**
* 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).
*/
customShortcuts?: CLIShortcut<Server>[]
}

export type CLIShortcut<Server = ViteDevServer | PreviewServer> = {
Expand Down Expand Up @@ -43,7 +46,6 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
}

const shortcuts = (opts?.customShortcuts ?? [])
.filter(isDefined)
// @ts-expect-error passing the right types, but typescript can't detect it
.concat(isDev ? BASE_DEV_SHORTCUTS : BASE_PREVIEW_SHORTCUTS)

Expand All @@ -53,18 +55,21 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
if (actionRunning) return

if (input === 'h') {
server.config.logger.info(
[
'',
colors.bold(' Shortcuts'),
...shortcuts.map(
(shortcut) =>
colors.dim(' press ') +
colors.bold(`${shortcut.key} + enter`) +
colors.dim(` to ${shortcut.description}`),
),
].join('\n'),
)
const loggedKeys = new Set<string>()
server.config.logger.info('\n Shortcuts')

for (const shortcut of shortcuts) {
if (loggedKeys.has(shortcut.key)) continue
loggedKeys.add(shortcut.key)

server.config.logger.info(
colors.dim(' press ') +
colors.bold(`${shortcut.key} + enter`) +
colors.dim(` to ${shortcut.description}`),
)
}

return
}

const shortcut = shortcuts.find((shortcut) => shortcut.key === input)
Expand Down