microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commandPaletteHandler.ts
93lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {CommandExecutor} from "../common/commandExecutor"; |
| 5 | import {Log} from "../common/log"; |
| 6 | import {Packager} from "../common/packager"; |
| 7 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
| 8 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 9 | import * as vscode from "vscode"; |
| 10 | |
| 11 | export class CommandPaletteHandler { |
| 12 | private reactNativePackager: Packager; |
| 13 | private workspaceRoot: string; |
| 14 | |
| 15 | constructor(workspaceRoot: string) { |
| 16 | this.workspaceRoot = workspaceRoot; |
| 17 | this.reactNativePackager = new Packager(workspaceRoot); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Starts the React Native packager |
| 22 | */ |
| 23 | public startPackager(): void { |
| 24 | TelemetryHelper.generate("RNCommand", (generator) => { |
| 25 | generator.add("command", "startPackager", false); |
| 26 | return this.executeCommandInContext(() => this.reactNativePackager.start(vscode.window.createOutputChannel("React-Native")).done()); |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Kills the React Native packager invoked by the extension's packager |
| 32 | */ |
| 33 | public stopPackager(): void { |
| 34 | TelemetryHelper.generate("RNCommand", (generator) => { |
| 35 | generator.add("command", "stopPackager", false); |
| 36 | return this.executeCommandInContext(() => this.reactNativePackager.stop(vscode.window.createOutputChannel("React-Native"))); |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Executes the 'react-native run-android' command |
| 42 | */ |
| 43 | public runAndroid(): void { |
| 44 | TelemetryHelper.generate("RNCommand", (generator) => { |
| 45 | generator.add("command", "runAndroid", false); |
| 46 | return this.executeCommandInContext(() => this.executeReactNativeRunCommand("run-android")); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Executes the 'react-native run-ios' command |
| 52 | */ |
| 53 | public runIos(): void { |
| 54 | TelemetryHelper.generate("RNCommand", (generator) => { |
| 55 | generator.add("command", "runIos", false); |
| 56 | return this.executeCommandInContext(() => this.executeReactNativeRunCommand("run-ios")); |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Executes a react-native command passed after starting the packager |
| 62 | * {command} The command to be executed |
| 63 | * {args} The arguments to be passed to the command |
| 64 | */ |
| 65 | private executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> { |
| 66 | // Start the packager before executing the React-Native command |
| 67 | let outputChannel = vscode.window.createOutputChannel("React-Native"); |
| 68 | Log.appendStringToOutputChannel("Attempting to start the React Native packager", outputChannel); |
| 69 | |
| 70 | return this.reactNativePackager.start(outputChannel) |
| 71 | .then(() => { |
| 72 | return new CommandExecutor(this.workspaceRoot).spawnReactCommand(command, args, undefined, outputChannel); |
| 73 | }).then(() => { |
| 74 | return Q.resolve<void>(void 0); |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Ensures that we are in a React Native project and then executes the operation |
| 80 | * Otherwise, displays an error message banner |
| 81 | * {operation} - a function that performs the expected operation |
| 82 | */ |
| 83 | private executeCommandInContext(operation: () => void): Q.Promise<void> { |
| 84 | let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath); |
| 85 | return reactNativeProjectHelper.isReactNativeProject().then(isRNProject => { |
| 86 | if (isRNProject) { |
| 87 | return operation(); |
| 88 | } else { |
| 89 | vscode.window.showErrorMessage("Current workspace is not a React Native project."); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | |