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