microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/utils/reactNativeCommandExecutor.ts
73lines · 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 {ReactNativeProjectHelper} from "./reactNativeProjectHelper"; |
| 9 | import * as vscode from "vscode"; |
| 10 | |
| 11 | export class ReactNativeCommandExecutor { |
| 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 | * Ensures that we are in a React Native project and then executes the operation |
| 22 | * Otherwise, displays an error message banner |
| 23 | * {operation} - a function that performs the expected operation |
| 24 | */ |
| 25 | public executeCommandInContext(operation: () => void): void { |
| 26 | let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath); |
| 27 | reactNativeProjectHelper.isReactNativeProject().done(isRNProject => { |
| 28 | if (isRNProject) { |
| 29 | operation(); |
| 30 | } else { |
| 31 | vscode.window.showErrorMessage("Current workspace is not a React Native project."); |
| 32 | } |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Starts the React Native packager |
| 38 | */ |
| 39 | public startPackager(): void { |
| 40 | this.reactNativePackager.start(true, vscode.window.createOutputChannel("React-Native")) |
| 41 | .done(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Kills the React Native packager invoked by the extension's packager |
| 46 | */ |
| 47 | public stopPackager(): void { |
| 48 | this.reactNativePackager.stop(vscode.window.createOutputChannel("React-Native")); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Executes the 'react-native run-android' command |
| 53 | */ |
| 54 | public runAndroid(): void { |
| 55 | this.executeReactNativeCommand("run-android"); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Executes the 'react-native run-ios' command |
| 60 | */ |
| 61 | public runIos(): void { |
| 62 | this.executeReactNativeCommand("run-ios"); |
| 63 | } |
| 64 | |
| 65 | public executeReactNativeCommand(command: string): void { |
| 66 | let resolver = new PlatformResolver(); |
| 67 | let desktopPlatform = resolver.resolveDesktopPlatform(); |
| 68 | |
| 69 | // Invoke "react-native" with the command passed |
| 70 | new CommandExecutor(this.workspaceRoot).spawn(desktopPlatform.reactNativeCommandName, [command], {}, vscode.window.createOutputChannel("React-Native")) |
| 71 | .done(); |
| 72 | } |
| 73 | } |