microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commandPaletteHandler.ts
220lines · modeblame
bef522ffMeena Kunnathur Balakrishnan10 years ago | 1 | // 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 ago | 4 | import * as vscode from "vscode"; |
| 5 | import * as Q from "q"; | |
7059d307Patricio Beltran9 years ago | 6 | import * as XDL from "../common/exponent/xdlInterface"; |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 7 | import {CommandExecutor} from "../common/commandExecutor"; |
df4bce40digeff10 years ago | 8 | import {SettingsHelper} from "./settingsHelper"; |
190e393cMeena Kunnathur Balakrishnan10 years ago | 9 | import {Log} from "../common/log/log"; |
1c32fe84Patricio Beltran9 years ago | 10 | import {Packager, PackagerRunAs} from "../common/packager"; |
52f3873ddigeff10 years ago | 11 | import {AndroidPlatform} from "../common/android/androidPlatform"; |
ffffa686Meena Kunnathur Balakrishnan10 years ago | 12 | import {PackagerStatus, PackagerStatusIndicator} from "./packagerStatusIndicator"; |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 13 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
aeaf46bcMeena Kunnathur Balakrishnan10 years ago | 14 | import {TargetPlatformHelper} from "../common/targetPlatformHelper"; |
d976d077Meena Kunnathur Balakrishnan10 years ago | 15 | import {TelemetryHelper} from "../common/telemetryHelper"; |
1ed272e1digeff10 years ago | 16 | import {IOSDebugModeManager} from "../common/ios/iOSDebugModeManager"; |
1c32fe84Patricio Beltran9 years ago | 17 | import {ExponentHelper} from "../common/exponent/exponentHelper"; |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 18 | |
e1c05e69dlebu10 years ago | 19 | export class CommandPaletteHandler { |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 20 | private reactNativePackager: Packager; |
ffffa686Meena Kunnathur Balakrishnan10 years ago | 21 | private reactNativePackageStatusIndicator: PackagerStatusIndicator; |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 22 | private workspaceRoot: string; |
1c32fe84Patricio Beltran9 years ago | 23 | private exponentHelper: ExponentHelper; |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 24 | |
1c32fe84Patricio Beltran9 years ago | 25 | constructor(workspaceRoot: string, reactNativePackager: Packager, packagerStatusIndicator: PackagerStatusIndicator, exponentHelper: ExponentHelper) { |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 26 | this.workspaceRoot = workspaceRoot; |
a822ac85dlebu10 years ago | 27 | this.reactNativePackager = reactNativePackager; |
ffffa686Meena Kunnathur Balakrishnan10 years ago | 28 | this.reactNativePackageStatusIndicator = packagerStatusIndicator; |
1c32fe84Patricio Beltran9 years ago | 29 | this.exponentHelper = exponentHelper; |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 30 | } |
| 31 | | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 32 | /** |
| 33 | * Starts the React Native packager | |
| 34 | */ | |
10873e11digeff10 years ago | 35 | public startPackager(): Q.Promise<void> { |
b0af599cJimmy Thomson9 years ago | 36 | return this.executeCommandInContext("startPackager", () => |
| 37 | this.reactNativePackager.isRunning() | |
| 38 | .then((running) => { | |
| 39 | if (running) { | |
| 40 | return this.reactNativePackager.stop(); | |
| 41 | } | |
| 42 | }) | |
| 43 | ).then(() => | |
| 44 | this.exponentHelper.configureReactNativeEnvironment() | |
| 45 | ).then(() => this.runStartPackagerCommandAndUpdateStatus()); | |
1c32fe84Patricio Beltran9 years ago | 46 | } |
| 47 | | |
| 48 | /** | |
| 49 | * Starts the Exponent packager | |
| 50 | */ | |
| 51 | public startExponentPackager(): Q.Promise<void> { | |
b0af599cJimmy Thomson9 years ago | 52 | return this.executeCommandInContext("startExponentPackager", () => |
| 53 | this.reactNativePackager.isRunning() | |
| 54 | .then((running) => { | |
| 55 | if (running) { | |
| 56 | return this.reactNativePackager.stop(); | |
| 57 | } | |
| 58 | }) | |
| 59 | ).then(() => | |
| 60 | this.exponentHelper.configureExponentEnvironment() | |
| 61 | ).then(() => this.runStartPackagerCommandAndUpdateStatus(PackagerRunAs.EXPONENT)); | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 62 | } |
| 63 | | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 64 | /** |
| 65 | * Kills the React Native packager invoked by the extension's packager | |
| 66 | */ | |
10873e11digeff10 years ago | 67 | public stopPackager(): Q.Promise<void> { |
ffffa686Meena Kunnathur Balakrishnan10 years ago | 68 | return this.executeCommandInContext("stopPackager", () => this.reactNativePackager.stop()) |
| 69 | .then(() => this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STOPPED)); | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 70 | } |
| 71 | | |
f2a58eefBret Johnson9 years ago | 72 | /** |
| 73 | * Restarts the React Native packager | |
| 74 | */ | |
| 75 | public restartPackager(): Q.Promise<void> { | |
| 76 | return this.executeCommandInContext("restartPackager", () => | |
| 77 | this.runRestartPackagerCommandAndUpdateStatus()); | |
| 78 | } | |
| 79 | | |
7893fb7eJimmy Thomson9 years ago | 80 | /** |
| 81 | * Execute command to publish to exponent host. | |
| 82 | */ | |
| 83 | public publishToExpHost(): Q.Promise<void> { | |
| 84 | return this.executeCommandInContext("publishToExpHost", () => { | |
| 85 | this.executePublishToExpHost().then((didPublish) => { | |
| 86 | if (!didPublish) { | |
| 87 | Log.logMessage("Publishing was unsuccessful. Please make sure you are logged in Exponent and your project is a valid Exponentjs project"); | |
| 88 | } | |
| 89 | }); | |
| 90 | }); | |
| 91 | } | |
| 92 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 93 | /** |
| 94 | * Executes the 'react-native run-android' command | |
| 95 | */ | |
10873e11digeff10 years ago | 96 | public runAndroid(): Q.Promise<void> { |
2d8b9177Daniel Lebu10 years ago | 97 | TargetPlatformHelper.checkTargetPlatformSupport("android"); |
52f3873ddigeff10 years ago | 98 | return this.executeCommandInContext("runAndroid", () => this.executeWithPackagerRunning(() => { |
| 99 | const packagerPort = SettingsHelper.getPackagerPort(); | |
| 100 | return new AndroidPlatform({ projectRoot: this.workspaceRoot, packagerPort: packagerPort }).runApp(/*shouldLaunchInAllDevices*/true); | |
| 101 | })); | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 102 | } |
| 103 | | |
52f3873ddigeff10 years ago | 104 | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 105 | /** |
| 106 | * Executes the 'react-native run-ios' command | |
| 107 | */ | |
10873e11digeff10 years ago | 108 | public runIos(): Q.Promise<void> { |
6e7f90d8Daniel Lebu10 years ago | 109 | TargetPlatformHelper.checkTargetPlatformSupport("ios"); |
10873e11digeff10 years ago | 110 | return this.executeCommandInContext("runIos", () => { |
| 111 | // Set the Debugging setting to disabled, because in iOS it's persisted across runs of the app | |
| 112 | return new IOSDebugModeManager(this.workspaceRoot).setSimulatorJSDebuggingModeSetting(/*enable=*/ false) | |
| 113 | .catch(() => { }) // If setting the debugging mode fails, we ignore the error and we run the run ios command anyways | |
| 114 | .then(() => this.executeReactNativeRunCommand("run-ios")); | |
| 115 | }); | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 116 | } |
| 117 | | |
1c32fe84Patricio Beltran9 years ago | 118 | private runRestartPackagerCommandAndUpdateStatus(): Q.Promise<void> { |
| 119 | return this.reactNativePackager.restart(SettingsHelper.getPackagerPort()) | |
f2a58eefBret Johnson9 years ago | 120 | .then(() => this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STARTED)); |
| 121 | } | |
| 122 | | |
1c32fe84Patricio Beltran9 years ago | 123 | /** |
| 124 | * Helper method to run packager and update appropriate configurations | |
| 125 | */ | |
| 126 | private runStartPackagerCommandAndUpdateStatus(startAs: PackagerRunAs = PackagerRunAs.REACT_NATIVE): Q.Promise<any> { | |
| 127 | if (startAs === PackagerRunAs.EXPONENT) { | |
7059d307Patricio Beltran9 years ago | 128 | return this.loginToExponent() |
| 129 | .then(() => | |
| 130 | this.reactNativePackager.startAsExponent(SettingsHelper.getPackagerPort()) | |
| 131 | ).then(exponentUrl => { | |
1c32fe84Patricio Beltran9 years ago | 132 | this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.EXPONENT_PACKAGER_STARTED); |
| 133 | Log.logMessage("Application is running on Exponent."); | |
280c0746Patricio Beltran9 years ago | 134 | const exponentOutput = `Open your exponent app at ${exponentUrl}`; |
| 135 | Log.logMessage(exponentOutput); | |
| 136 | vscode.window.showInformationMessage(exponentOutput); | |
1c32fe84Patricio Beltran9 years ago | 137 | }); |
| 138 | } | |
| 139 | return this.reactNativePackager.startAsReactNative(SettingsHelper.getPackagerPort()) | |
5e651f3edigeff10 years ago | 140 | .then(() => this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STARTED)); |
| 141 | } | |
| 142 | | |
fb8f49fbMeena Kunnathur Balakrishnan10 years ago | 143 | /** |
3cf70711Meena Kunnathur Balakrishnan10 years ago | 144 | * Executes a react-native command passed after starting the packager |
fb8f49fbMeena Kunnathur Balakrishnan10 years ago | 145 | * {command} The command to be executed |
| 146 | * {args} The arguments to be passed to the command | |
| 147 | */ | |
b3a793eeNisheet Jain10 years ago | 148 | private executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> { |
52f3873ddigeff10 years ago | 149 | return this.executeWithPackagerRunning(() => { |
| 150 | return new CommandExecutor(this.workspaceRoot).spawnReactCommand(command, args).outcome; | |
| 151 | }); | |
| 152 | } | |
| 153 | | |
| 154 | /** | |
| 155 | * Executes a lambda function after starting the packager | |
| 156 | * {lambda} The lambda function to be executed | |
| 157 | */ | |
| 158 | private executeWithPackagerRunning(lambda: () => Q.Promise<void>): Q.Promise<void> { | |
ec6e115dMeena Kunnathur Balakrishnan10 years ago | 159 | // Start the packager before executing the React-Native command |
53520386Meena Kunnathur Balakrishnan10 years ago | 160 | Log.logMessage("Attempting to start the React Native packager"); |
52f3873ddigeff10 years ago | 161 | return this.runStartPackagerCommandAndUpdateStatus().then(lambda); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 162 | } |
b3a793eeNisheet Jain10 years ago | 163 | |
| 164 | /** | |
| 165 | * Ensures that we are in a React Native project and then executes the operation | |
| 166 | * Otherwise, displays an error message banner | |
| 167 | * {operation} - a function that performs the expected operation | |
| 168 | */ | |
5e651f3edigeff10 years ago | 169 | private executeCommandInContext(rnCommand: string, operation: () => Q.Promise<void> | void): Q.Promise<void> { |
38edb09eAlexander Sorokin9 years ago | 170 | let reactNativeProjectHelper = new ReactNativeProjectHelper(this.workspaceRoot); |
10873e11digeff10 years ago | 171 | return TelemetryHelper.generate("RNCommand", (generator) => { |
8512ccfeMeena Kunnathur Balakrishnan10 years ago | 172 | generator.add("command", rnCommand, false); |
| 173 | return reactNativeProjectHelper.isReactNativeProject().then(isRNProject => { | |
0633e07bMeena Kunnathur Balakrishnan10 years ago | 174 | generator.add("isRNProject", isRNProject, false); |
8512ccfeMeena Kunnathur Balakrishnan10 years ago | 175 | if (isRNProject) { |
cf138e34Meena Kunnathur Balakrishnan10 years ago | 176 | // Bring the log channel to focus |
f1e34747Meena Kunnathur Balakrishnan10 years ago | 177 | Log.setFocusOnLogChannel(); |
cf138e34Meena Kunnathur Balakrishnan10 years ago | 178 | |
| 179 | // Execute the operation | |
8512ccfeMeena Kunnathur Balakrishnan10 years ago | 180 | return operation(); |
| 181 | } else { | |
| 182 | vscode.window.showErrorMessage("Current workspace is not a React Native project."); | |
| 183 | } | |
| 184 | }); | |
10873e11digeff10 years ago | 185 | }); |
b3a793eeNisheet Jain10 years ago | 186 | } |
7893fb7eJimmy Thomson9 years ago | 187 | |
| 188 | /** | |
| 189 | * Publish project to exponent server. In order to do this we need to make sure the user is logged in exponent and the packager is running. | |
| 190 | */ | |
| 191 | private executePublishToExpHost(): Q.Promise<boolean> { | |
| 192 | Log.logMessage("Publishing app to Exponent server. This might take a moment."); | |
7059d307Patricio Beltran9 years ago | 193 | return this.loginToExponent() |
| 194 | .then(user => { | |
| 195 | Log.logMessage(`Publishing as ${user.username}...`); | |
| 196 | return this.startExponentPackager() | |
| 197 | .then(() => | |
| 198 | XDL.publish(this.workspaceRoot)) | |
| 199 | .then(response => { | |
| 200 | if (response.err || !response.url) { | |
| 201 | return false; | |
| 202 | } | |
| 203 | const publishedOutput = `App successfully published to ${response.url}`; | |
| 204 | Log.logMessage(publishedOutput); | |
| 205 | vscode.window.showInformationMessage(publishedOutput); | |
| 206 | return true; | |
| 207 | }); | |
| 208 | }).catch(() => { | |
| 209 | Log.logWarning("An error has occured. Please make sure you are logged in to exponent, your project is setup correctly for publishing and your packager is running as exponent."); | |
7893fb7eJimmy Thomson9 years ago | 210 | return false; |
7059d307Patricio Beltran9 years ago | 211 | }); |
| 212 | } | |
| 213 | | |
| 214 | private loginToExponent(): Q.Promise<XDL.IUser> { | |
| 215 | return this.exponentHelper.loginToExponent( | |
| 216 | (message, password) => { return Q(vscode.window.showInputBox({ placeHolder: message, password: password })); }, | |
| 217 | (message) => { return Q(vscode.window.showInformationMessage(message)); } | |
| 218 | ); | |
7893fb7eJimmy Thomson9 years ago | 219 | } |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 220 | } |