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