microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
efd8fbff90ccf6d8b6743b251723b1ee27e9a2bb

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/reactNativeCommandExecutor.ts

80lines · 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 {Packager} from "./../debugger/packager";
7import {ReactNativeProjectHelper} from "./reactNativeProjectHelper";
8import * as vscode from "vscode";
9
10export class ReactNativeCommandExecutor {
11 private reactNativePackager: Packager;
12 private workspaceRoot: string;
13
14 constructor(workspaceRoot: string) {
15 this.workspaceRoot = workspaceRoot;
16 this.reactNativePackager = new Packager(workspaceRoot);
17 }
18
19 /**
20 * Starts the React Native packager
21 */
22 public startPackager(): void {
23 this.executeCommandInContext(() => this.reactNativePackager.start(vscode.window.createOutputChannel("React-Native")).done());
24 }
25
26 /**
27 * Kills the React Native packager invoked by the extension's packager
28 */
29 public stopPackager(): void {
30 this.executeCommandInContext(() => this.reactNativePackager.stop(vscode.window.createOutputChannel("React-Native")));
31 }
32
33 /**
34 * Executes the 'react-native run-android' command
35 */
36 public runAndroid(): void {
37 this.executeCommandInContext(() => this.executeReactNativeRunCommand("run-android"));
38 }
39
40 /**
41 * Executes the 'react-native run-ios' command
42 */
43 public runIos(): void {
44 this.executeCommandInContext(() => this.executeReactNativeRunCommand("run-ios"));
45 }
46
47 /**
48 * Executes a react-native command passed after starting the packager
49 * {command} The command to be executed
50 * {args} The arguments to be passed to the command
51 */
52 private executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> {
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)
58 .then(() => {
59 return new CommandExecutor(this.workspaceRoot).spawnReactCommand(command, args, undefined, outputChannel);
60 }).then(() => {
61 return Q.resolve<void>(void 0);
62 });
63 }
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 }
80}
81