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