microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b031edc774df5bdef810349bdd6d2c40c7acfadd

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commandPaletteHandler.ts

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