microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3314a820561f672a145a365db5c47896e6286fa2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/utils/reactNativeCommandExecutor.ts

94lines · 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
4import {CommandExecutor} from "./commands/commandExecutor";
5import {Log} from "./commands/log";
6import {PlatformResolver} from "./../debugger/platformResolver";
7import {Packager} from "./../debugger/packager";
8import {ReactNativeProjectHelper} from "./reactNativeProjectHelper";
9import * as vscode from "vscode";
10
11export class ReactNativeCommandExecutor {
12 private reactNativePackager: Packager;
13 private workspaceRoot: string;
14
15 constructor(workspaceRoot: string) {
16 let resolver = new PlatformResolver();
17 let desktopPlatform = resolver.resolveDesktopPlatform();
18 this.workspaceRoot = workspaceRoot;
19 this.reactNativePackager = new Packager(workspaceRoot, desktopPlatform);
20 }
21
22 /**
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
26 */
27 public executeCommandInContext(operation: () => void): void {
28 let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
29 reactNativeProjectHelper.isReactNativeProject().done(isRNProject => {
30 if (isRNProject) {
31 operation();
32 } else {
33 vscode.window.showErrorMessage("Current workspace is not a React Native project.");
34 }
35 });
36 }
37
38 /**
39 * Starts the React Native packager
40 */
41 public startPackager(): void {
42 this.reactNativePackager.start(vscode.window.createOutputChannel("React-Native"))
43 .done();
44 }
45
46 /**
47 * Kills the React Native packager invoked by the extension's packager
48 */
49 public stopPackager(): void {
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 {
57 this.executeReactNativeRunCommand("run-android");
58 }
59
60 /**
61 * Executes the 'react-native run-ios' command
62 */
63 public runIos(): void {
64 this.executeReactNativeRunCommand("run-ios");
65 }
66
67 /**
68 * Executes a react-native command passed after starting the packager
69 * {command} The command to be executed
70 * {args} The arguments to be passed to the command
71 */
72 public executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> {
73 let resolver = new PlatformResolver();
74 let desktopPlatform = resolver.resolveDesktopPlatform();
75
76 // Invoke "react-native" with the command passed
77 let runArguments = [command];
78
79 if (args) {
80 runArguments.concat(args);
81 }
82
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)
88 .then(() => {
89 return new CommandExecutor(this.workspaceRoot).spawn(desktopPlatform.reactNativeCommandName, runArguments, {}, vscode.window.createOutputChannel("React-Native"));
90 }).then(() => {
91 return Q.resolve<void>(void 0);
92 });
93 }
94}