-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Conversation
5ceab38
to
bfece2e
Compare
cc @benmccann, @dominikg: What do you think about this? Is this ok for SvelteKit? |
SvelteKit doesn't really do anything special, but just has users call |
/ecosystem-ci run |
📝 Ran ecosystem CI: Open
|
This patch appears to add keyboard shortcut support to the Vite CLI. The main changes are the addition of a new file, shortcuts.ts, which exports a bindShortcuts function that binds the specified shortcuts to the CLI, and updates to the cli.ts file that use this function to bind the default set of shortcuts as well as an additional shortcut to stop the profiler, if it is running. One issue with this patch is that it uses the global object to store the Session object used by the profiler. This is not recommended, as it can lead to variable name collisions and other issues if multiple modules use the global object in this way. It would be better to pass the Session object as an argument to the stopProfiler function, rather than using the global object. Another issue is that the stopProfiler function takes a log function as an argument, but the implementation of the shortcut that calls this function does not pass a function. Instead, it passes the result of calling the info method on the server.config.logger object, which is not a function. This will cause an error at runtime. To fix this, the info method should be called within the stopProfiler function, rather than passing the method itself. Aside from these issues, the changes in this patch appear to be well-structured and well-written. Adding keyboard shortcut support is a useful feature, and the implementation looks correct. |
Looks like ChatGPT was 0/2 on its code review suggestions, which are both wrong |
// ctrl+c or ctrl+d | ||
if (input === '\x03' || input === '\x04') { | ||
process.emit('SIGTERM') | ||
return | ||
} |
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 like SIGTERM
isn't supported in Windows too. Should we change this to SIGINT
?
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
packages/vite/src/node/index.ts
Outdated
@@ -60,6 +60,7 @@ export type { | |||
LogType, | |||
LoggerOptions, | |||
} from './logger' | |||
export type { BindShortcutsOptions, CLIShortcut } from './shortcuts' |
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.
This should be removed with the internal change too.
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 build process force me to expose it even when it's internal. Do you know how to fix it?
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.
Ah this issue. Maybe we could refactor a bit to have bindShortcuts()
do a server._shortcutsBound = true
in the function itself? That way we can remove
vite/packages/vite/src/node/server/index.ts
Lines 448 to 451 in 6ae1d2d
bindShortcuts(opts = {}) { | |
bindShortcuts(server, opts) | |
server._shortcutsBound = true | |
}, |
Otherwise the other option is to post process and strip it off like
vite/packages/vite/scripts/postPatchTypes.ts
Lines 19 to 20 in 6ae1d2d
// remove picomatch type import because only the internal property uses it | |
const picomatchImport = "import type { Matcher as Matcher_2 } from 'picomatch';" |
The first one would be easier and also prevents someone accessing it.
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.
You can also hide it in a weak map against the server, no?
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.
Found something else, tell me what you think
6ae1d2d
to
3647c78
Compare
Works great! I think we should merge and release it so we can give at least a day to the ecosystem to test it. @ArnaudBarre if you would like, maybe we could add in another PR a title when pressing Right now it looks a bit confusing when pressing twice (although it is an edge case) but the extra line and title help while reading the options. We could also align the options so |
Yeah we need to know if I think that with blank line + title we don't need to align. Will to it in a next PR |
Hi all. I believe this PR has broken Ctrl-C behaviour somehow. I notice if I run 2 vites servers using tools like |
@dbousamra I see you also found #11563, a fix on top of this PR. If you have a diff repro from #11434 or you are seeing a regression from it, please create an issue with a new repro against the latest main so we can track it. A comment in an old PR would get lost quickly. |
@patak-dev False alarm. Was actually an issue with vitest, not yarn. Was very confused |
This is based on the work from #9673 by @HomyeeKing , @aleclarson and @joelmukuthu
Closes #9673
Few changes have been made:
SIGTERM
for ctrl+c, enable nicer exit and probably better in middleware mode (not tested)