Skip to content

Commit

Permalink
feat(cli): add shortcuts (#2353)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed May 10, 2023
1 parent af4bb52 commit 97065ce
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/node/cli.ts
Expand Up @@ -4,6 +4,7 @@ import { createLogger } from 'vite'
import { build, createServer, serve } from '.'
import { init } from './init/init'
import { version } from '../../package.json'
import { bindShortcuts } from './shortcuts'

const argv: any = minimist(process.argv.slice(2))

Expand Down Expand Up @@ -33,6 +34,7 @@ if (!command || command === 'dev') {
await server.listen()
logVersion(server.config.logger)
server.printUrls()
bindShortcuts(server)
}
createDevServer().catch((err) => {
createLogger().error(
Expand Down
95 changes: 95 additions & 0 deletions src/node/shortcuts.ts
@@ -0,0 +1,95 @@
import colors from 'picocolors'
import type { ViteDevServer } from 'vite'

export type CLIShortcut = {
key: string
description: string
action(server: ViteDevServer): void | Promise<void>
}

export function bindShortcuts(server: ViteDevServer): 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')
)

let actionRunning = false

const onInput = async (input: string) => {
// ctrl+c or ctrl+d
if (input === '\x03' || input === '\x04') {
await server.close().finally(() => process.exit(1))
return
}

if (actionRunning) return

if (input === 'h') {
server.config.logger.info(
[
'',
colors.bold(' Shortcuts'),
...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
}

process.stdin.setRawMode(true)

process.stdin.on('data', onInput).setEncoding('utf8').resume()

server.httpServer.on('close', () => {
process.stdin.off('data', onInput).pause()
})
}

const SHORTCUTS: CLIShortcut[] = [
{
key: 'u',
description: 'show server url',
action(server) {
server.config.logger.info('')
server.printUrls()
}
},
{
key: 'o',
description: 'open in browser',
action(server) {
server.openBrowser()
}
},
{
key: 'c',
description: 'clear console',
action(server) {
server.config.logger.clearScreen('error')
}
},
{
key: 'q',
description: 'quit',
async action(server) {
await server.close().finally(() => process.exit())
}
}
]

0 comments on commit 97065ce

Please sign in to comment.