microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commandPaletteHandler.ts
420lines · 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"; |
0a68f8dbArtem Egorov8 years ago | 5 | import * as XDL from "./exponent/xdlInterface"; |
df4bce40digeff10 years ago | 6 | import {SettingsHelper} from "./settingsHelper"; |
0a68f8dbArtem Egorov8 years ago | 7 | import {OutputChannelLogger} from "./log/OutputChannelLogger"; |
db6fd42aRuslan Bikkinin7 years ago | 8 | import {TargetType, GeneralMobilePlatform} from "./generalMobilePlatform"; |
0a68f8dbArtem Egorov8 years ago | 9 | import {AndroidPlatform} from "./android/androidPlatform"; |
| 10 | import {IOSPlatform} from "./ios/iOSPlatform"; | |
6e1bcd36RedMickey6 years ago | 11 | import {ProjectVersionHelper} from "../common/projectVersionHelper"; |
e3706a1cRedMickey6 years ago | 12 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
aeaf46bcMeena Kunnathur Balakrishnan10 years ago | 13 | import {TargetPlatformHelper} from "../common/targetPlatformHelper"; |
d976d077Meena Kunnathur Balakrishnan10 years ago | 14 | import {TelemetryHelper} from "../common/telemetryHelper"; |
6e1bcd36RedMickey6 years ago | 15 | import {ProjectsStorage} from "./projectsStorage"; |
259c018fYuri Skorokhodov5 years ago | 16 | import {IAndroidRunOptions, IIOSRunOptions, PlatformType} from "./launchArgs"; |
d8b69cd3Yuri Skorokhodov7 years ago | 17 | import {ExponentPlatform} from "./exponent/exponentPlatform"; |
6277aa08Yuri Skorokhodov7 years ago | 18 | import {spawn, ChildProcess} from "child_process"; |
| 19 | import {HostPlatform} from "../common/hostPlatform"; | |
af1474acRedMickey6 years ago | 20 | import {CommandExecutor} from "../common/commandExecutor"; |
d7d405aeYuri Skorokhodov7 years ago | 21 | import * as nls from "vscode-nls"; |
af1474acRedMickey6 years ago | 22 | import {ErrorHelper} from "../common/error/errorHelper"; |
| 23 | import {InternalErrorCode} from "../common/error/internalErrorCode"; | |
6e1bcd36RedMickey6 years ago | 24 | import {AppLauncher} from "./appLauncher"; |
68a5b8d5JiglioNero5 years ago | 25 | import { AndroidEmulatorManager } from "./android/androidEmulatorManager"; |
| 26 | import { AdbHelper } from "./android/adb"; | |
2d8af448Yuri Skorokhodov6 years ago | 27 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
d7d405aeYuri Skorokhodov7 years ago | 28 | const localize = nls.loadMessageBundle(); |
4b37483dmax-mironov8 years ago | 29 | |
e1c05e69dlebu10 years ago | 30 | export class CommandPaletteHandler { |
a740b79aYuri Skorokhodov7 years ago | 31 | public static elementInspector: ChildProcess | null; |
2e432a9eArtem Egorov8 years ago | 32 | private static logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
6a465861max-mironov8 years ago | 33 | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 34 | /** |
| 35 | * Starts the React Native packager | |
| 36 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 37 | public static startPackager(): Promise<void> { |
2e432a9eArtem Egorov8 years ago | 38 | return this.selectProject() |
6e1bcd36RedMickey6 years ago | 39 | .then((appLauncher: AppLauncher) => { |
| 40 | return ProjectVersionHelper.getReactNativePackageVersionsFromNodeModules(appLauncher.getPackager().getProjectPath()) | |
7fa90b3bRedMickey6 years ago | 41 | .then(versions => { |
6e1bcd36RedMickey6 years ago | 42 | return this.executeCommandInContext("startPackager", appLauncher.getWorkspaceFolder(), () => { |
| 43 | return appLauncher.getPackager().isRunning() | |
af1474acRedMickey6 years ago | 44 | .then((running) => { |
ce5e88eeYuri Skorokhodov5 years ago | 45 | return running ? appLauncher.getPackager().stop() : Promise.resolve(); |
af1474acRedMickey6 years ago | 46 | }); |
| 47 | }) | |
6e1bcd36RedMickey6 years ago | 48 | .then(() => appLauncher.getPackager().start()); |
af1474acRedMickey6 years ago | 49 | }); |
2e432a9eArtem Egorov8 years ago | 50 | }); |
bef522ffMeena Kunnathur Balakrishnan10 years ago | 51 | } |
| 52 | | |
0904a0d7Meena Kunnathur Balakrishnan10 years ago | 53 | /** |
| 54 | * Kills the React Native packager invoked by the extension's packager | |
| 55 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 56 | public static stopPackager(): Promise<void> { |
2e432a9eArtem Egorov8 years ago | 57 | return this.selectProject() |
6e1bcd36RedMickey6 years ago | 58 | .then((appLauncher: AppLauncher) => { |
| 59 | return this.executeCommandInContext("stopPackager", appLauncher.getWorkspaceFolder(), () => appLauncher.getPackager().stop()); | |
2e432a9eArtem Egorov8 years ago | 60 | }); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 61 | } |
| 62 | | |
ce5e88eeYuri Skorokhodov5 years ago | 63 | public static stopAllPackagers(): Promise<void> { |
6e1bcd36RedMickey6 years ago | 64 | let keys = Object.keys(ProjectsStorage.projectsCache); |
ce5e88eeYuri Skorokhodov5 years ago | 65 | let promises: Promise<void>[] = []; |
4edcda70Artem Egorov8 years ago | 66 | keys.forEach((key) => { |
6e1bcd36RedMickey6 years ago | 67 | let appLauncher = ProjectsStorage.projectsCache[key]; |
| 68 | promises.push(this.executeCommandInContext("stopPackager", appLauncher.getWorkspaceFolder(), () => appLauncher.getPackager().stop())); | |
4edcda70Artem Egorov8 years ago | 69 | }); |
| 70 | | |
ce5e88eeYuri Skorokhodov5 years ago | 71 | return Promise.all(promises).then(() => {}); |
4edcda70Artem Egorov8 years ago | 72 | } |
| 73 | | |
f2a58eefBret Johnson9 years ago | 74 | /** |
| 75 | * Restarts the React Native packager | |
| 76 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 77 | public static restartPackager(): Promise<void> { |
2e432a9eArtem Egorov8 years ago | 78 | return this.selectProject() |
6e1bcd36RedMickey6 years ago | 79 | .then((appLauncher: AppLauncher) => { |
| 80 | return ProjectVersionHelper.getReactNativePackageVersionsFromNodeModules(appLauncher.getPackager().getProjectPath()) | |
7fa90b3bRedMickey6 years ago | 81 | .then(versions => { |
6e1bcd36RedMickey6 years ago | 82 | return this.executeCommandInContext("restartPackager", appLauncher.getWorkspaceFolder(), () => |
| 83 | this.runRestartPackagerCommandAndUpdateStatus(appLauncher)); | |
af1474acRedMickey6 years ago | 84 | }); |
2e432a9eArtem Egorov8 years ago | 85 | }); |
f2a58eefBret Johnson9 years ago | 86 | } |
| 87 | | |
7893fb7eJimmy Thomson9 years ago | 88 | /** |
| 89 | * Execute command to publish to exponent host. | |
| 90 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 91 | public static publishToExpHost(): Promise<void> { |
2e432a9eArtem Egorov8 years ago | 92 | return this.selectProject() |
6e1bcd36RedMickey6 years ago | 93 | .then((appLauncher: AppLauncher) => { |
| 94 | return this.executeCommandInContext("publishToExpHost", appLauncher.getWorkspaceFolder(), () => { | |
| 95 | return this.executePublishToExpHost(appLauncher).then((didPublish) => { | |
2e432a9eArtem Egorov8 years ago | 96 | if (!didPublish) { |
d7d405aeYuri Skorokhodov7 years ago | 97 | CommandPaletteHandler.logger.warning(localize("ExponentPublishingWasUnsuccessfulMakeSureYoureLoggedInToExpo", "Publishing was unsuccessful. Please make sure you are logged in Expo and your project is a valid Expo project")); |
2e432a9eArtem Egorov8 years ago | 98 | } |
| 99 | }); | |
| 100 | }); | |
7893fb7eJimmy Thomson9 years ago | 101 | }); |
| 102 | } | |
| 103 | | |
68a5b8d5JiglioNero5 years ago | 104 | public static async launchAndroidEmulator(): Promise<void> { |
| 105 | const appLauncher = await this.selectProject(); | |
| 106 | const adbHelper = new AdbHelper(appLauncher.getPackager().getProjectPath()); | |
| 107 | const androidEmulatorManager = new AndroidEmulatorManager(adbHelper); | |
| 108 | const emulator = await androidEmulatorManager.startSelection(); | |
| 109 | if (emulator) { | |
| 110 | androidEmulatorManager.tryLaunchEmulatorByName(emulator); | |
| 111 | } | |
| 112 | } | |
| 113 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 114 | /** |
| 115 | * Executes the 'react-native run-android' command | |
| 116 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 117 | public static runAndroid(target: TargetType = "simulator"): Promise<void> { |
2e432a9eArtem Egorov8 years ago | 118 | return this.selectProject() |
6e1bcd36RedMickey6 years ago | 119 | .then((appLauncher: AppLauncher) => { |
259c018fYuri Skorokhodov5 years ago | 120 | TargetPlatformHelper.checkTargetPlatformSupport(PlatformType.Android); |
6e1bcd36RedMickey6 years ago | 121 | return ProjectVersionHelper.getReactNativePackageVersionsFromNodeModules(appLauncher.getPackager().getProjectPath()) |
7fa90b3bRedMickey6 years ago | 122 | .then(versions => { |
6e1bcd36RedMickey6 years ago | 123 | appLauncher.setReactNativeVersions(versions); |
| 124 | return this.executeCommandInContext("runAndroid", appLauncher.getWorkspaceFolder(), () => { | |
259c018fYuri Skorokhodov5 years ago | 125 | const platform = <AndroidPlatform>this.createPlatform(appLauncher, PlatformType.Android, AndroidPlatform, target); |
68a5b8d5JiglioNero5 years ago | 126 | return platform.resolveVirtualDevice(target) |
259c018fYuri Skorokhodov5 years ago | 127 | .then(() => platform.beforeStartPackager()) |
| 128 | .then(() => { | |
| 129 | return platform.startPackager(); | |
| 130 | }) | |
| 131 | .then(() => { | |
| 132 | return platform.runApp(/*shouldLaunchInAllDevices*/true); | |
| 133 | }) | |
| 134 | .then(() => { | |
| 135 | return platform.disableJSDebuggingMode(); | |
| 136 | }); | |
2e432a9eArtem Egorov8 years ago | 137 | }); |
af1474acRedMickey6 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 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 145 | public static runIos(target: TargetType = "simulator"): Promise<void> { |
2e432a9eArtem Egorov8 years ago | 146 | return this.selectProject() |
6e1bcd36RedMickey6 years ago | 147 | .then((appLauncher: AppLauncher) => { |
| 148 | return ProjectVersionHelper.getReactNativePackageVersionsFromNodeModules(appLauncher.getPackager().getProjectPath()) | |
7fa90b3bRedMickey6 years ago | 149 | .then(versions => { |
6e1bcd36RedMickey6 years ago | 150 | appLauncher.setReactNativeVersions(versions); |
259c018fYuri Skorokhodov5 years ago | 151 | TargetPlatformHelper.checkTargetPlatformSupport(PlatformType.iOS); |
6e1bcd36RedMickey6 years ago | 152 | return this.executeCommandInContext("runIos", appLauncher.getWorkspaceFolder(), () => { |
259c018fYuri Skorokhodov5 years ago | 153 | const platform = <IOSPlatform>this.createPlatform(appLauncher, PlatformType.iOS, IOSPlatform, target); |
68a5b8d5JiglioNero5 years ago | 154 | return platform.resolveVirtualDevice(target) |
| 155 | .then(() => platform.beforeStartPackager()) | |
ba953e9fRedMickey6 years ago | 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 | }); | |
af1474acRedMickey6 years ago | 168 | }); |
4787ec09Artem Egorov8 years ago | 169 | }); |
| 170 | } | |
| 171 | | |
| 172 | /** | |
| 173 | * Starts the Exponent packager | |
| 174 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 175 | public static runExponent(): Promise<void> { |
4787ec09Artem Egorov8 years ago | 176 | return this.selectProject() |
6e1bcd36RedMickey6 years ago | 177 | .then((appLauncher: AppLauncher) => { |
| 178 | return ProjectVersionHelper.getReactNativePackageVersionsFromNodeModules(appLauncher.getPackager().getProjectPath()) | |
7fa90b3bRedMickey6 years ago | 179 | .then(versions => { |
6e1bcd36RedMickey6 years ago | 180 | return this.loginToExponent(appLauncher) |
af1474acRedMickey6 years ago | 181 | .then(() => { |
6e1bcd36RedMickey6 years ago | 182 | return this.executeCommandInContext("runExponent", appLauncher.getWorkspaceFolder(), () => { |
| 183 | appLauncher.setReactNativeVersions(versions); | |
259c018fYuri Skorokhodov5 years ago | 184 | const platform = <ExponentPlatform>this.createPlatform(appLauncher, PlatformType.Exponent, ExponentPlatform); |
af1474acRedMickey6 years ago | 185 | return platform.beforeStartPackager() |
| 186 | .then(() => { | |
| 187 | return platform.startPackager(); | |
| 188 | }) | |
| 189 | .then(() => { | |
| 190 | return platform.runApp(); | |
| 191 | }); | |
4787ec09Artem Egorov8 years ago | 192 | }); |
af1474acRedMickey6 years ago | 193 | }); |
4787ec09Artem Egorov8 years ago | 194 | }); |
2e432a9eArtem Egorov8 years ago | 195 | }); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 196 | } |
| 197 | | |
ce5e88eeYuri Skorokhodov5 years ago | 198 | public static showDevMenu(): Promise<void> { |
a41f5c68Artem Egorov8 years ago | 199 | return this.selectProject() |
6e1bcd36RedMickey6 years ago | 200 | .then((appLauncher: AppLauncher) => { |
259c018fYuri Skorokhodov5 years ago | 201 | const androidPlatform = <AndroidPlatform>this.createPlatform(appLauncher, PlatformType.Android, AndroidPlatform); |
db6fd42aRuslan Bikkinin7 years ago | 202 | androidPlatform.showDevMenu() |
a41f5c68Artem Egorov8 years ago | 203 | .catch(() => { }); // Ignore any errors |
2ecfbd20Yuri Skorokhodov7 years ago | 204 | |
| 205 | if (process.platform === "darwin") { | |
259c018fYuri Skorokhodov5 years ago | 206 | const iosPlatform = <IOSPlatform>this.createPlatform(appLauncher, PlatformType.iOS, IOSPlatform); |
7e74daf7Yuri Skorokhodov6 years ago | 207 | iosPlatform.showDevMenu(appLauncher) |
2ecfbd20Yuri Skorokhodov7 years ago | 208 | .catch(() => { }); // Ignore any errors |
| 209 | } | |
ce5e88eeYuri Skorokhodov5 years ago | 210 | return Promise.resolve(); |
a41f5c68Artem Egorov8 years ago | 211 | }); |
7daed3fcArtem Egorov8 years ago | 212 | } |
| 213 | | |
ce5e88eeYuri Skorokhodov5 years ago | 214 | public static reloadApp(): Promise<void> { |
a41f5c68Artem Egorov8 years ago | 215 | return this.selectProject() |
6e1bcd36RedMickey6 years ago | 216 | .then((appLauncher: AppLauncher) => { |
259c018fYuri Skorokhodov5 years ago | 217 | const androidPlatform = <AndroidPlatform>this.createPlatform(appLauncher, PlatformType.Android, AndroidPlatform); |
db6fd42aRuslan Bikkinin7 years ago | 218 | androidPlatform.reloadApp() |
a41f5c68Artem Egorov8 years ago | 219 | .catch(() => { }); // Ignore any errors |
2ecfbd20Yuri Skorokhodov7 years ago | 220 | |
| 221 | if (process.platform === "darwin") { | |
259c018fYuri Skorokhodov5 years ago | 222 | const iosPlatform = <IOSPlatform>this.createPlatform(appLauncher, PlatformType.iOS, IOSPlatform); |
7e74daf7Yuri Skorokhodov6 years ago | 223 | iosPlatform.reloadApp(appLauncher) |
2ecfbd20Yuri Skorokhodov7 years ago | 224 | .catch(() => { }); // Ignore any errors |
| 225 | } | |
ce5e88eeYuri Skorokhodov5 years ago | 226 | return Promise.resolve(); |
a41f5c68Artem Egorov8 years ago | 227 | }); |
7daed3fcArtem Egorov8 years ago | 228 | } |
| 229 | | |
ce5e88eeYuri Skorokhodov5 years ago | 230 | public static runElementInspector(): Promise<void> { |
14fc3172Artem Egorov7 years ago | 231 | if (!CommandPaletteHandler.elementInspector) { |
6277aa08Yuri Skorokhodov7 years ago | 232 | // Remove the following env variables to prevent running electron app in node mode. |
af0c5c30Yuri Skorokhodov5 years ago | 233 | // https://github.com/microsoft/vscode/issues/3011#issuecomment-184577502 |
6277aa08Yuri Skorokhodov7 years ago | 234 | let env = Object.assign({}, process.env); |
| 235 | delete env.ATOM_SHELL_INTERNAL_RUN_AS_NODE; | |
| 236 | delete env.ELECTRON_RUN_AS_NODE; | |
| 237 | let command = HostPlatform.getNpmCliCommand("react-devtools"); | |
| 238 | CommandPaletteHandler.elementInspector = spawn(command, [], { | |
| 239 | env, | |
| 240 | }); | |
| 241 | if (!CommandPaletteHandler.elementInspector.pid) { | |
| 242 | CommandPaletteHandler.elementInspector = null; | |
ce5e88eeYuri Skorokhodov5 years ago | 243 | return Promise.reject(ErrorHelper.getInternalError(InternalErrorCode.ReactDevtoolsIsNotInstalled)); |
fd2c4686Yuri Skorokhodov7 years ago | 244 | } |
6277aa08Yuri Skorokhodov7 years ago | 245 | CommandPaletteHandler.elementInspector.stdout.on("data", (data: string) => { |
| 246 | this.logger.info(data); | |
| 247 | }); | |
| 248 | CommandPaletteHandler.elementInspector.stderr.on("data", (data: string) => { | |
| 249 | this.logger.error(data); | |
| 250 | }); | |
| 251 | CommandPaletteHandler.elementInspector.once("exit", () => { | |
| 252 | CommandPaletteHandler.elementInspector = null; | |
| 253 | }); | |
62e2a78aYuri Skorokhodov7 years ago | 254 | } else { |
d7d405aeYuri Skorokhodov7 years ago | 255 | this.logger.info(localize("AnotherElementInspectorAlreadyRun", "Another element inspector already run")); |
62e2a78aYuri Skorokhodov7 years ago | 256 | } |
ce5e88eeYuri Skorokhodov5 years ago | 257 | return Promise.resolve(); |
d8b69cd3Yuri Skorokhodov7 years ago | 258 | } |
14fc3172Artem Egorov7 years ago | 259 | |
| 260 | public static stopElementInspector(): void { | |
| 261 | return CommandPaletteHandler.elementInspector ? CommandPaletteHandler.elementInspector.kill() : void 0; | |
| 262 | } | |
| 263 | | |
031832ffArtem Egorov8 years ago | 264 | public static getPlatformByCommandName(commandName: string): string { |
| 265 | commandName = commandName.toLocaleLowerCase(); | |
| 266 | | |
259c018fYuri Skorokhodov5 years ago | 267 | if (commandName.indexOf(PlatformType.Android) > -1) { |
| 268 | return PlatformType.Android; | |
031832ffArtem Egorov8 years ago | 269 | } |
| 270 | | |
259c018fYuri Skorokhodov5 years ago | 271 | if (commandName.indexOf(PlatformType.iOS) > -1) { |
| 272 | return PlatformType.iOS; | |
031832ffArtem Egorov8 years ago | 273 | } |
| 274 | | |
259c018fYuri Skorokhodov5 years ago | 275 | if (commandName.indexOf(PlatformType.Exponent) > -1) { |
| 276 | return PlatformType.Exponent; | |
031832ffArtem Egorov8 years ago | 277 | } |
| 278 | | |
| 279 | return ""; | |
| 280 | } | |
| 281 | | |
259c018fYuri Skorokhodov5 years ago | 282 | private static createPlatform(appLauncher: AppLauncher, platform: PlatformType.iOS | PlatformType.Android | PlatformType.Exponent, platformClass: typeof GeneralMobilePlatform, target?: TargetType): GeneralMobilePlatform { |
6e1bcd36RedMickey6 years ago | 283 | const runOptions = CommandPaletteHandler.getRunOptions(appLauncher, platform, target); |
db6fd42aRuslan Bikkinin7 years ago | 284 | return new platformClass(runOptions, { |
6e1bcd36RedMickey6 years ago | 285 | packager: appLauncher.getPackager(), |
db6fd42aRuslan Bikkinin7 years ago | 286 | }); |
| 287 | } | |
| 288 | | |
ce5e88eeYuri Skorokhodov5 years ago | 289 | private static runRestartPackagerCommandAndUpdateStatus(appLauncher: AppLauncher): Promise<void> { |
6e1bcd36RedMickey6 years ago | 290 | return appLauncher.getPackager().restart(SettingsHelper.getPackagerPort(appLauncher.getWorkspaceFolderUri().fsPath)); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 291 | } |
b3a793eeNisheet Jain10 years ago | 292 | |
| 293 | /** | |
| 294 | * Ensures that we are in a React Native project and then executes the operation | |
| 295 | * Otherwise, displays an error message banner | |
| 296 | * {operation} - a function that performs the expected operation | |
| 297 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 298 | private static executeCommandInContext(rnCommand: string, workspaceFolder: vscode.WorkspaceFolder, operation: () => Promise<void>): Promise<void> { |
031832ffArtem Egorov8 years ago | 299 | const extProps = { |
| 300 | platform: { | |
| 301 | value: CommandPaletteHandler.getPlatformByCommandName(rnCommand), | |
| 302 | isPii: false, | |
| 303 | }, | |
| 304 | }; | |
| 305 | | |
| 306 | return TelemetryHelper.generate("RNCommand", extProps, (generator) => { | |
8512ccfeMeena Kunnathur Balakrishnan10 years ago | 307 | generator.add("command", rnCommand, false); |
e4dd9aa4Serge Svekolnikov8 years ago | 308 | const projectRoot = SettingsHelper.getReactNativeProjectRoot(workspaceFolder.uri.fsPath); |
db6fd42aRuslan Bikkinin7 years ago | 309 | this.logger.debug(`Command palette: run project ${projectRoot} in context`); |
| 310 | return ReactNativeProjectHelper.isReactNativeProject(projectRoot) | |
| 311 | .then(isRNProject => { | |
| 312 | generator.add("isRNProject", isRNProject, false); | |
| 313 | if (isRNProject) { | |
| 314 | // Bring the log channel to focus | |
| 315 | this.logger.setFocusOnLogChannel(); | |
| 316 | | |
| 317 | // Execute the operation | |
| 318 | return operation(); | |
| 319 | } else { | |
| 320 | vscode.window.showErrorMessage(`${projectRoot} workspace is not a React Native project.`); | |
| 321 | return; | |
| 322 | } | |
| 323 | }); | |
10873e11digeff10 years ago | 324 | }); |
b3a793eeNisheet Jain10 years ago | 325 | } |
7893fb7eJimmy Thomson9 years ago | 326 | |
| 327 | /** | |
| 328 | * 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. | |
| 329 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 330 | private static executePublishToExpHost(appLauncher: AppLauncher): Promise<boolean> { |
d7d405aeYuri Skorokhodov7 years ago | 331 | CommandPaletteHandler.logger.info(localize("PublishingAppToExponentServer", "Publishing app to Expo server. This might take a moment.")); |
6e1bcd36RedMickey6 years ago | 332 | return this.loginToExponent(appLauncher) |
7059d307Patricio Beltran9 years ago | 333 | .then(user => { |
2e432a9eArtem Egorov8 years ago | 334 | CommandPaletteHandler.logger.debug(`Publishing as ${user.username}...`); |
4787ec09Artem Egorov8 years ago | 335 | return this.runExponent() |
7059d307Patricio Beltran9 years ago | 336 | .then(() => |
6e1bcd36RedMickey6 years ago | 337 | XDL.publish(appLauncher.getWorkspaceFolderUri().fsPath)) |
7059d307Patricio Beltran9 years ago | 338 | .then(response => { |
| 339 | if (response.err || !response.url) { | |
| 340 | return false; | |
| 341 | } | |
d7d405aeYuri Skorokhodov7 years ago | 342 | const publishedOutput = localize("ExpoAppSuccessfullyPublishedTo", "Expo app successfully published to {0}", response.url); |
2e432a9eArtem Egorov8 years ago | 343 | CommandPaletteHandler.logger.info(publishedOutput); |
7059d307Patricio Beltran9 years ago | 344 | vscode.window.showInformationMessage(publishedOutput); |
| 345 | return true; | |
| 346 | }); | |
| 347 | }); | |
| 348 | } | |
| 349 | | |
ce5e88eeYuri Skorokhodov5 years ago | 350 | private static loginToExponent(appLauncher: AppLauncher): Promise<XDL.IUser> { |
6e1bcd36RedMickey6 years ago | 351 | return appLauncher.getExponentHelper().loginToExponent( |
5c8365a6Artem Egorov8 years ago | 352 | (message, password) => { |
ce5e88eeYuri Skorokhodov5 years ago | 353 | return new Promise((resolve, reject) => { |
5c8365a6Artem Egorov8 years ago | 354 | vscode.window.showInputBox({ placeHolder: message, password: password }) |
2e432a9eArtem Egorov8 years ago | 355 | .then(login => { |
| 356 | resolve(login || ""); | |
| 357 | }, reject); | |
5c8365a6Artem Egorov8 years ago | 358 | }); |
| 359 | }, | |
| 360 | (message) => { | |
ce5e88eeYuri Skorokhodov5 years ago | 361 | return new Promise((resolve, reject) => { |
5c8365a6Artem Egorov8 years ago | 362 | vscode.window.showInformationMessage(message) |
2e432a9eArtem Egorov8 years ago | 363 | .then(password => { |
| 364 | resolve(password || ""); | |
| 365 | }, reject); | |
5c8365a6Artem Egorov8 years ago | 366 | }); |
| 367 | } | |
4787ec09Artem Egorov8 years ago | 368 | ) |
| 369 | .catch((err) => { | |
d7d405aeYuri Skorokhodov7 years ago | 370 | CommandPaletteHandler.logger.warning(localize("ExpoErrorOccuredMakeSureYouAreLoggedIn", "An error has occured. Please make sure you are logged in to Expo, your project is setup correctly for publishing and your packager is running as Expo.")); |
4787ec09Artem Egorov8 years ago | 371 | throw err; |
| 372 | }); | |
7893fb7eJimmy Thomson9 years ago | 373 | } |
2e432a9eArtem Egorov8 years ago | 374 | |
ce5e88eeYuri Skorokhodov5 years ago | 375 | private static selectProject(): Promise<AppLauncher> { |
6e1bcd36RedMickey6 years ago | 376 | let keys = Object.keys(ProjectsStorage.projectsCache); |
2e432a9eArtem Egorov8 years ago | 377 | if (keys.length > 1) { |
ce5e88eeYuri Skorokhodov5 years ago | 378 | return new Promise((resolve, reject) => { |
2e432a9eArtem Egorov8 years ago | 379 | vscode.window.showQuickPick(keys) |
| 380 | .then((selected) => { | |
| 381 | if (selected) { | |
db6fd42aRuslan Bikkinin7 years ago | 382 | this.logger.debug(`Command palette: selected project ${selected}`); |
6e1bcd36RedMickey6 years ago | 383 | resolve(ProjectsStorage.projectsCache[selected]); |
2e432a9eArtem Egorov8 years ago | 384 | } |
| 385 | }, reject); | |
| 386 | }); | |
| 387 | } else if (keys.length === 1) { | |
db6fd42aRuslan Bikkinin7 years ago | 388 | this.logger.debug(`Command palette: once project ${keys[0]}`); |
ce5e88eeYuri Skorokhodov5 years ago | 389 | return Promise.resolve(ProjectsStorage.projectsCache[keys[0]]); |
2e432a9eArtem Egorov8 years ago | 390 | } else { |
ce5e88eeYuri Skorokhodov5 years ago | 391 | return Promise.reject(ErrorHelper.getInternalError(InternalErrorCode.WorkspaceNotFound, "Current workspace does not contain React Native projects.")); |
2e432a9eArtem Egorov8 years ago | 392 | } |
| 393 | } | |
d1fc7f8aArtem Egorov8 years ago | 394 | |
259c018fYuri Skorokhodov5 years ago | 395 | private static getRunOptions(appLauncher: AppLauncher, platform: PlatformType.iOS | PlatformType.Android | PlatformType.Exponent, target: TargetType = "simulator"): IAndroidRunOptions | IIOSRunOptions { |
6e1bcd36RedMickey6 years ago | 396 | const packagerPort = SettingsHelper.getPackagerPort(appLauncher.getWorkspaceFolderUri().fsPath); |
| 397 | const runArgs = SettingsHelper.getRunArgs(platform, target, appLauncher.getWorkspaceFolderUri()); | |
| 398 | const envArgs = SettingsHelper.getEnvArgs(platform, target, appLauncher.getWorkspaceFolderUri()); | |
| 399 | const envFile = SettingsHelper.getEnvFile(platform, target, appLauncher.getWorkspaceFolderUri()); | |
| 400 | const projectRoot = SettingsHelper.getReactNativeProjectRoot(appLauncher.getWorkspaceFolderUri().fsPath); | |
d1fc7f8aArtem Egorov8 years ago | 401 | const runOptions: IAndroidRunOptions | IIOSRunOptions = { |
| 402 | platform: platform, | |
6e1bcd36RedMickey6 years ago | 403 | workspaceRoot: appLauncher.getWorkspaceFolderUri().fsPath, |
d1fc7f8aArtem Egorov8 years ago | 404 | projectRoot: projectRoot, |
| 405 | packagerPort: packagerPort, | |
| 406 | runArguments: runArgs, | |
| 407 | env: envArgs, | |
| 408 | envFile: envFile, | |
6e1bcd36RedMickey6 years ago | 409 | reactNativeVersions: appLauncher.getReactNativeVersions() || {reactNativeVersion: "", reactNativeWindowsVersion: ""}, |
d1fc7f8aArtem Egorov8 years ago | 410 | }; |
| 411 | | |
4dfc9ffdRedMickey5 years ago | 412 | if (platform === PlatformType.iOS && target === "device") { |
| 413 | runOptions.target = "device"; | |
| 414 | } | |
| 415 | | |
6e1bcd36RedMickey6 years ago | 416 | CommandExecutor.ReactNativeCommand = SettingsHelper.getReactNativeGlobalCommandName(appLauncher.getWorkspaceFolderUri()); |
af1474acRedMickey6 years ago | 417 | |
d1fc7f8aArtem Egorov8 years ago | 418 | return runOptions; |
| 419 | } | |
d7d405aeYuri Skorokhodov7 years ago | 420 | } |