Skip to content

Commit

Permalink
feat: restart dev server via CLI shortcut (#3655)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Oct 8, 2024
1 parent e80273b commit 8f53644
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
13 changes: 11 additions & 2 deletions packages/core/src/server/cliShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ export function setupCliShortcuts({
openPage,
closeServer,
printUrls,
restartServer,
}: {
openPage: () => Promise<void>;
closeServer: () => Promise<void>;
printUrls: () => void;
restartServer?: () => Promise<void>;
}): void {
const shortcuts: CliShortcut[] = [
const shortcuts = [
{
key: 'c',
description: `${color.bold('c + enter')} ${color.dim('clear console')}`,
Expand All @@ -47,12 +49,19 @@ export function setupCliShortcuts({
}
},
},
restartServer
? {
key: 'r',
description: `${color.bold('r + enter')} ${color.dim('restart server')}`,
action: restartServer,
}
: null,
{
key: 'u',
description: `${color.bold('u + enter')} ${color.dim('show urls')}`,
action: printUrls,
},
];
].filter(Boolean) as CliShortcut[];

logger.log(
` ➜ ${color.dim('press')} ${color.bold('h + enter')} ${color.dim('to show shortcuts')}\n`,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/server/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import { createHttpServer } from './httpServer';
import { notFoundMiddleware } from './middlewares';
import { open } from './open';
import { onBeforeRestartServer } from './restart';
import { onBeforeRestartServer, restartDevServer } from './restart';
import { setupWatchFiles } from './watchFiles';

type HTTPServer = Server | Http2SecureServer;
Expand Down Expand Up @@ -224,6 +224,7 @@ export async function createDevServer<
openPage,
closeServer,
printUrls,
restartServer: () => restartDevServer({ clear: false }),
});
}

Expand Down
20 changes: 15 additions & 5 deletions packages/core/src/server/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,23 @@ const clearConsole = () => {

export const restartDevServer = async ({
filePath,
clear = true,
}: {
filePath: string;
}): Promise<void> => {
clearConsole();
filePath?: string;
clear?: boolean;
} = {}): Promise<void> => {
if (clear) {
clearConsole();
}

const filename = path.basename(filePath);
logger.info(`Restart because ${color.yellow(filename)} is changed.\n`);
if (filePath) {
const filename = path.basename(filePath);
logger.info(
`Restart server because ${color.yellow(filename)} is changed.\n`,
);
} else {
logger.info('Restarting server...\n');
}

for (const cleaner of cleaners) {
await cleaner();
Expand Down

0 comments on commit 8f53644

Please sign in to comment.