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