microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
10873e115f85fec54e69c69e094d16414ba24972

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commandPaletteHandler.ts

91lines · 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 "../common/commandExecutor";
5import {Log} from "../common/log";
6import {Packager} from "../common/packager";
7import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
8import {TelemetryHelper} from "../common/telemetryHelper";
9import * as vscode from "vscode";
10import {IOSDebugModeManager} from "../common/ios/iOSDebugModeManager";
11
12export class CommandPaletteHandler {
13 private reactNativePackager: Packager;
14 private workspaceRoot: string;
15
16 constructor(workspaceRoot: string) {
17 this.workspaceRoot = workspaceRoot;
18 this.reactNativePackager = new Packager(workspaceRoot);
19 }
20
21 /**
22 * Starts the React Native packager
23 */
24 public startPackager(): Q.Promise<void> {
25 return this.executeCommandInContext("startPackager", () => this.reactNativePackager.start(vscode.window.createOutputChannel("React-Native")));
26 }
27
28 /**
29 * Kills the React Native packager invoked by the extension's packager
30 */
31 public stopPackager(): Q.Promise<void> {
32 return this.executeCommandInContext("stopPackager", () => this.reactNativePackager.stop(vscode.window.createOutputChannel("React-Native")));
33 }
34
35 /**
36 * Executes the 'react-native run-android' command
37 */
38 public runAndroid(): Q.Promise<void> {
39 return this.executeCommandInContext("runAndroid", () => this.executeReactNativeRunCommand("run-android"));
40 }
41
42 /**
43 * Executes the 'react-native run-ios' command
44 */
45 public runIos(): Q.Promise<void> {
46 return this.executeCommandInContext("runIos", () => {
47 // Set the Debugging setting to disabled, because in iOS it's persisted across runs of the app
48 return new IOSDebugModeManager(this.workspaceRoot).setSimulatorJSDebuggingModeSetting(/*enable=*/ false)
49 .catch(() => { }) // If setting the debugging mode fails, we ignore the error and we run the run ios command anyways
50 .then(() => this.executeReactNativeRunCommand("run-ios"));
51 });
52 }
53
54 /**
55 * Executes a react-native command passed after starting the packager
56 * {command} The command to be executed
57 * {args} The arguments to be passed to the command
58 */
59 private executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> {
60 // Start the packager before executing the React-Native command
61 let outputChannel = vscode.window.createOutputChannel("React-Native");
62 Log.appendStringToOutputChannel("Attempting to start the React Native packager", outputChannel);
63
64 return this.reactNativePackager.start(outputChannel)
65 .then(() => {
66 return new CommandExecutor(this.workspaceRoot).spawnAndWaitReactCommand(command, args, null, outputChannel);
67 }).then(() => {
68 return Q.resolve<void>(void 0);
69 });
70 }
71
72 /**
73 * Ensures that we are in a React Native project and then executes the operation
74 * Otherwise, displays an error message banner
75 * {operation} - a function that performs the expected operation
76 */
77 private executeCommandInContext(rnCommand: string, operation: () => void): Q.Promise<void> {
78 let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
79 return TelemetryHelper.generate("RNCommand", (generator) => {
80 generator.add("command", rnCommand, false);
81 return reactNativeProjectHelper.isReactNativeProject().then(isRNProject => {
82 generator.add("isRNProject", isRNProject, false);
83 if (isRNProject) {
84 return operation();
85 } else {
86 vscode.window.showErrorMessage("Current workspace is not a React Native project.");
87 }
88 });
89 });
90 }
91}
92