-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
feat: add CLI keyboard shortcuts #11228
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a29cce
feat: add CLI keyboard shortcuts
ArnaudBarre 0f18233
fix: additionalShortCuts -> customShortcuts
ArnaudBarre 70f006c
chore: use ??=
ArnaudBarre 023f47d
fix: drop toggle hmr shortcut
ArnaudBarre 3647c78
feat: restart profiler
ArnaudBarre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) | ||
}, | ||
}, | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The node docs showed that it's commonly emitted as
SIGINT
for all platforms. Seems likeSIGTERM
isn't supported in Windows too. Should we change this toSIGINT
?With that said we only listen for
SIGTERM
for the server today (not sure if that has been an issue in Windows), but I think it's nice to follow the default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will test if it's still exit cleanly on mac os with SIGINT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't close at all when emitting SIGINT.
Doing
await server.close().finally(() => process.exit())
works fine, but maybe not perfect for middleware mode