forked from MarvelSQ/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CLI keyboard shortcuts (vitejs#11228)
- Loading branch information
1 parent
59018d3
commit 5755244
Showing
3 changed files
with
175 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import colors from 'picocolors' | ||
import type { ViteDevServer } from './server' | ||
import { openBrowser } from './server/openBrowser' | ||
import { isDefined } from './utils' | ||
|
||
export type BindShortcutsOptions = { | ||
/** | ||
* Print a one line hint to the terminal. | ||
*/ | ||
print?: boolean | ||
customShortcuts?: (CLIShortcut | undefined | null)[] | ||
} | ||
|
||
export type CLIShortcut = { | ||
key: string | ||
description: string | ||
action(server: ViteDevServer): void | Promise<void> | ||
} | ||
|
||
export function bindShortcuts( | ||
server: ViteDevServer, | ||
opts: BindShortcutsOptions, | ||
): void { | ||
if (!server.httpServer) return | ||
server._shortcutsOptions = opts | ||
|
||
if (opts.print) { | ||
server.config.logger.info( | ||
colors.dim(colors.green(' ➜')) + | ||
colors.dim(' press ') + | ||
colors.bold('h') + | ||
colors.dim(' to show help'), | ||
) | ||
} | ||
|
||
const shortcuts = (opts.customShortcuts ?? []) | ||
.filter(isDefined) | ||
.concat(BASE_SHORTCUTS) | ||
|
||
let actionRunning = false | ||
|
||
const onInput = async (input: string) => { | ||
// ctrl+c or ctrl+d | ||
if (input === '\x03' || input === '\x04') { | ||
process.emit('SIGTERM') | ||
return | ||
} | ||
|
||
if (actionRunning) return | ||
|
||
if (input === 'h') { | ||
server.config.logger.info( | ||
shortcuts | ||
.map( | ||
(shortcut) => | ||
colors.dim(' press ') + | ||
colors.bold(shortcut.key) + | ||
colors.dim(` to ${shortcut.description}`), | ||
) | ||
.join('\n'), | ||
) | ||
} | ||
|
||
const shortcut = shortcuts.find((shortcut) => shortcut.key === input) | ||
if (!shortcut) return | ||
|
||
actionRunning = true | ||
await shortcut.action(server) | ||
actionRunning = false | ||
} | ||
|
||
if (process.stdin.isTTY) { | ||
process.stdin.setRawMode(true) | ||
} | ||
|
||
process.stdin.on('data', onInput).setEncoding('utf8').resume() | ||
|
||
server.httpServer.on('close', () => { | ||
process.stdin.off('data', onInput).pause() | ||
}) | ||
} | ||
|
||
const BASE_SHORTCUTS: CLIShortcut[] = [ | ||
{ | ||
key: 'r', | ||
description: 'restart the server', | ||
async action(server) { | ||
await server.restart() | ||
}, | ||
}, | ||
{ | ||
key: 'o', | ||
description: 'open in browser', | ||
action(server) { | ||
const url = server.resolvedUrls?.local[0] | ||
|
||
if (!url) { | ||
server.config.logger.warn('No URL available to open in browser') | ||
return | ||
} | ||
|
||
openBrowser(url, true, server.config.logger) | ||
}, | ||
}, | ||
{ | ||
key: 'q', | ||
description: 'quit', | ||
async action(server) { | ||
await server.close().finally(() => process.exit()) | ||
}, | ||
}, | ||
] |