microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/reactNativeCommandExecutor.ts
81lines · 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 | /** |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 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 | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 23 | */ |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 24 | public executeCommandInContext(operation: () => void): void { |
3d97c2a3Meena Kunnathur Balakrishnan10 years ago | 25 | let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath); |
| 26 | reactNativeProjectHelper.isReactNativeProject().done(isRNProject => { | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 27 | if (isRNProject) { |
| 28 | operation(); | |
| 29 | } else { | |
| 30 | vscode.window.showErrorMessage("Current workspace is not a React Native project."); | |
| 31 | } | |
| 32 | }); | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 33 | } |
| 34 | | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 35 | /** |
| 36 | * Starts the React Native packager | |
| 37 | */ | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 38 | public startPackager(): void { |
ec6e115dMeena Kunnathur Balakrishnan10 years ago | 39 | this.reactNativePackager.start(vscode.window.createOutputChannel("React-Native")) |
f8d32439dlebu10 years ago | 40 | .done(); |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 41 | } |
| 42 | | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 43 | /** |
| 44 | * Kills the React Native packager invoked by the extension's packager | |
| 45 | */ | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 46 | public stopPackager(): void { |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 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 { | |
3cf70711Meena Kunnathur Balakrishnan10 years ago | 54 | this.executeReactNativeRunCommand("run-android"); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 55 | } |
| 56 | | |
| 57 | /** | |
| 58 | * Executes the 'react-native run-ios' command | |
| 59 | */ | |
| 60 | public runIos(): void { | |
3cf70711Meena Kunnathur Balakrishnan10 years ago | 61 | this.executeReactNativeRunCommand("run-ios"); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 62 | } |
| 63 | | |
fb8f49fbMeena Kunnathur Balakrishnan10 years ago | 64 | /** |
3cf70711Meena Kunnathur Balakrishnan10 years ago | 65 | * Executes a react-native command passed after starting the packager |
fb8f49fbMeena Kunnathur Balakrishnan10 years ago | 66 | * {command} The command to be executed |
| 67 | * {args} The arguments to be passed to the command | |
| 68 | */ | |
3cf70711Meena Kunnathur Balakrishnan10 years ago | 69 | public executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> { |
ec6e115dMeena Kunnathur Balakrishnan10 years ago | 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) | |
f8d32439dlebu10 years ago | 75 | .then(() => { |
| 76 | return new CommandExecutor(this.workspaceRoot).spawnReactCommand(command, args, undefined, outputChannel); | |
| 77 | }).then(() => { | |
| 78 | return Q.resolve<void>(void 0); | |
| 79 | }); | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 80 | } |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 81 | } |