feat(cli): add shortcut for restarting server (#2403)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/2431/head
Alex 2 years ago committed by GitHub
parent 93f8a05ad2
commit 64b06db3ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -34,7 +34,7 @@ if (!command || command === 'dev') {
await server.listen()
logVersion(server.config.logger)
server.printUrls()
bindShortcuts(server)
bindShortcuts(server, createDevServer)
}
createDevServer().catch((err) => {
createLogger().error(

@ -1,22 +1,31 @@
import colors from 'picocolors'
import type { ViteDevServer } from 'vite'
import c from 'picocolors'
import { clearCache } from './markdownToVue'
type CreateDevServer = () => Promise<void>
export type CLIShortcut = {
key: string
description: string
action(server: ViteDevServer): void | Promise<void>
action(
server: ViteDevServer,
createDevServer: CreateDevServer
): void | Promise<void>
}
export function bindShortcuts(server: ViteDevServer): void {
export function bindShortcuts(
server: ViteDevServer,
createDevServer: CreateDevServer
): void {
if (!server.httpServer || !process.stdin.isTTY || process.env.CI) {
return
}
server.config.logger.info(
colors.dim(colors.green(' ➜')) +
colors.dim(' press ') +
colors.bold('h') +
colors.dim(' to show help')
c.dim(c.green(' ➜')) +
c.dim(' press ') +
c.bold('h') +
c.dim(' to show help')
)
let actionRunning = false
@ -34,12 +43,12 @@ export function bindShortcuts(server: ViteDevServer): void {
server.config.logger.info(
[
'',
colors.bold(' Shortcuts'),
c.bold(' Shortcuts'),
...SHORTCUTS.map(
(shortcut) =>
colors.dim(' press ') +
colors.bold(shortcut.key) +
colors.dim(` to ${shortcut.description}`)
c.dim(' press ') +
c.bold(shortcut.key) +
c.dim(` to ${shortcut.description}`)
)
].join('\n')
)
@ -49,7 +58,7 @@ export function bindShortcuts(server: ViteDevServer): void {
if (!shortcut) return
actionRunning = true
await shortcut.action(server)
await shortcut.action(server, createDevServer)
actionRunning = false
}
@ -63,6 +72,19 @@ export function bindShortcuts(server: ViteDevServer): void {
}
const SHORTCUTS: CLIShortcut[] = [
{
key: 'r',
description: 'restart the server',
async action(server, createDevServer) {
server.config.logger.info(c.green(`restarting server...\n`), {
clear: true,
timestamp: true
})
clearCache()
await server.close()
await createDevServer()
}
},
{
key: 'u',
description: 'show server url',

Loading…
Cancel
Save