microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/reactNativeCommandExecutor.ts
84lines · 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 {PlatformResolver} from "./../debugger/platformResolver"; |
| 6 | import {Package} from "./node/package"; |
| 7 | import {Packager} from "./../debugger/packager"; |
| 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 | this.isReactNativeProject().done(isRNProject => { |
| 26 | if (isRNProject) { |
| 27 | operation(); |
| 28 | } else { |
| 29 | vscode.window.showErrorMessage("Current workspace is not a React Native project."); |
| 30 | } |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Starts the React Native packager |
| 36 | */ |
| 37 | public startPackager(): void { |
| 38 | this.reactNativePackager.start(true, vscode.window.createOutputChannel("React-Native")) |
| 39 | .done(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Kills the React Native packager invoked by the extension's packager |
| 44 | */ |
| 45 | public stopPackager(): void { |
| 46 | this.reactNativePackager.stop(vscode.window.createOutputChannel("React-Native")); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Executes the 'react-native run-android' command |
| 51 | */ |
| 52 | public runAndroid(): void { |
| 53 | this.executeReactNativeCommand("run-android"); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Executes the 'react-native run-ios' command |
| 58 | */ |
| 59 | public runIos(): void { |
| 60 | this.executeReactNativeCommand("run-ios"); |
| 61 | } |
| 62 | |
| 63 | public executeReactNativeCommand(command: string): void { |
| 64 | let resolver = new PlatformResolver(); |
| 65 | let desktopPlatform = resolver.resolveDesktopPlatform(); |
| 66 | |
| 67 | // Invoke "react-native" with the command passed |
| 68 | new CommandExecutor(this.workspaceRoot).spawn(desktopPlatform.reactNativeCommandName, [command], {}, vscode.window.createOutputChannel("React-Native")) |
| 69 | .done(); |
| 70 | } |
| 71 | |
| 72 | private isReactNativeProject(): Q.Promise<boolean> { |
| 73 | let currentPackage = new Package(this.workspaceRoot); |
| 74 | return currentPackage.dependencies().then(dependencies => { |
| 75 | if (dependencies && dependencies["react-native"]) { |
| 76 | return true; |
| 77 | } else { |
| 78 | return false; |
| 79 | } |
| 80 | }).catch(() => { |
| 81 | return Q.resolve(false); |
| 82 | }); |
| 83 | } |
| 84 | } |
| 85 | |