Skip to content

Commit 0d796b6

Browse files
nishu-murmuaklinker1Timeraa
authored
feat: Keyboard shortcut to reopen the browser without restarting the dev command (#1211)
Co-authored-by: Aaron <[email protected]> Co-authored-by: Florian Metz <[email protected]>
1 parent cb143d2 commit 0d796b6

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Diff for: packages/wxt/src/core/create-server.ts

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
getContentScriptJs,
2828
mapWxtOptionsToRegisteredContentScript,
2929
} from './utils/content-scripts';
30+
import { createKeyboardShortcuts } from './keyboard-shortcuts';
3031

3132
/**
3233
* Creates a dev server and pre-builds all the files that need to exist before loading the extension.
@@ -108,9 +109,13 @@ async function createServerInternal(): Promise<WxtDevServer> {
108109
// Listen for file changes and reload different parts of the extension accordingly
109110
const reloadOnChange = createFileReloader(server);
110111
server.watcher.on('all', reloadOnChange);
112+
keyboardShortcuts.start();
113+
keyboardShortcuts.printHelp();
111114
},
115+
112116
async stop() {
113117
wasStopped = true;
118+
keyboardShortcuts.stop();
114119
await runner.closeBrowser();
115120
await builderServer.close();
116121
await wxt.hooks.callHook('server:closed', wxt, server);
@@ -136,11 +141,14 @@ async function createServerInternal(): Promise<WxtDevServer> {
136141
},
137142
async restartBrowser() {
138143
await runner.closeBrowser();
144+
keyboardShortcuts.stop();
139145
await wxt.reloadConfig();
140146
runner = await createExtensionRunner();
141147
await runner.openBrowser();
148+
keyboardShortcuts.start();
142149
},
143150
};
151+
const keyboardShortcuts = createKeyboardShortcuts(server);
144152

145153
const buildAndOpenBrowser = async () => {
146154
// Build after starting the dev server so it can be used to transform HTML files
@@ -230,6 +238,7 @@ function createFileReloader(server: WxtDevServer) {
230238
break;
231239
case 'content-script-reload':
232240
reloadContentScripts(changes.changedSteps, server);
241+
233242
const rebuiltNames = changes.rebuildGroups
234243
.flat()
235244
.map((entry) => entry.name);

Diff for: packages/wxt/src/core/keyboard-shortcuts.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import readline from 'node:readline';
2+
import { WxtDevServer } from '../types';
3+
import { wxt } from './wxt';
4+
import pc from 'picocolors';
5+
6+
export interface KeyboardShortcutWatcher {
7+
start(): void;
8+
stop(): void;
9+
printHelp(): void;
10+
}
11+
12+
/**
13+
* Function that creates a keyboard shortcut handler for the extension.
14+
*/
15+
export function createKeyboardShortcuts(
16+
server: WxtDevServer,
17+
): KeyboardShortcutWatcher {
18+
let isWatching = false;
19+
let rl: readline.Interface | undefined;
20+
21+
const handleInput = (line: string) => {
22+
// Only handle our specific command
23+
if (line.trim() === 'o') {
24+
server.restartBrowser();
25+
}
26+
};
27+
28+
return {
29+
start() {
30+
if (isWatching) return;
31+
32+
rl = readline.createInterface({
33+
input: process.stdin,
34+
terminal: false, // Don't intercept ctrl+C, ctrl+Z, etc
35+
});
36+
37+
rl.on('line', handleInput);
38+
isWatching = true;
39+
},
40+
41+
stop() {
42+
if (!isWatching) return;
43+
44+
if (rl) {
45+
rl.close();
46+
rl = undefined;
47+
}
48+
49+
isWatching = false;
50+
},
51+
52+
printHelp() {
53+
if (!wxt.config.runnerConfig.config.disabled) {
54+
wxt.logger.info(
55+
`${pc.dim('Press')} ${pc.bold('o + enter')} ${pc.dim('to reopen the browser')}`,
56+
);
57+
}
58+
},
59+
};
60+
}

0 commit comments

Comments
 (0)