|
| 1 | +import * as readline from "readline"; |
| 2 | +import { ReadStream } from "tty"; |
| 3 | +import * as helpers from "../common/helpers"; |
| 4 | +const chalk = require("chalk"); |
| 5 | + |
| 6 | +export class PreviewCommandHelper implements IPreviewCommandHelper { |
| 7 | + private ctrlcReader: readline.ReadLine; |
| 8 | + |
| 9 | + constructor(private $logger: ILogger, |
| 10 | + private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator, |
| 11 | + private $processService: IProcessService) { |
| 12 | + this.$processService.attachToProcessExitSignals(this, () => { |
| 13 | + this.stopWaitingForCommand(); |
| 14 | + }); |
| 15 | + } |
| 16 | + |
| 17 | + public run() { |
| 18 | + this.printUsage(); |
| 19 | + this.startWaitingForCommand(); |
| 20 | + } |
| 21 | + |
| 22 | + private startWaitingForCommand(): void { |
| 23 | + if (helpers.isInteractive()) { |
| 24 | + this.ctrlcReader = readline.createInterface(<any>{ |
| 25 | + input: process.stdin, |
| 26 | + output: process.stdout |
| 27 | + }); |
| 28 | + this.ctrlcReader.on("SIGINT", process.exit); |
| 29 | + |
| 30 | + (<ReadStream>process.stdin).setRawMode(true); |
| 31 | + process.stdin.resume(); |
| 32 | + process.stdin.setEncoding("utf8"); |
| 33 | + process.stdin.on("data", this.handleKeypress.bind(this)); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private stopWaitingForCommand(): void { |
| 38 | + if (helpers.isInteractive()) { |
| 39 | + process.stdin.removeListener("data", this.handleKeypress); |
| 40 | + (<ReadStream>process.stdin).setRawMode(false); |
| 41 | + process.stdin.resume(); |
| 42 | + this.closeCtrlcReader(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private async handleKeypress(key: string): Promise<void> { |
| 47 | + switch (key) { |
| 48 | + case "a": |
| 49 | + await this.$playgroundQrCodeGenerator.generateQrCodeForAndroid(); |
| 50 | + this.printUsage(); |
| 51 | + return; |
| 52 | + case "i": |
| 53 | + await this.$playgroundQrCodeGenerator.generateQrCodeForiOS(); |
| 54 | + this.printUsage(); |
| 55 | + return; |
| 56 | + case "q": |
| 57 | + await this.$playgroundQrCodeGenerator.generateQrCodeForCurrentApp(); |
| 58 | + this.printUsage(); |
| 59 | + return; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private printUsage(): void { |
| 64 | + this.$logger.info(` |
| 65 | +-> Press ${this.underlineBoldCyan("a")} to show the QR code of NativeScript Playground app for ${this.underlineBoldCyan("Android")} devices |
| 66 | +-> Press ${this.underlineBoldCyan("i")} to show the QR code of NativeScript Playground app for ${this.underlineBoldCyan("iOS")} devices |
| 67 | +-> Press ${this.underlineBoldCyan("q")} to display the QR code of the current application. |
| 68 | + `); |
| 69 | + } |
| 70 | + |
| 71 | + private underlineBoldCyan(str: string) { |
| 72 | + const { bold, underline, cyan } = chalk; |
| 73 | + return underline(bold(cyan(str))); |
| 74 | + } |
| 75 | + |
| 76 | + private closeCtrlcReader() { |
| 77 | + if (this.ctrlcReader) { |
| 78 | + this.ctrlcReader.close(); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +$injector.register("previewCommandHelper", PreviewCommandHelper); |
0 commit comments