Skip to content
Closed
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
28 changes: 18 additions & 10 deletions packages/vite/src/node/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ export type CLIShortcut<Server = ViteDevServer | PreviewServer> = {
action?(server: Server): void | Promise<void>
}

let rl: readline.Interface
const shortcuts: CLIShortcut[] = []
export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
server: Server,
opts?: BindCLIShortcutsOptions<Server>,
): void {
if (!server.httpServer || !process.stdin.isTTY || process.env.CI) {
return
}

const isDev = isDevServer(server)

if (isDev) {
Expand All @@ -48,17 +49,10 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
)
}

const shortcuts = (opts?.customShortcuts ?? []).concat(
(isDev
? BASE_DEV_SHORTCUTS
: BASE_PREVIEW_SHORTCUTS) as CLIShortcut<Server>[],
)

let actionRunning = false

const onInput = async (input: string) => {
if (actionRunning) return

if (input === 'h') {
const loggedKeys = new Set<string>()
server.config.logger.info('\n Shortcuts')
Expand Down Expand Up @@ -86,8 +80,22 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
await shortcut.action(server)
actionRunning = false
}

const rl = readline.createInterface({ input: process.stdin })
/**
* User may add custom shortcuts (see #15781)
* This will ensure that only one instance of readline is running
*/
if (rl) {
rl.close()
} else {
// No readline instance, push the default shortcuts
shortcuts.push(
...((isDev
? BASE_DEV_SHORTCUTS
: BASE_PREVIEW_SHORTCUTS) as CLIShortcut<Server>[]),
)
}
shortcuts.push(...(opts?.customShortcuts ?? []))
rl = readline.createInterface({ input: process.stdin })
rl.on('line', onInput)
server.httpServer.on('close', () => rl.close())
}
Expand Down