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