microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
881dd8be6de113dd9a45da8f9211f47d16efaf25

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commandPaletteHandler.ts

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