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