microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commandPaletteHandler.ts
356lines · 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 | |
| 4 | import * as vscode from "vscode"; |
| 5 | import * as Q from "q"; |
| 6 | import * as XDL from "./exponent/xdlInterface"; |
| 7 | import {SettingsHelper} from "./settingsHelper"; |
| 8 | import {OutputChannelLogger} from "./log/OutputChannelLogger"; |
| 9 | import {Packager} from "../common/packager"; |
| 10 | import {TargetType} from "./generalMobilePlatform"; |
| 11 | import {AndroidPlatform} from "./android/androidPlatform"; |
| 12 | import {IOSPlatform} from "./ios/iOSPlatform"; |
| 13 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
| 14 | import {TargetPlatformHelper} from "../common/targetPlatformHelper"; |
| 15 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 16 | import {ExponentHelper} from "./exponent/exponentHelper"; |
| 17 | import {ReactDirManager} from "./reactDirManager"; |
| 18 | import {ExtensionServer} from "./extensionServer"; |
| 19 | import {IAndroidRunOptions, IIOSRunOptions} from "./launchArgs"; |
| 20 | import { ExponentPlatform } from "./exponent/exponentPlatform"; |
| 21 | |
| 22 | interface IReactNativeStuff { |
| 23 | packager: Packager; |
| 24 | exponentHelper: ExponentHelper; |
| 25 | reactDirManager: ReactDirManager; |
| 26 | extensionServer: ExtensionServer; |
| 27 | } |
| 28 | |
| 29 | interface IReactNativeProject extends IReactNativeStuff { |
| 30 | workspaceFolder: vscode.WorkspaceFolder; |
| 31 | } |
| 32 | |
| 33 | export class CommandPaletteHandler { |
| 34 | private static projectsCache: {[key: string]: IReactNativeProject} = {}; |
| 35 | private static logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
| 36 | |
| 37 | public static addFolder(workspaceFolder: vscode.WorkspaceFolder, stuff: IReactNativeStuff): void { |
| 38 | this.projectsCache[workspaceFolder.uri.fsPath] = { |
| 39 | ...stuff, |
| 40 | workspaceFolder, |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | public static getFolder(workspaceFolder: vscode.WorkspaceFolder): IReactNativeProject { |
| 45 | return this.projectsCache[workspaceFolder.uri.fsPath]; |
| 46 | } |
| 47 | |
| 48 | public static delFolder(workspaceFolder: vscode.WorkspaceFolder): void { |
| 49 | delete this.projectsCache[workspaceFolder.uri.fsPath]; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Starts the React Native packager |
| 54 | */ |
| 55 | public static startPackager(): Q.Promise<void> { |
| 56 | return this.selectProject() |
| 57 | .then((project: IReactNativeProject) => { |
| 58 | return this.executeCommandInContext("startPackager", project.workspaceFolder, () => { |
| 59 | return project.packager.isRunning() |
| 60 | .then((running) => { |
| 61 | return running ? project.packager.stop() : Q.resolve(void 0); |
| 62 | }); |
| 63 | }) |
| 64 | .then(() => project.packager.start()); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Kills the React Native packager invoked by the extension's packager |
| 70 | */ |
| 71 | public static stopPackager(): Q.Promise<void> { |
| 72 | return this.selectProject() |
| 73 | .then((project: IReactNativeProject) => { |
| 74 | return this.executeCommandInContext("stopPackager", project.workspaceFolder, () => project.packager.stop()); |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | public static stopAllPackagers(): Q.Promise<void> { |
| 79 | let keys = Object.keys(this.projectsCache); |
| 80 | let promises: Q.Promise<void>[] = []; |
| 81 | keys.forEach((key) => { |
| 82 | let project = this.projectsCache[key]; |
| 83 | promises.push(this.executeCommandInContext("stopPackager", project.workspaceFolder, () => project.packager.stop())); |
| 84 | }); |
| 85 | |
| 86 | return Q.all(promises).then(() => {}); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Restarts the React Native packager |
| 91 | */ |
| 92 | public static restartPackager(): Q.Promise<void> { |
| 93 | return this.selectProject() |
| 94 | .then((project: IReactNativeProject) => { |
| 95 | return this.executeCommandInContext("restartPackager", project.workspaceFolder, () => |
| 96 | this.runRestartPackagerCommandAndUpdateStatus(project)); |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Execute command to publish to exponent host. |
| 102 | */ |
| 103 | public static publishToExpHost(): Q.Promise<void> { |
| 104 | return this.selectProject() |
| 105 | .then((project: IReactNativeProject) => { |
| 106 | return this.executeCommandInContext("publishToExpHost", project.workspaceFolder, () => { |
| 107 | return this.executePublishToExpHost(project).then((didPublish) => { |
| 108 | if (!didPublish) { |
| 109 | CommandPaletteHandler.logger.warning("Publishing was unsuccessful. Please make sure you are logged in Exponent and your project is a valid Exponentjs project"); |
| 110 | } |
| 111 | }); |
| 112 | }); |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Executes the 'react-native run-android' command |
| 118 | */ |
| 119 | public static runAndroid(target: TargetType = "simulator"): Q.Promise<void> { |
| 120 | return this.selectProject() |
| 121 | .then((project: IReactNativeProject) => { |
| 122 | TargetPlatformHelper.checkTargetPlatformSupport("android"); |
| 123 | return this.executeCommandInContext("runAndroid", project.workspaceFolder, () => { |
| 124 | const runOptions = CommandPaletteHandler.getRunOptions(project, "android", target); |
| 125 | const platform = new AndroidPlatform(runOptions, { |
| 126 | packager: project.packager, |
| 127 | }); |
| 128 | return platform.beforeStartPackager() |
| 129 | .then(() => { |
| 130 | return platform.startPackager(); |
| 131 | }) |
| 132 | .then(() => { |
| 133 | return platform.runApp(/*shouldLaunchInAllDevices*/true); |
| 134 | }) |
| 135 | .then(() => { |
| 136 | return platform.disableJSDebuggingMode(); |
| 137 | }); |
| 138 | }); |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Executes the 'react-native run-ios' command |
| 144 | */ |
| 145 | public static runIos(target: TargetType = "simulator"): Q.Promise<void> { |
| 146 | return this.selectProject() |
| 147 | .then((project: IReactNativeProject) => { |
| 148 | TargetPlatformHelper.checkTargetPlatformSupport("ios"); |
| 149 | return this.executeCommandInContext("runIos", project.workspaceFolder, () => { |
| 150 | const runOptions = CommandPaletteHandler.getRunOptions(project, "ios", target); |
| 151 | const platform = new IOSPlatform(runOptions, { |
| 152 | packager: project.packager, |
| 153 | }); |
| 154 | |
| 155 | return platform.beforeStartPackager() |
| 156 | .then(() => { |
| 157 | return platform.startPackager(); |
| 158 | }) |
| 159 | .then(() => { |
| 160 | // Set the Debugging setting to disabled, because in iOS it's persisted across runs of the app |
| 161 | return platform.disableJSDebuggingMode(); |
| 162 | }) |
| 163 | .catch(() => { }) // If setting the debugging mode fails, we ignore the error and we run the run ios command anyways |
| 164 | .then(() => { |
| 165 | return platform.runApp(); |
| 166 | }); |
| 167 | }); |
| 168 | }); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Starts the Exponent packager |
| 173 | */ |
| 174 | public static runExponent(): Q.Promise<void> { |
| 175 | return this.selectProject() |
| 176 | .then((project: IReactNativeProject) => { |
| 177 | return this.loginToExponent(project) |
| 178 | .then(() => { |
| 179 | return this.executeCommandInContext("runExponent", project.workspaceFolder, () => { |
| 180 | const runOptions = CommandPaletteHandler.getRunOptions(project, "exponent"); |
| 181 | const platform = new ExponentPlatform(runOptions, { |
| 182 | packager: project.packager, |
| 183 | }); |
| 184 | return platform.beforeStartPackager() |
| 185 | .then(() => { |
| 186 | return platform.startPackager(); |
| 187 | }) |
| 188 | .then(() => { |
| 189 | return platform.runApp(); |
| 190 | }); |
| 191 | }); |
| 192 | }); |
| 193 | }); |
| 194 | } |
| 195 | |
| 196 | public static showDevMenu(): Q.Promise<void> { |
| 197 | return this.selectProject() |
| 198 | .then((project: IReactNativeProject) => { |
| 199 | AndroidPlatform.showDevMenu() |
| 200 | .catch(() => { }); // Ignore any errors |
| 201 | IOSPlatform.showDevMenu(project.workspaceFolder.uri.fsPath) |
| 202 | .catch(() => { }); // Ignore any errors |
| 203 | return Q.resolve(void 0); |
| 204 | }); |
| 205 | } |
| 206 | |
| 207 | public static reloadApp(): Q.Promise<void> { |
| 208 | return this.selectProject() |
| 209 | .then((project: IReactNativeProject) => { |
| 210 | AndroidPlatform.reloadApp() |
| 211 | .catch(() => { }); // Ignore any errors |
| 212 | IOSPlatform.reloadApp(project.workspaceFolder.uri.fsPath) |
| 213 | .catch(() => { }); // Ignore any errors |
| 214 | return Q.resolve(void 0); |
| 215 | }); |
| 216 | } |
| 217 | |
| 218 | public static getPlatformByCommandName(commandName: string): string { |
| 219 | commandName = commandName.toLocaleLowerCase(); |
| 220 | |
| 221 | if (commandName.indexOf("android") > -1) { |
| 222 | return "android"; |
| 223 | } |
| 224 | |
| 225 | if (commandName.indexOf("ios") > -1) { |
| 226 | return "ios"; |
| 227 | } |
| 228 | |
| 229 | if (commandName.indexOf("exponent") > -1) { |
| 230 | return "exponent"; |
| 231 | } |
| 232 | |
| 233 | return ""; |
| 234 | } |
| 235 | |
| 236 | private static runRestartPackagerCommandAndUpdateStatus(project: IReactNativeProject): Q.Promise<void> { |
| 237 | return project.packager.restart(SettingsHelper.getPackagerPort(project.workspaceFolder.uri.fsPath)); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Ensures that we are in a React Native project and then executes the operation |
| 242 | * Otherwise, displays an error message banner |
| 243 | * {operation} - a function that performs the expected operation |
| 244 | */ |
| 245 | private static executeCommandInContext(rnCommand: string, workspaceFolder: vscode.WorkspaceFolder, operation: () => Q.Promise<void>): Q.Promise<void> { |
| 246 | const extProps = { |
| 247 | platform: { |
| 248 | value: CommandPaletteHandler.getPlatformByCommandName(rnCommand), |
| 249 | isPii: false, |
| 250 | }, |
| 251 | }; |
| 252 | |
| 253 | return TelemetryHelper.generate("RNCommand", extProps, (generator) => { |
| 254 | generator.add("command", rnCommand, false); |
| 255 | const projectRoot = SettingsHelper.getReactNativeProjectRoot(workspaceFolder.uri.fsPath); |
| 256 | return ReactNativeProjectHelper.isReactNativeProject(projectRoot).then(isRNProject => { |
| 257 | generator.add("isRNProject", isRNProject, false); |
| 258 | if (isRNProject) { |
| 259 | // Bring the log channel to focus |
| 260 | CommandPaletteHandler.logger.setFocusOnLogChannel(); |
| 261 | |
| 262 | // Execute the operation |
| 263 | return operation(); |
| 264 | } else { |
| 265 | vscode.window.showErrorMessage("Current workspace is not a React Native project."); |
| 266 | return; |
| 267 | } |
| 268 | }); |
| 269 | }); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * 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. |
| 274 | */ |
| 275 | private static executePublishToExpHost(project: IReactNativeProject): Q.Promise<boolean> { |
| 276 | CommandPaletteHandler.logger.info("Publishing app to Exponent server. This might take a moment."); |
| 277 | return this.loginToExponent(project) |
| 278 | .then(user => { |
| 279 | CommandPaletteHandler.logger.debug(`Publishing as ${user.username}...`); |
| 280 | return this.runExponent() |
| 281 | .then(() => |
| 282 | XDL.publish(project.workspaceFolder.uri.fsPath)) |
| 283 | .then(response => { |
| 284 | if (response.err || !response.url) { |
| 285 | return false; |
| 286 | } |
| 287 | const publishedOutput = `App successfully published to ${response.url}`; |
| 288 | CommandPaletteHandler.logger.info(publishedOutput); |
| 289 | vscode.window.showInformationMessage(publishedOutput); |
| 290 | return true; |
| 291 | }); |
| 292 | }); |
| 293 | } |
| 294 | |
| 295 | private static loginToExponent(project: IReactNativeProject): Q.Promise<XDL.IUser> { |
| 296 | return project.exponentHelper.loginToExponent( |
| 297 | (message, password) => { |
| 298 | return Q.Promise((resolve, reject) => { |
| 299 | vscode.window.showInputBox({ placeHolder: message, password: password }) |
| 300 | .then(login => { |
| 301 | resolve(login || ""); |
| 302 | }, reject); |
| 303 | }); |
| 304 | }, |
| 305 | (message) => { |
| 306 | return Q.Promise((resolve, reject) => { |
| 307 | vscode.window.showInformationMessage(message) |
| 308 | .then(password => { |
| 309 | resolve(password || ""); |
| 310 | }, reject); |
| 311 | }); |
| 312 | } |
| 313 | ) |
| 314 | .catch((err) => { |
| 315 | 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."); |
| 316 | throw err; |
| 317 | }); |
| 318 | } |
| 319 | |
| 320 | private static selectProject(): Q.Promise<IReactNativeProject> { |
| 321 | let keys = Object.keys(this.projectsCache); |
| 322 | if (keys.length > 1) { |
| 323 | return Q.Promise((resolve, reject) => { |
| 324 | vscode.window.showQuickPick(keys) |
| 325 | .then((selected) => { |
| 326 | if (selected) { |
| 327 | resolve(this.projectsCache[selected]); |
| 328 | } |
| 329 | }, reject); |
| 330 | }); |
| 331 | } else if (keys.length === 1) { |
| 332 | return Q.resolve(this.projectsCache[keys[0]]); |
| 333 | } else { |
| 334 | return Q.reject(new Error("Current workspace is not a React Native project.")); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | private static getRunOptions(project: IReactNativeProject, platform: "ios" | "android" | "exponent", target: TargetType = "simulator"): IAndroidRunOptions | IIOSRunOptions { |
| 339 | const packagerPort = SettingsHelper.getPackagerPort(project.workspaceFolder.uri.fsPath); |
| 340 | const runArgs = SettingsHelper.getRunArgs(platform, target, project.workspaceFolder.uri); |
| 341 | const envArgs = SettingsHelper.getEnvArgs(platform, target, project.workspaceFolder.uri); |
| 342 | const envFile = SettingsHelper.getEnvFile(platform, target, project.workspaceFolder.uri); |
| 343 | const projectRoot = SettingsHelper.getReactNativeProjectRoot(project.workspaceFolder.uri.fsPath); |
| 344 | const runOptions: IAndroidRunOptions | IIOSRunOptions = { |
| 345 | platform: platform, |
| 346 | workspaceRoot: project.workspaceFolder.uri.fsPath, |
| 347 | projectRoot: projectRoot, |
| 348 | packagerPort: packagerPort, |
| 349 | runArguments: runArgs, |
| 350 | env: envArgs, |
| 351 | envFile: envFile, |
| 352 | }; |
| 353 | |
| 354 | return runOptions; |
| 355 | } |
| 356 | } |