microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ae883d21e1dec95f5927ece720829c6249380f3a

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commandPaletteHandler.ts

120lines · 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";
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(vscode.window.createOutputChannel("React-Native")));
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(vscode.window.createOutputChannel("React-Native")));
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 let outputChannel = vscode.window.createOutputChannel("React-Native");
91 Log.appendStringToOutputChannel("Attempting to start the React Native packager", outputChannel);
92
93 return this.reactNativePackager.start(outputChannel)
94 .then(() => {
95 return new CommandExecutor(this.workspaceRoot).spawnAndWaitReactCommand(command, args, null, outputChannel);
96 }).then(() => {
97 return Q.resolve<void>(void 0);
98 });
99 }
100
101 /**
102 * Ensures that we are in a React Native project and then executes the operation
103 * Otherwise, displays an error message banner
104 * {operation} - a function that performs the expected operation
105 */
106 private executeCommandInContext(rnCommand: string, operation: () => void): Q.Promise<void> {
107 let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
108 return TelemetryHelper.generate("RNCommand", (generator) => {
109 generator.add("command", rnCommand, false);
110 return reactNativeProjectHelper.isReactNativeProject().then(isRNProject => {
111 generator.add("isRNProject", isRNProject, false);
112 if (isRNProject) {
113 return operation();
114 } else {
115 vscode.window.showErrorMessage("Current workspace is not a React Native project.");
116 }
117 });
118 });
119 }
120}
121