Skip to content

Commit

Permalink
feat: add CLI keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Dec 6, 2022
1 parent d135b11 commit 5ceab38
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path'
import fs from 'node:fs'
import { performance } from 'node:perf_hooks'
import type { Session } from 'node:inspector'
import { cac } from 'cac'
import colors from 'picocolors'
import type { BuildOptions } from './build'
Expand Down Expand Up @@ -30,9 +31,10 @@ interface GlobalCLIOptions {
force?: boolean
}

// @ts-ignore
const profileSession: Session | undefined = global.__vite_profile_session

export const stopProfiler = (log: (message: string) => void): void => {
// @ts-ignore
const profileSession = global.__vite_profile_session
if (profileSession) {
profileSession.post('Profiler.stop', (err: any, { profile }: any) => {
// Write profile to disk, upload, etc.
Expand Down Expand Up @@ -148,7 +150,18 @@ cli
)

server.printUrls()
stopProfiler((message) => server.config.logger.info(` ${message}`))
server.bindShortcuts({
print: true,
additionalShortCuts: [
profileSession && {
key: 's',
description: 'stop the profiler',
action(server) {
stopProfiler(server.config.logger.info)
},
},
],
})
} catch (e) {
const logger = createLogger(options.logLevel)
logger.error(colors.red(`error when starting dev server:\n${e.stack}`), {
Expand Down
20 changes: 20 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {
initDepsOptimizer,
initDevSsrDepsOptimizer,
} from '../optimizer'
import { bindShortcuts } from '../shortcuts'
import type { BindShortcutsOptions } from '../shortcuts'
import { CLIENT_DIR } from '../constants'
import type { Logger } from '../logger'
import { printServerUrls } from '../logger'
Expand Down Expand Up @@ -262,6 +264,10 @@ export interface ViteDevServer {
* Print server urls
*/
printUrls(): void
/**
* Listen to `process.stdin` for keyboard shortcuts.
*/
bindShortcuts(opts?: BindShortcutsOptions): void
/**
* Restart the server.
*
Expand Down Expand Up @@ -300,6 +306,10 @@ export interface ViteDevServer {
* @internal
*/
_fsDenyGlob: Matcher
/**
* @internal
*/
_shortcutsBound: boolean
}

export interface ResolvedServerUrls {
Expand Down Expand Up @@ -435,6 +445,10 @@ export async function createServer(
)
}
},
bindShortcuts(opts = {}) {
bindShortcuts(server, opts)
server._shortcutsBound = true
},
async restart(forceOptimize?: boolean) {
if (!server._restartPromise) {
server._forceOptimizeOnRestart = !!forceOptimize
Expand All @@ -452,6 +466,7 @@ export async function createServer(
_forceOptimizeOnRestart: false,
_pendingRequests: new Map(),
_fsDenyGlob: picomatch(config.server.fs.deny, { matchBase: true }),
_shortcutsBound: false,
}

server.transformIndexHtml = createDevHtmlTransformFn(server)
Expand Down Expand Up @@ -771,6 +786,7 @@ async function restartServer(server: ViteDevServer) {
// @ts-ignore
global.__vite_start_time = performance.now()
const { port: prevPort, host: prevHost } = server.config.server
const bindShortcuts = server._shortcutsBound

await server.close()

Expand Down Expand Up @@ -819,6 +835,10 @@ async function restartServer(server: ViteDevServer) {
logger.info('server restarted.', { timestamp: true })
}

if (bindShortcuts) {
newServer.bindShortcuts()
}

// new server (the current server) can restart now
newServer._restartPromise = null
}
Expand Down
136 changes: 136 additions & 0 deletions packages/vite/src/node/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import colors from 'picocolors'
import type { ViteDevServer } from './server'
import { openBrowser } from './server/openBrowser'
import type { HmrOptions } from './server/hmr'
import { isDefined } from './utils'

export type BindShortcutsOptions = {
/**
* Print a one line hint to the terminal.
*/
print?: boolean
additionalShortCuts?: (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

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.additionalShortCuts ?? [])
.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()
})
}

let initialHmrOptions: HmrOptions | boolean

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: 'm',
description: 'toggle hmr on/off',
action({ config }: ViteDevServer): void {
if (initialHmrOptions === undefined) {
initialHmrOptions = config.server.hmr ?? true
}
/**
* Mutating the server config works because Vite reads from
* it on every file change, instead of caching its value.
*/
config.server.hmr =
config.server.hmr === false
? initialHmrOptions === false
? true
: initialHmrOptions
: false
config.logger.info(
colors.cyan(`hmr ${config.server.hmr ? `enabled` : `disabled`}`),
)
},
},
{
key: 'q',
description: 'quit',
async action(server) {
await server.close().finally(() => process.exit())
},
},
]

0 comments on commit 5ceab38

Please sign in to comment.