microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commandPaletteHandler.ts
356lines · 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"; |
4787ec09Artem Egorov8 years ago | 9 | import {Packager} from "../common/packager"; |
634e3ecaRuslan Bikkinin7 years ago | 10 | import {TargetType} from "./generalMobilePlatform"; |
0a68f8dbArtem Egorov8 years ago | 11 | import {AndroidPlatform} from "./android/androidPlatform"; |
| 12 | import {IOSPlatform} from "./ios/iOSPlatform"; | |
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"; | |
d1fc7f8aArtem Egorov8 years ago | 19 | import {IAndroidRunOptions, IIOSRunOptions} from "./launchArgs"; |
4787ec09Artem Egorov8 years ago | 20 | import { ExponentPlatform } from "./exponent/exponentPlatform"; |
4b37483dmax-mironov8 years ago | 21 | |
4edcda70Artem Egorov8 years ago | 22 | interface IReactNativeStuff { |
| 23 | packager: Packager; | |
2e432a9eArtem Egorov8 years ago | 24 | exponentHelper: ExponentHelper; |
4edcda70Artem Egorov8 years ago | 25 | reactDirManager: ReactDirManager; |
| 26 | extensionServer: ExtensionServer; | |
| 27 | } | |
640e6e98max-mironov8 years ago | 28 | |
4edcda70Artem Egorov8 years ago | 29 | interface IReactNativeProject extends IReactNativeStuff { |
2e432a9eArtem Egorov8 years ago | 30 | workspaceFolder: vscode.WorkspaceFolder; |
| 31 | } | |
| 32 | | |
e1c05e69dlebu10 years ago | 33 | export class CommandPaletteHandler { |
4edcda70Artem Egorov8 years ago | 34 | private static projectsCache: {[key: string]: IReactNativeProject} = {}; |
2e432a9eArtem Egorov8 years ago | 35 | private static logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
6a465861max-mironov8 years ago | 36 | |
4edcda70Artem Egorov8 years ago | 37 | public static addFolder(workspaceFolder: vscode.WorkspaceFolder, stuff: IReactNativeStuff): void { |
| 38 | this.projectsCache[workspaceFolder.uri.fsPath] = { | |
| 39 | ...stuff, | |
2e432a9eArtem Egorov8 years ago | 40 | workspaceFolder, |
| 41 | }; | |
| 42 | } | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 43 | |
4edcda70Artem Egorov8 years ago | 44 | public static getFolder(workspaceFolder: vscode.WorkspaceFolder): IReactNativeProject { |
| 45 | return this.projectsCache[workspaceFolder.uri.fsPath]; | |
2e432a9eArtem Egorov8 years ago | 46 | } |
| 47 | | |
4edcda70Artem Egorov8 years ago | 48 | public static delFolder(workspaceFolder: vscode.WorkspaceFolder): void { |
| 49 | delete this.projectsCache[workspaceFolder.uri.fsPath]; | |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 50 | } |
| 51 | | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 52 | /** |
| 53 | * Starts the React Native packager | |
| 54 | */ | |
2e432a9eArtem Egorov8 years ago | 55 | public static startPackager(): Q.Promise<void> { |
| 56 | return this.selectProject() | |
| 57 | .then((project: IReactNativeProject) => { | |
4787ec09Artem Egorov8 years ago | 58 | return this.executeCommandInContext("startPackager", project.workspaceFolder, () => { |
| 59 | return project.packager.isRunning() | |
2e432a9eArtem Egorov8 years ago | 60 | .then((running) => { |
4edcda70Artem Egorov8 years ago | 61 | return running ? project.packager.stop() : Q.resolve(void 0); |
4787ec09Artem Egorov8 years ago | 62 | }); |
| 63 | }) | |
| 64 | .then(() => project.packager.start()); | |
2e432a9eArtem Egorov8 years ago | 65 | }); |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 66 | } |
| 67 | | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 68 | /** |
| 69 | * Kills the React Native packager invoked by the extension's packager | |
| 70 | */ | |
2e432a9eArtem Egorov8 years ago | 71 | public static stopPackager(): Q.Promise<void> { |
| 72 | return this.selectProject() | |
| 73 | .then((project: IReactNativeProject) => { | |
4787ec09Artem Egorov8 years ago | 74 | return this.executeCommandInContext("stopPackager", project.workspaceFolder, () => project.packager.stop()); |
2e432a9eArtem Egorov8 years ago | 75 | }); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 76 | } |
| 77 | | |
4edcda70Artem Egorov8 years ago | 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]; | |
4787ec09Artem Egorov8 years ago | 83 | promises.push(this.executeCommandInContext("stopPackager", project.workspaceFolder, () => project.packager.stop())); |
4edcda70Artem Egorov8 years ago | 84 | }); |
| 85 | | |
| 86 | return Q.all(promises).then(() => {}); | |
| 87 | } | |
| 88 | | |
f2a58eefBret Johnson9 years ago | 89 | /** |
| 90 | * Restarts the React Native packager | |
| 91 | */ | |
2e432a9eArtem Egorov8 years ago | 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 | }); | |
f2a58eefBret Johnson9 years ago | 98 | } |
| 99 | | |
7893fb7eJimmy Thomson9 years ago | 100 | /** |
| 101 | * Execute command to publish to exponent host. | |
| 102 | */ | |
2e432a9eArtem Egorov8 years ago | 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 | }); | |
7893fb7eJimmy Thomson9 years ago | 113 | }); |
| 114 | } | |
| 115 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 116 | /** |
| 117 | * Executes the 'react-native run-android' command | |
| 118 | */ | |
d1fc7f8aArtem Egorov8 years ago | 119 | public static runAndroid(target: TargetType = "simulator"): Q.Promise<void> { |
2e432a9eArtem Egorov8 years ago | 120 | return this.selectProject() |
| 121 | .then((project: IReactNativeProject) => { | |
0611729dRuslan Bikkinin7 years ago | 122 | TargetPlatformHelper.checkTargetPlatformSupport("android"); |
4787ec09Artem Egorov8 years ago | 123 | return this.executeCommandInContext("runAndroid", project.workspaceFolder, () => { |
634e3ecaRuslan Bikkinin7 years ago | 124 | const runOptions = CommandPaletteHandler.getRunOptions(project, "android", target); |
| 125 | const platform = new AndroidPlatform(runOptions, { | |
| 126 | packager: project.packager, | |
| 127 | }); | |
4787ec09Artem Egorov8 years ago | 128 | return platform.beforeStartPackager() |
| 129 | .then(() => { | |
| 130 | return platform.startPackager(); | |
| 131 | }) | |
| 132 | .then(() => { | |
| 133 | return platform.runApp(/*shouldLaunchInAllDevices*/true); | |
| 134 | }) | |
2e432a9eArtem Egorov8 years ago | 135 | .then(() => { |
| 136 | return platform.disableJSDebuggingMode(); | |
| 137 | }); | |
4787ec09Artem Egorov8 years ago | 138 | }); |
0a68f8dbArtem Egorov8 years ago | 139 | }); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 140 | } |
| 141 | | |
| 142 | /** | |
| 143 | * Executes the 'react-native run-ios' command | |
| 144 | */ | |
d1fc7f8aArtem Egorov8 years ago | 145 | public static runIos(target: TargetType = "simulator"): Q.Promise<void> { |
2e432a9eArtem Egorov8 years ago | 146 | return this.selectProject() |
| 147 | .then((project: IReactNativeProject) => { | |
0611729dRuslan Bikkinin7 years ago | 148 | TargetPlatformHelper.checkTargetPlatformSupport("ios"); |
4787ec09Artem Egorov8 years ago | 149 | return this.executeCommandInContext("runIos", project.workspaceFolder, () => { |
634e3ecaRuslan Bikkinin7 years ago | 150 | const runOptions = CommandPaletteHandler.getRunOptions(project, "ios", target); |
| 151 | const platform = new IOSPlatform(runOptions, { | |
| 152 | packager: project.packager, | |
| 153 | }); | |
| 154 | | |
4787ec09Artem Egorov8 years ago | 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 | }) | |
2e432a9eArtem Egorov8 years ago | 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 | }); | |
4787ec09Artem Egorov8 years ago | 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(() => { | |
031832ffArtem Egorov8 years ago | 179 | return this.executeCommandInContext("runExponent", project.workspaceFolder, () => { |
634e3ecaRuslan Bikkinin7 years ago | 180 | const runOptions = CommandPaletteHandler.getRunOptions(project, "exponent"); |
| 181 | const platform = new ExponentPlatform(runOptions, { | |
| 182 | packager: project.packager, | |
| 183 | }); | |
4787ec09Artem Egorov8 years ago | 184 | return platform.beforeStartPackager() |
| 185 | .then(() => { | |
| 186 | return platform.startPackager(); | |
| 187 | }) | |
| 188 | .then(() => { | |
| 189 | return platform.runApp(); | |
| 190 | }); | |
| 191 | }); | |
| 192 | }); | |
2e432a9eArtem Egorov8 years ago | 193 | }); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 194 | } |
| 195 | | |
2e432a9eArtem Egorov8 years ago | 196 | public static showDevMenu(): Q.Promise<void> { |
a41f5c68Artem Egorov8 years ago | 197 | return this.selectProject() |
| 198 | .then((project: IReactNativeProject) => { | |
634e3ecaRuslan Bikkinin7 years ago | 199 | AndroidPlatform.showDevMenu() |
a41f5c68Artem Egorov8 years ago | 200 | .catch(() => { }); // Ignore any errors |
634e3ecaRuslan Bikkinin7 years ago | 201 | IOSPlatform.showDevMenu(project.workspaceFolder.uri.fsPath) |
a41f5c68Artem Egorov8 years ago | 202 | .catch(() => { }); // Ignore any errors |
| 203 | return Q.resolve(void 0); | |
| 204 | }); | |
7daed3fcArtem Egorov8 years ago | 205 | } |
| 206 | | |
2e432a9eArtem Egorov8 years ago | 207 | public static reloadApp(): Q.Promise<void> { |
a41f5c68Artem Egorov8 years ago | 208 | return this.selectProject() |
| 209 | .then((project: IReactNativeProject) => { | |
634e3ecaRuslan Bikkinin7 years ago | 210 | AndroidPlatform.reloadApp() |
a41f5c68Artem Egorov8 years ago | 211 | .catch(() => { }); // Ignore any errors |
634e3ecaRuslan Bikkinin7 years ago | 212 | IOSPlatform.reloadApp(project.workspaceFolder.uri.fsPath) |
a41f5c68Artem Egorov8 years ago | 213 | .catch(() => { }); // Ignore any errors |
| 214 | return Q.resolve(void 0); | |
| 215 | }); | |
7daed3fcArtem Egorov8 years ago | 216 | } |
| 217 | | |
031832ffArtem Egorov8 years ago | 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 | | |
2e432a9eArtem Egorov8 years ago | 236 | private static runRestartPackagerCommandAndUpdateStatus(project: IReactNativeProject): Q.Promise<void> { |
4787ec09Artem Egorov8 years ago | 237 | return project.packager.restart(SettingsHelper.getPackagerPort(project.workspaceFolder.uri.fsPath)); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 238 | } |
b3a793eeNisheet Jain10 years ago | 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 | */ | |
2e432a9eArtem Egorov8 years ago | 245 | private static executeCommandInContext(rnCommand: string, workspaceFolder: vscode.WorkspaceFolder, operation: () => Q.Promise<void>): Q.Promise<void> { |
031832ffArtem Egorov8 years ago | 246 | const extProps = { |
| 247 | platform: { | |
| 248 | value: CommandPaletteHandler.getPlatformByCommandName(rnCommand), | |
| 249 | isPii: false, | |
| 250 | }, | |
| 251 | }; | |
| 252 | | |
| 253 | return TelemetryHelper.generate("RNCommand", extProps, (generator) => { | |
8512ccfeMeena Kunnathur Balakrishnan10 years ago | 254 | generator.add("command", rnCommand, false); |
e4dd9aa4Serge Svekolnikov8 years ago | 255 | const projectRoot = SettingsHelper.getReactNativeProjectRoot(workspaceFolder.uri.fsPath); |
634e3ecaRuslan Bikkinin7 years ago | 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 | }); | |
10873e11digeff10 years ago | 269 | }); |
b3a793eeNisheet Jain10 years ago | 270 | } |
7893fb7eJimmy Thomson9 years ago | 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 | */ | |
2e432a9eArtem Egorov8 years ago | 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) | |
7059d307Patricio Beltran9 years ago | 278 | .then(user => { |
2e432a9eArtem Egorov8 years ago | 279 | CommandPaletteHandler.logger.debug(`Publishing as ${user.username}...`); |
4787ec09Artem Egorov8 years ago | 280 | return this.runExponent() |
7059d307Patricio Beltran9 years ago | 281 | .then(() => |
a41f5c68Artem Egorov8 years ago | 282 | XDL.publish(project.workspaceFolder.uri.fsPath)) |
7059d307Patricio Beltran9 years ago | 283 | .then(response => { |
| 284 | if (response.err || !response.url) { | |
| 285 | return false; | |
| 286 | } | |
| 287 | const publishedOutput = `App successfully published to ${response.url}`; | |
2e432a9eArtem Egorov8 years ago | 288 | CommandPaletteHandler.logger.info(publishedOutput); |
7059d307Patricio Beltran9 years ago | 289 | vscode.window.showInformationMessage(publishedOutput); |
| 290 | return true; | |
| 291 | }); | |
| 292 | }); | |
| 293 | } | |
| 294 | | |
2e432a9eArtem Egorov8 years ago | 295 | private static loginToExponent(project: IReactNativeProject): Q.Promise<XDL.IUser> { |
| 296 | return project.exponentHelper.loginToExponent( | |
5c8365a6Artem Egorov8 years ago | 297 | (message, password) => { |
| 298 | return Q.Promise((resolve, reject) => { | |
| 299 | vscode.window.showInputBox({ placeHolder: message, password: password }) | |
2e432a9eArtem Egorov8 years ago | 300 | .then(login => { |
| 301 | resolve(login || ""); | |
| 302 | }, reject); | |
5c8365a6Artem Egorov8 years ago | 303 | }); |
| 304 | }, | |
| 305 | (message) => { | |
| 306 | return Q.Promise((resolve, reject) => { | |
| 307 | vscode.window.showInformationMessage(message) | |
2e432a9eArtem Egorov8 years ago | 308 | .then(password => { |
| 309 | resolve(password || ""); | |
| 310 | }, reject); | |
5c8365a6Artem Egorov8 years ago | 311 | }); |
| 312 | } | |
4787ec09Artem Egorov8 years ago | 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 | }); | |
7893fb7eJimmy Thomson9 years ago | 318 | } |
2e432a9eArtem Egorov8 years ago | 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 { | |
634e3ecaRuslan Bikkinin7 years ago | 334 | return Q.reject(new Error("Current workspace is not a React Native project.")); |
2e432a9eArtem Egorov8 years ago | 335 | } |
| 336 | } | |
d1fc7f8aArtem Egorov8 years ago | 337 | |
4787ec09Artem Egorov8 years ago | 338 | private static getRunOptions(project: IReactNativeProject, platform: "ios" | "android" | "exponent", target: TargetType = "simulator"): IAndroidRunOptions | IIOSRunOptions { |
d1fc7f8aArtem Egorov8 years ago | 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 | } | |
0a283547Anna Kocheshkova8 years ago | 356 | } |