microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/utils/reactNativeCommandExecutor.ts
87lines · 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 {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 | let resolver = new PlatformResolver(); |
| 16 | let desktopPlatform = resolver.resolveDesktopPlatform(); |
| 17 | this.workspaceRoot = workspaceRoot; |
| 18 | this.reactNativePackager = new Packager(workspaceRoot, desktopPlatform); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Ensures that we are in a React Native project and then executes the operation |
| 23 | * Otherwise, displays an error message banner |
| 24 | * {operation} - a function that performs the expected operation |
| 25 | */ |
| 26 | public executeCommandInContext(operation: () => void): void { |
| 27 | let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath); |
| 28 | reactNativeProjectHelper.isReactNativeProject().done(isRNProject => { |
| 29 | if (isRNProject) { |
| 30 | operation(); |
| 31 | } else { |
| 32 | vscode.window.showErrorMessage("Current workspace is not a React Native project."); |
| 33 | } |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Starts the React Native packager |
| 39 | */ |
| 40 | public startPackager(): void { |
| 41 | this.reactNativePackager.start(true, vscode.window.createOutputChannel("React-Native")) |
| 42 | .done(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Kills the React Native packager invoked by the extension's packager |
| 47 | */ |
| 48 | public stopPackager(): void { |
| 49 | this.reactNativePackager.stop(vscode.window.createOutputChannel("React-Native")); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Executes the 'react-native run-android' command |
| 54 | */ |
| 55 | public runAndroid(): void { |
| 56 | this.executeReactNativeCommand("run-android"); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Executes the 'react-native run-ios' command |
| 61 | */ |
| 62 | public runIos(): void { |
| 63 | this.executeReactNativeCommand("run-ios"); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Executes a react-native command passed |
| 68 | * {command} The command to be executed |
| 69 | * {args} The arguments to be passed to the command |
| 70 | */ |
| 71 | public executeReactNativeCommand(command: string, args?: string[]): Q.Promise<void> { |
| 72 | let resolver = new PlatformResolver(); |
| 73 | let desktopPlatform = resolver.resolveDesktopPlatform(); |
| 74 | |
| 75 | // Invoke "react-native" with the command passed |
| 76 | let runArguments = [command]; |
| 77 | |
| 78 | if (args) { |
| 79 | runArguments.concat(args); |
| 80 | } |
| 81 | |
| 82 | return new CommandExecutor(this.workspaceRoot).spawn(desktopPlatform.reactNativeCommandName, runArguments, {}, vscode.window.createOutputChannel("React-Native")) |
| 83 | .then(() => { |
| 84 | return Q.resolve<void>(void 0); |
| 85 | }); |
| 86 | } |
| 87 | } |
| 88 | |