microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commandPaletteHandler.ts

128lines · modeblame

bef522ffMeena Kunnathur Balakrishnan10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
6e179583digeff10 years ago4import * as vscode from "vscode";
5import * as Q from "q";
b0061ac6Meena Kunnathur Balakrishnan10 years ago6import {CommandExecutor} from "../common/commandExecutor";
fcb6a5dadlebu10 years ago7import {DeviceHelper, IDevice} from "../common/android/deviceHelper";
190e393cMeena Kunnathur Balakrishnan10 years ago8import {Log} from "../common/log/log";
b0061ac6Meena Kunnathur Balakrishnan10 years ago9import {Packager} from "../common/packager";
fcb6a5dadlebu10 years ago10import {Package} from "../common/node/package";
11import {PackageNameResolver} from "../common/android/packageNameResolver";
ffffa686Meena Kunnathur Balakrishnan10 years ago12import {PackagerStatus, PackagerStatusIndicator} from "./packagerStatusIndicator";
b0061ac6Meena Kunnathur Balakrishnan10 years ago13import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
d976d077Meena Kunnathur Balakrishnan10 years ago14import {TelemetryHelper} from "../common/telemetryHelper";
1ed272e1digeff10 years ago15import {IOSDebugModeManager} from "../common/ios/iOSDebugModeManager";
bef522ffMeena Kunnathur Balakrishnan10 years ago16
e1c05e69dlebu10 years ago17export class CommandPaletteHandler {
bef522ffMeena Kunnathur Balakrishnan10 years ago18private reactNativePackager: Packager;
ffffa686Meena Kunnathur Balakrishnan10 years ago19private reactNativePackageStatusIndicator: PackagerStatusIndicator;
bef522ffMeena Kunnathur Balakrishnan10 years ago20private workspaceRoot: string;
21
ffffa686Meena Kunnathur Balakrishnan10 years ago22constructor(workspaceRoot: string, reactNativePackager: Packager, packagerStatusIndicator: PackagerStatusIndicator) {
bef522ffMeena Kunnathur Balakrishnan10 years ago23this.workspaceRoot = workspaceRoot;
a822ac85dlebu10 years ago24this.reactNativePackager = reactNativePackager;
ffffa686Meena Kunnathur Balakrishnan10 years ago25this.reactNativePackageStatusIndicator = packagerStatusIndicator;
bef522ffMeena Kunnathur Balakrishnan10 years ago26}
27
0904a0d7Meena Kunnathur Balakrishnan10 years ago28/**
29* Starts the React Native packager
30*/
10873e11digeff10 years ago31public startPackager(): Q.Promise<void> {
ffffa686Meena Kunnathur Balakrishnan10 years ago32return this.executeCommandInContext("startPackager", () => this.reactNativePackager.start())
33.then(() => this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STARTED));
bef522ffMeena Kunnathur Balakrishnan10 years ago34}
35
0904a0d7Meena Kunnathur Balakrishnan10 years ago36/**
37* Kills the React Native packager invoked by the extension's packager
38*/
10873e11digeff10 years ago39public stopPackager(): Q.Promise<void> {
ffffa686Meena Kunnathur Balakrishnan10 years ago40return this.executeCommandInContext("stopPackager", () => this.reactNativePackager.stop())
41.then(() => this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STOPPED));
3194e9afMeena Kunnathur Balakrishnan10 years ago42}
43
44/**
45* Executes the 'react-native run-android' command
46*/
10873e11digeff10 years ago47public runAndroid(): Q.Promise<void> {
2a8515bcdlebu10 years ago48/* If there are multiple devices available, the run-android command will install the application on each and then print a warning.
49The command will succeed but the application will not be launched on any device.
50We fix this behavior by checking if there are more than one devices available and running the application on each. */
fcb6a5dadlebu10 years ago51return this.executeCommandInContext("runAndroid", () => this.executeReactNativeRunCommand("run-android"))
52.then(() => {
53let deviceHelper = new DeviceHelper();
54let pkg = new Package(this.workspaceRoot);
55
56return Q.all<any>([
57pkg.name().then((appName) => new PackageNameResolver(appName).resolvePackageName(this.workspaceRoot)),
58deviceHelper.getConnectedDevices()
59]).spread<any>((packagName: string, devices: IDevice[]) => {
60if (devices.length > 1) {
61let result = Q<void>(void 0);
62/* if we have more than one device, launch the application on each */
63devices.forEach((device: IDevice) => {
64if (device.isOnline) {
65result = result.then(() => deviceHelper.launchApp(this.workspaceRoot, packagName, device.id));
66}
67});
68return result;
69} else {
70return Q.resolve(void 0);
71}
72});
73});
3194e9afMeena Kunnathur Balakrishnan10 years ago74}
75
76/**
77* Executes the 'react-native run-ios' command
78*/
10873e11digeff10 years ago79public runIos(): Q.Promise<void> {
80return this.executeCommandInContext("runIos", () => {
81// Set the Debugging setting to disabled, because in iOS it's persisted across runs of the app
82return 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});
3194e9afMeena Kunnathur Balakrishnan10 years ago86}
87
fb8f49fbMeena Kunnathur Balakrishnan10 years ago88/**
3cf70711Meena Kunnathur Balakrishnan10 years ago89* Executes a react-native command passed after starting the packager
fb8f49fbMeena Kunnathur Balakrishnan10 years ago90* {command} The command to be executed
91* {args} The arguments to be passed to the command
92*/
b3a793eeNisheet Jain10 years ago93private executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> {
ec6e115dMeena Kunnathur Balakrishnan10 years ago94// Start the packager before executing the React-Native command
53520386Meena Kunnathur Balakrishnan10 years ago95Log.logMessage("Attempting to start the React Native packager");
ec6e115dMeena Kunnathur Balakrishnan10 years ago96
f1a07677Meena Kunnathur Balakrishnan10 years ago97return this.reactNativePackager.start()
f8d32439dlebu10 years ago98.then(() => {
f1a07677Meena Kunnathur Balakrishnan10 years ago99return new CommandExecutor(this.workspaceRoot).spawnAndWaitReactCommand(command, args, null);
f8d32439dlebu10 years ago100}).then(() => {
101return Q.resolve<void>(void 0);
102});
3194e9afMeena Kunnathur Balakrishnan10 years ago103}
b3a793eeNisheet Jain10 years ago104
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*/
10873e11digeff10 years ago110private executeCommandInContext(rnCommand: string, operation: () => void): Q.Promise<void> {
b3a793eeNisheet Jain10 years ago111let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
10873e11digeff10 years ago112return TelemetryHelper.generate("RNCommand", (generator) => {
8512ccfeMeena Kunnathur Balakrishnan10 years ago113generator.add("command", rnCommand, false);
114return reactNativeProjectHelper.isReactNativeProject().then(isRNProject => {
0633e07bMeena Kunnathur Balakrishnan10 years ago115generator.add("isRNProject", isRNProject, false);
8512ccfeMeena Kunnathur Balakrishnan10 years ago116if (isRNProject) {
cf138e34Meena Kunnathur Balakrishnan10 years ago117// Bring the log channel to focus
f1e34747Meena Kunnathur Balakrishnan10 years ago118Log.setFocusOnLogChannel();
cf138e34Meena Kunnathur Balakrishnan10 years ago119
120// Execute the operation
8512ccfeMeena Kunnathur Balakrishnan10 years ago121return operation();
122} else {
123vscode.window.showErrorMessage("Current workspace is not a React Native project.");
124}
125});
10873e11digeff10 years ago126});
b3a793eeNisheet Jain10 years ago127}
bef522ffMeena Kunnathur Balakrishnan10 years ago128}