-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Expand file tree
/
Copy pathattachKeyHandlers.js
More file actions
133 lines (119 loc) · 3.22 KB
/
Copy pathattachKeyHandlers.js
File metadata and controls
133 lines (119 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import type {TerminalReporter} from 'metro';
import OpenDebuggerKeyboardHandler from './OpenDebuggerKeyboardHandler';
import invariant from 'invariant';
import readline from 'node:readline';
import {ReadStream} from 'node:tty';
import {styleText} from 'node:util';
const CTRL_C = '\u0003';
const CTRL_D = '\u0004';
const RELOAD_TIMEOUT = 500;
const throttle = (callback: () => void, timeout: number) => {
let previousCallTimestamp = 0;
return () => {
const currentCallTimestamp = new Date().getTime();
if (currentCallTimestamp - previousCallTimestamp > timeout) {
previousCallTimestamp = currentCallTimestamp;
callback();
}
};
};
type KeyEvent = {
sequence: string,
name: string,
ctrl: boolean,
meta: boolean,
shift: boolean,
};
export default function attachKeyHandlers({
devServerUrl,
messageSocket,
reporter,
}: {
devServerUrl: string,
messageSocket: Readonly<{
broadcast: (type: string, params?: Record<string, unknown> | null) => void,
...
}>,
reporter: TerminalReporter,
}) {
if (process.stdin.isTTY !== true) {
reporter.update({
type: 'unstable_server_log',
level: 'info',
data: 'Interactive mode is not supported in this environment',
});
return;
}
readline.emitKeypressEvents(process.stdin);
setRawMode(true);
const reload = throttle(() => {
reporter.update({
type: 'unstable_server_log',
level: 'info',
data: 'Reloading connected app(s)...',
});
messageSocket.broadcast('reload', null);
}, RELOAD_TIMEOUT);
const openDebuggerKeyboardHandler = new OpenDebuggerKeyboardHandler({
reporter,
devServerUrl,
});
process.stdin.on('keypress', (str: string, key: KeyEvent) => {
if (openDebuggerKeyboardHandler.maybeHandleTargetSelection(key.name)) {
return;
}
switch (key.sequence) {
case 'r':
reload();
break;
case 'd':
reporter.update({
type: 'unstable_server_log',
level: 'info',
data: 'Opening Dev Menu...',
});
messageSocket.broadcast('devMenu', null);
break;
case 'j':
void openDebuggerKeyboardHandler.handleOpenDebugger();
break;
case CTRL_C:
case CTRL_D:
openDebuggerKeyboardHandler.dismiss();
reporter.update({
type: 'unstable_server_log',
level: 'info',
data: 'Stopping server',
});
setRawMode(false);
process.stdin.pause();
process.emit('SIGINT');
process.exit();
}
});
reporter.update({
type: 'unstable_server_log',
level: 'info',
data: `Key commands available:
${styleText(['bold', 'inverse'], ' r ')} - reload app(s)
${styleText(['bold', 'inverse'], ' d ')} - open Dev Menu
${styleText(['bold', 'inverse'], ' j ')} - open DevTools
`,
});
}
function setRawMode(enable: boolean) {
invariant(
process.stdin instanceof ReadStream,
'process.stdin must be a readable stream to modify raw mode',
);
process.stdin.setRawMode(enable);
}