-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpreview-command-helper.ts
64 lines (56 loc) · 1.75 KB
/
preview-command-helper.ts
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
import * as readline from "readline";
import { ReadStream } from "tty";
import * as helpers from "../common/helpers";
export class PreviewCommandHelper implements IPreviewCommandHelper {
private ctrlcReader: readline.ReadLine;
constructor(private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator,
private $options: IOptions,
private $processService: IProcessService) {
this.$processService.attachToProcessExitSignals(this, () => {
this.stopWaitingForCommand();
});
}
public run(): void {
this.startWaitingForCommand();
}
private startWaitingForCommand(): void {
if (helpers.isInteractive()) {
this.ctrlcReader = readline.createInterface(<any>{
input: process.stdin,
output: process.stdout
});
this.ctrlcReader.on("SIGINT", process.exit);
(<ReadStream>process.stdin).setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.on("data", this.handleKeypress.bind(this));
}
}
private stopWaitingForCommand(): void {
if (helpers.isInteractive()) {
process.stdin.removeListener("data", this.handleKeypress);
(<ReadStream>process.stdin).setRawMode(false);
process.stdin.resume();
this.closeCtrlcReader();
}
}
private async handleKeypress(key: string): Promise<void> {
switch (key) {
case "a":
await this.$playgroundQrCodeGenerator.generateQrCodeForAndroid();
return;
case "i":
await this.$playgroundQrCodeGenerator.generateQrCodeForiOS();
return;
case "c":
await this.$playgroundQrCodeGenerator.generateQrCodeForCurrentApp({ useHotModuleReload: this.$options.hmr });
return;
}
}
private closeCtrlcReader() {
if (this.ctrlcReader) {
this.ctrlcReader.close();
}
}
}
$injector.register("previewCommandHelper", PreviewCommandHelper);