microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3194e9af2e1a743d064817ed379e68081bf53716

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/reactNativeCommandExecutor.ts

84lines · 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 {PlatformResolver} from "./../debugger/platformResolver";
6import {Package} from "./node/package";
7import {Packager} from "./../debugger/packager";
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 * 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
23 */
24 public executeCommandInContext(operation: () => void): void {
25 this.isReactNativeProject().done(isRNProject => {
26 if (isRNProject) {
27 operation();
28 } else {
29 vscode.window.showErrorMessage("Current workspace is not a React Native project.");
30 }
31 });
32 }
33
34 /**
35 * Starts the React Native packager
36 */
37 public startPackager(): void {
38 this.reactNativePackager.start(true, vscode.window.createOutputChannel("React-Native"))
39 .done();
40 }
41
42 /**
43 * Kills the React Native packager invoked by the extension's packager
44 */
45 public stopPackager(): void {
46 this.reactNativePackager.stop(vscode.window.createOutputChannel("React-Native"));
47 }
48
49 /**
50 * Executes the 'react-native run-android' command
51 */
52 public runAndroid(): void {
53 this.executeReactNativeCommand("run-android");
54 }
55
56 /**
57 * Executes the 'react-native run-ios' command
58 */
59 public runIos(): void {
60 this.executeReactNativeCommand("run-ios");
61 }
62
63 public executeReactNativeCommand(command: string): void {
64 let resolver = new PlatformResolver();
65 let desktopPlatform = resolver.resolveDesktopPlatform();
66
67 // Invoke "react-native" with the command passed
68 new CommandExecutor(this.workspaceRoot).spawn(desktopPlatform.reactNativeCommandName, [command], {}, vscode.window.createOutputChannel("React-Native"))
69 .done();
70 }
71
72 private isReactNativeProject(): Q.Promise<boolean> {
73 let currentPackage = new Package(this.workspaceRoot);
74 return currentPackage.dependencies().then(dependencies => {
75 if (dependencies && dependencies["react-native"]) {
76 return true;
77 } else {
78 return false;
79 }
80 }).catch(() => {
81 return Q.resolve(false);
82 });
83 }
84}
85