microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bf6ca50469e7197ca79e913dfab8ef6372097a15

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commandPaletteHandler.ts

119lines · 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 * as vscode from "vscode";
5import * as Q from "q";
6import {CommandExecutor} from "../common/commandExecutor";
7import {DeviceHelper, IDevice} from "../common/android/deviceHelper";
8import {Log} from "../common/log/log";
9import {Packager} from "../common/packager";
10import {Package} from "../common/node/package";
11import {PackageNameResolver} from "../common/android/packageNameResolver";
12import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
13import {TelemetryHelper} from "../common/telemetryHelper";
14import {IOSDebugModeManager} from "../common/ios/iOSDebugModeManager";
15
16export class CommandPaletteHandler {
17 private reactNativePackager: Packager;
18 private workspaceRoot: string;
19
20 constructor(workspaceRoot: string, reactNativePackager: Packager) {
21 this.workspaceRoot = workspaceRoot;
22 this.reactNativePackager = reactNativePackager;
23 }
24
25 /**
26 * Starts the React Native packager
27 */
28 public startPackager(): Q.Promise<void> {
29 return this.executeCommandInContext("startPackager", () => this.reactNativePackager.start());
30 }
31
32 /**
33 * Kills the React Native packager invoked by the extension's packager
34 */
35 public stopPackager(): Q.Promise<void> {
36 return this.executeCommandInContext("stopPackager", () => this.reactNativePackager.stop());
37 }
38
39 /**
40 * Executes the 'react-native run-android' command
41 */
42 public runAndroid(): Q.Promise<void> {
43 /* If there are multiple devices available, the run-android command will install the application on each and then print a warning.
44 The command will succeed but the application will not be launched on any device.
45 We fix this behavior by checking if there are more than one devices available and running the application on each. */
46 return this.executeCommandInContext("runAndroid", () => this.executeReactNativeRunCommand("run-android"))
47 .then(() => {
48 let deviceHelper = new DeviceHelper();
49 let pkg = new Package(this.workspaceRoot);
50
51 return Q.all<any>([
52 pkg.name().then((appName) => new PackageNameResolver(appName).resolvePackageName(this.workspaceRoot)),
53 deviceHelper.getConnectedDevices()
54 ]).spread<any>((packagName: string, devices: IDevice[]) => {
55 if (devices.length > 1) {
56 let result = Q<void>(void 0);
57 /* if we have more than one device, launch the application on each */
58 devices.forEach((device: IDevice) => {
59 if (device.isOnline) {
60 result = result.then(() => deviceHelper.launchApp(this.workspaceRoot, packagName, device.id));
61 }
62 });
63 return result;
64 } else {
65 return Q.resolve(void 0);
66 }
67 });
68 });
69 }
70
71 /**
72 * Executes the 'react-native run-ios' command
73 */
74 public runIos(): Q.Promise<void> {
75 return this.executeCommandInContext("runIos", () => {
76 // Set the Debugging setting to disabled, because in iOS it's persisted across runs of the app
77 return new IOSDebugModeManager(this.workspaceRoot).setSimulatorJSDebuggingModeSetting(/*enable=*/ false)
78 .catch(() => { }) // If setting the debugging mode fails, we ignore the error and we run the run ios command anyways
79 .then(() => this.executeReactNativeRunCommand("run-ios"));
80 });
81 }
82
83 /**
84 * Executes a react-native command passed after starting the packager
85 * {command} The command to be executed
86 * {args} The arguments to be passed to the command
87 */
88 private executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> {
89 // Start the packager before executing the React-Native command
90 Log.logMessage("Attempting to start the React Native packager");
91
92 return this.reactNativePackager.start()
93 .then(() => {
94 return new CommandExecutor(this.workspaceRoot).spawnAndWaitReactCommand(command, args, null);
95 }).then(() => {
96 return Q.resolve<void>(void 0);
97 });
98 }
99
100 /**
101 * Ensures that we are in a React Native project and then executes the operation
102 * Otherwise, displays an error message banner
103 * {operation} - a function that performs the expected operation
104 */
105 private executeCommandInContext(rnCommand: string, operation: () => void): Q.Promise<void> {
106 let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
107 return TelemetryHelper.generate("RNCommand", (generator) => {
108 generator.add("command", rnCommand, false);
109 return reactNativeProjectHelper.isReactNativeProject().then(isRNProject => {
110 generator.add("isRNProject", isRNProject, false);
111 if (isRNProject) {
112 return operation();
113 } else {
114 vscode.window.showErrorMessage("Current workspace is not a React Native project.");
115 }
116 });
117 });
118 }
119}