microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commands/util/index.ts
128lines · 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 nls from "vscode-nls"; |
| 6 | import WebSocket = require("ws"); |
| 7 | import { AppLauncher } from "../../appLauncher"; |
| 8 | import { |
| 9 | IAndroidRunOptions, |
| 10 | IIOSRunOptions, |
| 11 | ImacOSRunOptions, |
| 12 | IWindowsRunOptions, |
| 13 | PlatformType, |
| 14 | } from "../../launchArgs"; |
| 15 | import { OutputChannelLogger } from "../../log/OutputChannelLogger"; |
| 16 | import { SettingsHelper } from "../../settingsHelper"; |
| 17 | import { TargetType } from "../../generalPlatform"; |
| 18 | import { CommandExecutor } from "../../../common/commandExecutor"; |
| 19 | import { ProjectsStorage } from "../../projectsStorage"; |
| 20 | import { ErrorHelper } from "../../../common/error/errorHelper"; |
| 21 | import { InternalErrorCode } from "../../../common/error/internalErrorCode"; |
| 22 | |
| 23 | nls.config({ |
| 24 | messageFormat: nls.MessageFormat.bundle, |
| 25 | bundleFormat: nls.BundleFormat.standalone, |
| 26 | })(); |
| 27 | const localize = nls.loadMessageBundle(); |
| 28 | |
| 29 | type IPlatformRunOptions = |
| 30 | | IAndroidRunOptions |
| 31 | | IIOSRunOptions |
| 32 | | IWindowsRunOptions |
| 33 | | ImacOSRunOptions; |
| 34 | |
| 35 | export function getRunOptions( |
| 36 | project: AppLauncher, |
| 37 | platform: PlatformType, |
| 38 | target: TargetType = TargetType.Simulator, |
| 39 | ): IPlatformRunOptions { |
| 40 | const folderUri = project.getWorkspaceFolderUri(); |
| 41 | |
| 42 | const runOptions: IPlatformRunOptions = { |
| 43 | platform, |
| 44 | packagerPort: SettingsHelper.getPackagerPort(folderUri.fsPath), |
| 45 | runArguments: SettingsHelper.getRunArgs(platform, target, folderUri), |
| 46 | env: SettingsHelper.getEnvArgs(platform, target, folderUri), |
| 47 | envFile: SettingsHelper.getEnvFile(platform, target, folderUri), |
| 48 | projectRoot: SettingsHelper.getReactNativeProjectRoot(folderUri.fsPath), |
| 49 | nodeModulesRoot: project.getOrUpdateNodeModulesRoot(), |
| 50 | reactNativeVersions: project.getReactNativeVersions() || { |
| 51 | reactNativeVersion: "", |
| 52 | reactNativeWindowsVersion: "", |
| 53 | reactNativeMacOSVersion: "", |
| 54 | }, |
| 55 | workspaceRoot: project.getWorkspaceFolderUri().fsPath, |
| 56 | ...(platform === PlatformType.iOS && target === "device" && { target: "device" }), |
| 57 | }; |
| 58 | |
| 59 | CommandExecutor.ReactNativeCommand = SettingsHelper.getReactNativeGlobalCommandName( |
| 60 | project.getWorkspaceFolderUri(), |
| 61 | ); |
| 62 | |
| 63 | return runOptions; |
| 64 | } |
| 65 | |
| 66 | export async function loginToExponent(project: AppLauncher): Promise<xdl.IUser> { |
| 67 | try { |
| 68 | return await project.getExponentHelper().loginToExponent( |
| 69 | (message, password) => |
| 70 | new Promise<string | undefined>((res, rej) => { |
| 71 | vscode.window.showInputBox({ placeHolder: message, password }).then(res, rej); |
| 72 | }).then(it => it || ""), |
| 73 | message => |
| 74 | new Promise<string | undefined>((res, rej) => { |
| 75 | vscode.window.showInformationMessage(message).then(res, rej); |
| 76 | }).then(it => it || ""), |
| 77 | ); |
| 78 | } catch (err) { |
| 79 | OutputChannelLogger.getMainChannel().warning( |
| 80 | localize( |
| 81 | "ExpoErrorOccuredMakeSureYouAreLoggedIn", |
| 82 | "An error has occurred. Please make sure you are logged in to Expo, your project is setup correctly for publishing and your packager is running as Expo.", |
| 83 | ), |
| 84 | ); |
| 85 | throw err; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | export async function selectProject(): Promise<AppLauncher> { |
| 90 | const logger = OutputChannelLogger.getMainChannel(); |
| 91 | const projectKeys = Object.keys(ProjectsStorage.projectsCache); |
| 92 | |
| 93 | if (projectKeys.length === 0) { |
| 94 | throw ErrorHelper.getInternalError( |
| 95 | InternalErrorCode.WorkspaceNotFound, |
| 96 | "Current workspace does not contain React Native projects.", |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | if (projectKeys.length === 1) { |
| 101 | logger.debug(`Command palette: once project ${projectKeys[0]}`); |
| 102 | return ProjectsStorage.projectsCache[projectKeys[0]]; |
| 103 | } |
| 104 | |
| 105 | const selected = await vscode.window.showQuickPick(projectKeys); |
| 106 | |
| 107 | if (!selected) { |
| 108 | throw ErrorHelper.getInternalError(InternalErrorCode.UserInputCanceled); |
| 109 | } |
| 110 | |
| 111 | logger.debug(`Command palette: selected project ${selected}`); |
| 112 | return ProjectsStorage.projectsCache[selected]; |
| 113 | } |
| 114 | |
| 115 | export async function sendMessageToMetro(method: "devMenu" | "reload", project: AppLauncher) { |
| 116 | const port = SettingsHelper.getPackagerPort(project.getWorkspaceFolderUri().fsPath); |
| 117 | const ws = new WebSocket(`ws://localhost:${port}/message`); |
| 118 | await new Promise(resolve => { |
| 119 | ws.addEventListener("open", resolve); |
| 120 | }); |
| 121 | ws.send( |
| 122 | JSON.stringify({ |
| 123 | version: 2, |
| 124 | method, |
| 125 | }), |
| 126 | ); |
| 127 | ws.close(); |
| 128 | } |
| 129 | |