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() await server.listen()
logVersion(server.config.logger) logVersion(server.config.logger)
server.printUrls() server.printUrls()
bindShortcuts(server) bindShortcuts(server, createDevServer)
} }
createDevServer().catch((err) => { createDevServer().catch((err) => {
createLogger().error( createLogger().error(

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

Loading…
Cancel
Save