microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commandPaletteHandler.ts
320lines · 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"; | |
0a68f8dbArtem Egorov8 years ago | 6 | import * as XDL from "./exponent/xdlInterface"; |
df4bce40digeff10 years ago | 7 | import {SettingsHelper} from "./settingsHelper"; |
0a68f8dbArtem Egorov8 years ago | 8 | import {OutputChannelLogger} from "./log/OutputChannelLogger"; |
1c32fe84Patricio Beltran9 years ago | 9 | import {Packager, PackagerRunAs} from "../common/packager"; |
0a68f8dbArtem Egorov8 years ago | 10 | import {AndroidPlatform} from "./android/androidPlatform"; |
| 11 | import {IOSPlatform} from "./ios/iOSPlatform"; | |
2e432a9eArtem Egorov8 years ago | 12 | import {PackagerStatus} 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"; |
0a68f8dbArtem Egorov8 years ago | 16 | import {ExponentHelper} from "./exponent/exponentHelper"; |
4edcda70Artem Egorov8 years ago | 17 | import {ReactDirManager} from "./reactDirManager"; |
| 18 | import {ExtensionServer} from "./extensionServer"; | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 19 | |
4edcda70Artem Egorov8 years ago | 20 | interface IReactNativeStuff { |
| 21 | packager: Packager; | |
2e432a9eArtem Egorov8 years ago | 22 | exponentHelper: ExponentHelper; |
4edcda70Artem Egorov8 years ago | 23 | reactDirManager: ReactDirManager; |
| 24 | extensionServer: ExtensionServer; | |
| 25 | } | |
| 26 | interface IReactNativeProject extends IReactNativeStuff { | |
2e432a9eArtem Egorov8 years ago | 27 | workspaceFolder: vscode.WorkspaceFolder; |
| 28 | } | |
| 29 | | |
e1c05e69dlebu10 years ago | 30 | export class CommandPaletteHandler { |
4edcda70Artem Egorov8 years ago | 31 | private static projectsCache: {[key: string]: IReactNativeProject} = {}; |
2e432a9eArtem Egorov8 years ago | 32 | private static logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
| 33 | | |
4edcda70Artem Egorov8 years ago | 34 | public static addFolder(workspaceFolder: vscode.WorkspaceFolder, stuff: IReactNativeStuff): void { |
| 35 | this.projectsCache[workspaceFolder.uri.fsPath] = { | |
| 36 | ...stuff, | |
2e432a9eArtem Egorov8 years ago | 37 | workspaceFolder, |
| 38 | }; | |
| 39 | } | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 40 | |
4edcda70Artem Egorov8 years ago | 41 | public static getFolder(workspaceFolder: vscode.WorkspaceFolder): IReactNativeProject { |
| 42 | return this.projectsCache[workspaceFolder.uri.fsPath]; | |
2e432a9eArtem Egorov8 years ago | 43 | } |
| 44 | | |
4edcda70Artem Egorov8 years ago | 45 | public static delFolder(workspaceFolder: vscode.WorkspaceFolder): void { |
| 46 | delete this.projectsCache[workspaceFolder.uri.fsPath]; | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 47 | } |
| 48 | | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 49 | /** |
| 50 | * Starts the React Native packager | |
| 51 | */ | |
2e432a9eArtem Egorov8 years ago | 52 | public static startPackager(): Q.Promise<void> { |
| 53 | return this.selectProject() | |
| 54 | .then((project: IReactNativeProject) => { | |
| 55 | return this.executeCommandInContext("startPackager", project.workspaceFolder, () => | |
4edcda70Artem Egorov8 years ago | 56 | project.packager.isRunning() |
2e432a9eArtem Egorov8 years ago | 57 | .then((running) => { |
4edcda70Artem Egorov8 years ago | 58 | return running ? project.packager.stop() : Q.resolve(void 0); |
2e432a9eArtem Egorov8 years ago | 59 | }) |
| 60 | ) | |
| 61 | .then(() => this.runStartPackagerCommandAndUpdateStatus(project)); | |
| 62 | }); | |
1c32fe84Patricio Beltran9 years ago | 63 | } |
| 64 | | |
| 65 | /** | |
| 66 | * Starts the Exponent packager | |
| 67 | */ | |
2e432a9eArtem Egorov8 years ago | 68 | public static startExponentPackager(): Q.Promise<void> { |
| 69 | return this.selectProject() | |
| 70 | .then((project: IReactNativeProject) => { | |
| 71 | return this.executeCommandInContext("startExponentPackager", project.workspaceFolder, () => | |
4edcda70Artem Egorov8 years ago | 72 | project.packager.isRunning() |
2e432a9eArtem Egorov8 years ago | 73 | .then((running) => { |
4edcda70Artem Egorov8 years ago | 74 | return running ? project.packager.stop() : Q.resolve(void 0); |
2e432a9eArtem Egorov8 years ago | 75 | }) |
| 76 | ).then(() => | |
| 77 | project.exponentHelper.configureExponentEnvironment() | |
| 78 | ).then(() => this.runStartPackagerCommandAndUpdateStatus(project, PackagerRunAs.EXPONENT)); | |
| 79 | }); | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 80 | } |
| 81 | | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 82 | /** |
| 83 | * Kills the React Native packager invoked by the extension's packager | |
| 84 | */ | |
2e432a9eArtem Egorov8 years ago | 85 | public static stopPackager(): Q.Promise<void> { |
| 86 | return this.selectProject() | |
| 87 | .then((project: IReactNativeProject) => { | |
4edcda70Artem Egorov8 years ago | 88 | return this.executeCommandInContext("stopPackager", project.workspaceFolder, () => project.packager.stop()) |
| 89 | .then(() => project.packager.statusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STOPPED)); | |
2e432a9eArtem Egorov8 years ago | 90 | }); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 91 | } |
| 92 | | |
4edcda70Artem Egorov8 years ago | 93 | public static stopAllPackagers(): Q.Promise<void> { |
| 94 | let keys = Object.keys(this.projectsCache); | |
| 95 | let promises: Q.Promise<void>[] = []; | |
| 96 | keys.forEach((key) => { | |
| 97 | let project = this.projectsCache[key]; | |
| 98 | promises.push(this.executeCommandInContext("stopPackager", project.workspaceFolder, () => project.packager.stop()) | |
| 99 | .then(() => project.packager.statusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STOPPED))); | |
| 100 | }); | |
| 101 | | |
| 102 | return Q.all(promises).then(() => {}); | |
| 103 | } | |
| 104 | | |
f2a58eefBret Johnson9 years ago | 105 | /** |
| 106 | * Restarts the React Native packager | |
| 107 | */ | |
2e432a9eArtem Egorov8 years ago | 108 | public static restartPackager(): Q.Promise<void> { |
| 109 | return this.selectProject() | |
| 110 | .then((project: IReactNativeProject) => { | |
| 111 | return this.executeCommandInContext("restartPackager", project.workspaceFolder, () => | |
| 112 | this.runRestartPackagerCommandAndUpdateStatus(project)); | |
| 113 | }); | |
f2a58eefBret Johnson9 years ago | 114 | } |
| 115 | | |
7893fb7eJimmy Thomson9 years ago | 116 | /** |
| 117 | * Execute command to publish to exponent host. | |
| 118 | */ | |
2e432a9eArtem Egorov8 years ago | 119 | public static publishToExpHost(): Q.Promise<void> { |
| 120 | return this.selectProject() | |
| 121 | .then((project: IReactNativeProject) => { | |
| 122 | return this.executeCommandInContext("publishToExpHost", project.workspaceFolder, () => { | |
| 123 | return this.executePublishToExpHost(project).then((didPublish) => { | |
| 124 | if (!didPublish) { | |
| 125 | CommandPaletteHandler.logger.warning("Publishing was unsuccessful. Please make sure you are logged in Exponent and your project is a valid Exponentjs project"); | |
| 126 | } | |
| 127 | }); | |
| 128 | }); | |
7893fb7eJimmy Thomson9 years ago | 129 | }); |
| 130 | } | |
| 131 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 132 | /** |
| 133 | * Executes the 'react-native run-android' command | |
| 134 | */ | |
2e432a9eArtem Egorov8 years ago | 135 | public static runAndroid(target: "device" | "simulator" = "simulator"): Q.Promise<void> { |
2d8b9177Daniel Lebu10 years ago | 136 | TargetPlatformHelper.checkTargetPlatformSupport("android"); |
2e432a9eArtem Egorov8 years ago | 137 | return this.selectProject() |
| 138 | .then((project: IReactNativeProject) => { | |
| 139 | return this.executeCommandInContext("runAndroid", project.workspaceFolder, () => this.executeWithPackagerRunning(project, () => { | |
a41f5c68Artem Egorov8 years ago | 140 | const packagerPort = SettingsHelper.getPackagerPort(project.workspaceFolder.uri.fsPath); |
4edcda70Artem Egorov8 years ago | 141 | const runArgs = SettingsHelper.getRunArgs("android", target, project.workspaceFolder.uri); |
a41f5c68Artem Egorov8 years ago | 142 | const platform = new AndroidPlatform({ platform: "android", workspaceRoot: project.workspaceFolder.uri.fsPath, projectRoot: project.workspaceFolder.uri.fsPath, packagerPort: packagerPort, runArguments: runArgs }, { |
4edcda70Artem Egorov8 years ago | 143 | packager: project.packager, |
2e432a9eArtem Egorov8 years ago | 144 | }); |
| 145 | return platform.runApp(/*shouldLaunchInAllDevices*/true) | |
| 146 | .then(() => { | |
| 147 | return platform.disableJSDebuggingMode(); | |
| 148 | }); | |
| 149 | })); | |
0a68f8dbArtem Egorov8 years ago | 150 | }); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 151 | } |
| 152 | | |
| 153 | /** | |
| 154 | * Executes the 'react-native run-ios' command | |
| 155 | */ | |
2e432a9eArtem Egorov8 years ago | 156 | public static runIos(target: "device" | "simulator" = "simulator"): Q.Promise<void> { |
6e7f90d8Daniel Lebu10 years ago | 157 | TargetPlatformHelper.checkTargetPlatformSupport("ios"); |
2e432a9eArtem Egorov8 years ago | 158 | return this.selectProject() |
| 159 | .then((project: IReactNativeProject) => { | |
| 160 | return this.executeCommandInContext("runIos", project.workspaceFolder, () => this.executeWithPackagerRunning(project, () => { | |
a41f5c68Artem Egorov8 years ago | 161 | const packagerPort = SettingsHelper.getPackagerPort(project.workspaceFolder.uri.fsPath); |
4edcda70Artem Egorov8 years ago | 162 | const runArgs = SettingsHelper.getRunArgs("ios", target, project.workspaceFolder.uri); |
| 163 | const platform = new IOSPlatform({ platform: "ios", workspaceRoot: project.workspaceFolder.uri.fsPath, projectRoot: project.workspaceFolder.uri.fsPath, packagerPort, runArguments: runArgs }, { packager: project.packager }); | |
2e432a9eArtem Egorov8 years ago | 164 | |
| 165 | // Set the Debugging setting to disabled, because in iOS it's persisted across runs of the app | |
| 166 | return platform.disableJSDebuggingMode() | |
| 167 | .catch(() => { }) // If setting the debugging mode fails, we ignore the error and we run the run ios command anyways | |
| 168 | .then(() => { | |
| 169 | return platform.runApp(); | |
| 170 | }); | |
| 171 | })); | |
| 172 | }); | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 173 | } |
| 174 | | |
2e432a9eArtem Egorov8 years ago | 175 | public static showDevMenu(): Q.Promise<void> { |
a41f5c68Artem Egorov8 years ago | 176 | return this.selectProject() |
| 177 | .then((project: IReactNativeProject) => { | |
| 178 | AndroidPlatform.showDevMenu() | |
| 179 | .catch(() => { }); // Ignore any errors | |
4edcda70Artem Egorov8 years ago | 180 | IOSPlatform.showDevMenu(project.workspaceFolder.uri.fsPath) |
a41f5c68Artem Egorov8 years ago | 181 | .catch(() => { }); // Ignore any errors |
| 182 | return Q.resolve(void 0); | |
| 183 | }); | |
7daed3fcArtem Egorov8 years ago | 184 | } |
| 185 | | |
2e432a9eArtem Egorov8 years ago | 186 | public static reloadApp(): Q.Promise<void> { |
a41f5c68Artem Egorov8 years ago | 187 | return this.selectProject() |
| 188 | .then((project: IReactNativeProject) => { | |
| 189 | AndroidPlatform.reloadApp() | |
| 190 | .catch(() => { }); // Ignore any errors | |
4edcda70Artem Egorov8 years ago | 191 | IOSPlatform.reloadApp(project.workspaceFolder.uri.fsPath) |
a41f5c68Artem Egorov8 years ago | 192 | .catch(() => { }); // Ignore any errors |
| 193 | return Q.resolve(void 0); | |
| 194 | }); | |
7daed3fcArtem Egorov8 years ago | 195 | } |
| 196 | | |
2e432a9eArtem Egorov8 years ago | 197 | private static runRestartPackagerCommandAndUpdateStatus(project: IReactNativeProject): Q.Promise<void> { |
4edcda70Artem Egorov8 years ago | 198 | return project.packager.restart(SettingsHelper.getPackagerPort(project.workspaceFolder.uri.fsPath)) |
| 199 | .then(() => project.packager.statusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STARTED)); | |
f2a58eefBret Johnson9 years ago | 200 | } |
| 201 | | |
1c32fe84Patricio Beltran9 years ago | 202 | /** |
| 203 | * Helper method to run packager and update appropriate configurations | |
| 204 | */ | |
2e432a9eArtem Egorov8 years ago | 205 | private static runStartPackagerCommandAndUpdateStatus(project: IReactNativeProject, startAs: PackagerRunAs = PackagerRunAs.REACT_NATIVE): Q.Promise<any> { |
1c32fe84Patricio Beltran9 years ago | 206 | if (startAs === PackagerRunAs.EXPONENT) { |
2e432a9eArtem Egorov8 years ago | 207 | return this.loginToExponent(project) |
7059d307Patricio Beltran9 years ago | 208 | .then(() => |
4edcda70Artem Egorov8 years ago | 209 | project.packager.startAsExponent() |
7059d307Patricio Beltran9 years ago | 210 | ).then(exponentUrl => { |
4edcda70Artem Egorov8 years ago | 211 | project.packager.statusIndicator.updatePackagerStatus(PackagerStatus.EXPONENT_PACKAGER_STARTED); |
2e432a9eArtem Egorov8 years ago | 212 | CommandPaletteHandler.logger.info("Application is running on Exponent."); |
280c0746Patricio Beltran9 years ago | 213 | const exponentOutput = `Open your exponent app at ${exponentUrl}`; |
2e432a9eArtem Egorov8 years ago | 214 | CommandPaletteHandler.logger.info(exponentOutput); |
e23fa745Vladimir Kotikov9 years ago | 215 | vscode.commands.executeCommand("vscode.previewHtml", vscode.Uri.parse(exponentUrl), 1, "Expo QR code"); |
1c32fe84Patricio Beltran9 years ago | 216 | }); |
| 217 | } | |
4edcda70Artem Egorov8 years ago | 218 | return project.packager.startAsReactNative() |
| 219 | .then(() => project.packager.statusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STARTED)); | |
5e651f3edigeff10 years ago | 220 | } |
| 221 | | |
52f3873ddigeff10 years ago | 222 | /** |
| 223 | * Executes a lambda function after starting the packager | |
| 224 | * {lambda} The lambda function to be executed | |
| 225 | */ | |
2e432a9eArtem Egorov8 years ago | 226 | private static executeWithPackagerRunning(project: IReactNativeProject, lambda: () => Q.Promise<void>): Q.Promise<void> { |
ec6e115dMeena Kunnathur Balakrishnan10 years ago | 227 | // Start the packager before executing the React-Native command |
2e432a9eArtem Egorov8 years ago | 228 | CommandPaletteHandler.logger.info("Attempting to start the React Native packager"); |
| 229 | return this.runStartPackagerCommandAndUpdateStatus(project).then(lambda); | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 230 | } |
b3a793eeNisheet Jain10 years ago | 231 | |
| 232 | /** | |
| 233 | * Ensures that we are in a React Native project and then executes the operation | |
| 234 | * Otherwise, displays an error message banner | |
| 235 | * {operation} - a function that performs the expected operation | |
| 236 | */ | |
2e432a9eArtem Egorov8 years ago | 237 | private static executeCommandInContext(rnCommand: string, workspaceFolder: vscode.WorkspaceFolder, operation: () => Q.Promise<void>): Q.Promise<void> { |
10873e11digeff10 years ago | 238 | return TelemetryHelper.generate("RNCommand", (generator) => { |
8512ccfeMeena Kunnathur Balakrishnan10 years ago | 239 | generator.add("command", rnCommand, false); |
a41f5c68Artem Egorov8 years ago | 240 | return ReactNativeProjectHelper.isReactNativeProject(workspaceFolder.uri.fsPath).then(isRNProject => { |
0633e07bMeena Kunnathur Balakrishnan10 years ago | 241 | generator.add("isRNProject", isRNProject, false); |
8512ccfeMeena Kunnathur Balakrishnan10 years ago | 242 | if (isRNProject) { |
cf138e34Meena Kunnathur Balakrishnan10 years ago | 243 | // Bring the log channel to focus |
2e432a9eArtem Egorov8 years ago | 244 | CommandPaletteHandler.logger.setFocusOnLogChannel(); |
cf138e34Meena Kunnathur Balakrishnan10 years ago | 245 | |
| 246 | // Execute the operation | |
8512ccfeMeena Kunnathur Balakrishnan10 years ago | 247 | return operation(); |
| 248 | } else { | |
| 249 | vscode.window.showErrorMessage("Current workspace is not a React Native project."); | |
3c172a05Artem Egorov8 years ago | 250 | return; |
8512ccfeMeena Kunnathur Balakrishnan10 years ago | 251 | } |
| 252 | }); | |
10873e11digeff10 years ago | 253 | }); |
b3a793eeNisheet Jain10 years ago | 254 | } |
7893fb7eJimmy Thomson9 years ago | 255 | |
| 256 | /** | |
| 257 | * 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. | |
| 258 | */ | |
2e432a9eArtem Egorov8 years ago | 259 | private static executePublishToExpHost(project: IReactNativeProject): Q.Promise<boolean> { |
| 260 | CommandPaletteHandler.logger.info("Publishing app to Exponent server. This might take a moment."); | |
| 261 | return this.loginToExponent(project) | |
7059d307Patricio Beltran9 years ago | 262 | .then(user => { |
2e432a9eArtem Egorov8 years ago | 263 | CommandPaletteHandler.logger.debug(`Publishing as ${user.username}...`); |
7059d307Patricio Beltran9 years ago | 264 | return this.startExponentPackager() |
| 265 | .then(() => | |
a41f5c68Artem Egorov8 years ago | 266 | XDL.publish(project.workspaceFolder.uri.fsPath)) |
7059d307Patricio Beltran9 years ago | 267 | .then(response => { |
| 268 | if (response.err || !response.url) { | |
| 269 | return false; | |
| 270 | } | |
| 271 | const publishedOutput = `App successfully published to ${response.url}`; | |
2e432a9eArtem Egorov8 years ago | 272 | CommandPaletteHandler.logger.info(publishedOutput); |
7059d307Patricio Beltran9 years ago | 273 | vscode.window.showInformationMessage(publishedOutput); |
| 274 | return true; | |
| 275 | }); | |
| 276 | }).catch(() => { | |
2e432a9eArtem Egorov8 years ago | 277 | CommandPaletteHandler.logger.warning("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 | 278 | return false; |
7059d307Patricio Beltran9 years ago | 279 | }); |
| 280 | } | |
| 281 | | |
2e432a9eArtem Egorov8 years ago | 282 | private static loginToExponent(project: IReactNativeProject): Q.Promise<XDL.IUser> { |
| 283 | return project.exponentHelper.loginToExponent( | |
5c8365a6Artem Egorov8 years ago | 284 | (message, password) => { |
| 285 | return Q.Promise((resolve, reject) => { | |
| 286 | vscode.window.showInputBox({ placeHolder: message, password: password }) | |
2e432a9eArtem Egorov8 years ago | 287 | .then(login => { |
| 288 | resolve(login || ""); | |
| 289 | }, reject); | |
5c8365a6Artem Egorov8 years ago | 290 | }); |
| 291 | }, | |
| 292 | (message) => { | |
| 293 | return Q.Promise((resolve, reject) => { | |
| 294 | vscode.window.showInformationMessage(message) | |
2e432a9eArtem Egorov8 years ago | 295 | .then(password => { |
| 296 | resolve(password || ""); | |
| 297 | }, reject); | |
5c8365a6Artem Egorov8 years ago | 298 | }); |
| 299 | } | |
7059d307Patricio Beltran9 years ago | 300 | ); |
7893fb7eJimmy Thomson9 years ago | 301 | } |
2e432a9eArtem Egorov8 years ago | 302 | |
| 303 | private static selectProject(): Q.Promise<IReactNativeProject> { | |
| 304 | let keys = Object.keys(this.projectsCache); | |
| 305 | if (keys.length > 1) { | |
| 306 | return Q.Promise((resolve, reject) => { | |
| 307 | vscode.window.showQuickPick(keys) | |
| 308 | .then((selected) => { | |
| 309 | if (selected) { | |
| 310 | resolve(this.projectsCache[selected]); | |
| 311 | } | |
| 312 | }, reject); | |
| 313 | }); | |
| 314 | } else if (keys.length === 1) { | |
| 315 | return Q.resolve(this.projectsCache[keys[0]]); | |
| 316 | } else { | |
| 317 | return Q.reject(); | |
| 318 | } | |
| 319 | } | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 320 | } |