microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3a0bfdb2c230b6319fe09bfd70e3bae29d0fe088

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commandPaletteHandler.ts

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