microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/configurationProviderHelper.ts
247lines · 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 nls from "vscode-nls"; |
| 5 | import { |
| 6 | MultiStepInput, |
| 7 | IQuickPickParameters, |
| 8 | } from "../extension/debuggingConfiguration/multiStepInput"; |
| 9 | import { ILaunchRequestArgs } from "../debugger/debugSessionBase"; |
| 10 | import { ExpoHostType, PlatformType } from "../extension/launchArgs"; |
| 11 | import { |
| 12 | DebugConfigurationState, |
| 13 | DebugConfigurationQuickPickItem, |
| 14 | appTypePickConfig, |
| 15 | expoHostTypePickConfig, |
| 16 | shouldUseHermesEngine, |
| 17 | DEBUG_TYPES, |
| 18 | browserTypePickConfig, |
| 19 | BROWSER_TYPES, |
| 20 | } from "../extension/debuggingConfiguration/debugConfigTypesAndConstants"; |
| 21 | import { IWDPHelper } from "../debugger/direct/IWDPHelper"; |
| 22 | import { Packager } from "./packager"; |
| 23 | |
| 24 | nls.config({ |
| 25 | messageFormat: nls.MessageFormat.bundle, |
| 26 | bundleFormat: nls.BundleFormat.standalone, |
| 27 | })(); |
| 28 | const localize = nls.loadMessageBundle(); |
| 29 | |
| 30 | export class ConfigurationProviderHelper { |
| 31 | public async selectPlatform( |
| 32 | input: MultiStepInput<DebugConfigurationState>, |
| 33 | config: Partial<ILaunchRequestArgs>, |
| 34 | platformTypePickConfig: DebugConfigurationQuickPickItem[], |
| 35 | step: number, |
| 36 | totalSteps: number, |
| 37 | ): Promise<Partial<ILaunchRequestArgs>> { |
| 38 | const pick = await input.showQuickPick< |
| 39 | DebugConfigurationQuickPickItem, |
| 40 | IQuickPickParameters<DebugConfigurationQuickPickItem> |
| 41 | >({ |
| 42 | title: localize("PlatformSelectionTitle", "Select platform"), |
| 43 | placeholder: localize("PlatformSelectionPrompt", "Platform to run on"), |
| 44 | step, |
| 45 | totalSteps, |
| 46 | items: platformTypePickConfig, |
| 47 | activeItem: platformTypePickConfig[0], |
| 48 | }); |
| 49 | |
| 50 | if (!pick) { |
| 51 | throw new Error("Platform is not selected"); |
| 52 | } |
| 53 | |
| 54 | config.platform = pick.type; |
| 55 | return config; |
| 56 | } |
| 57 | |
| 58 | public async selectApplicationType( |
| 59 | input: MultiStepInput<DebugConfigurationState>, |
| 60 | config: Partial<ILaunchRequestArgs>, |
| 61 | step: number, |
| 62 | totalSteps: number, |
| 63 | ): Promise<Partial<ILaunchRequestArgs>> { |
| 64 | const pick = await input.showQuickPick< |
| 65 | DebugConfigurationQuickPickItem, |
| 66 | IQuickPickParameters<DebugConfigurationQuickPickItem> |
| 67 | >({ |
| 68 | title: localize( |
| 69 | "ApplicationTypeSelectionTitle", |
| 70 | "Select type of React Native application", |
| 71 | ), |
| 72 | placeholder: localize( |
| 73 | "ApplicationTypeSelectionPrompt", |
| 74 | "Type of React Native application", |
| 75 | ), |
| 76 | step, |
| 77 | totalSteps, |
| 78 | items: appTypePickConfig, |
| 79 | activeItem: appTypePickConfig[0], |
| 80 | }); |
| 81 | |
| 82 | if (!pick) { |
| 83 | throw new Error("Application type is not selected"); |
| 84 | } |
| 85 | |
| 86 | config.type = pick.type; |
| 87 | return config; |
| 88 | } |
| 89 | |
| 90 | public async shouldUseHermesEngine( |
| 91 | input: MultiStepInput<DebugConfigurationState>, |
| 92 | config: Partial<ILaunchRequestArgs>, |
| 93 | step: number, |
| 94 | totalSteps: number, |
| 95 | ): Promise<Partial<ILaunchRequestArgs>> { |
| 96 | const shouldUseHermes = await input.showQuickPick< |
| 97 | DebugConfigurationQuickPickItem, |
| 98 | IQuickPickParameters<DebugConfigurationQuickPickItem> |
| 99 | >({ |
| 100 | title: localize("UseHermesEngine", "Use Hermes engine"), |
| 101 | placeholder: localize( |
| 102 | "UseHermesEnginePrompt", |
| 103 | "Use Hermes engine for direct debugging?", |
| 104 | ), |
| 105 | step, |
| 106 | totalSteps, |
| 107 | items: shouldUseHermesEngine, |
| 108 | activeItem: shouldUseHermesEngine[0], |
| 109 | }); |
| 110 | |
| 111 | if (!shouldUseHermes) { |
| 112 | throw new Error( |
| 113 | localize("UseHermesEngineInvalid", "Using Hermes engine is not confirmed"), |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | config.useHermesEngine = shouldUseHermes.type === "yes"; |
| 118 | |
| 119 | return config; |
| 120 | } |
| 121 | |
| 122 | public async selectExpoHostType( |
| 123 | input: MultiStepInput<DebugConfigurationState>, |
| 124 | config: Partial<ILaunchRequestArgs>, |
| 125 | step: number, |
| 126 | totalSteps: number, |
| 127 | ): Promise<Partial<ILaunchRequestArgs>> { |
| 128 | const pick = await input.showQuickPick< |
| 129 | DebugConfigurationQuickPickItem, |
| 130 | IQuickPickParameters<DebugConfigurationQuickPickItem> |
| 131 | >({ |
| 132 | title: localize("ExpoHostTypeSelectionTitle", "Select type of Expo host parameter"), |
| 133 | placeholder: localize("ExpoHostTypeSelectionPrompt", "Type of Expo host parameter"), |
| 134 | step, |
| 135 | totalSteps, |
| 136 | items: expoHostTypePickConfig, |
| 137 | activeItem: expoHostTypePickConfig[0], |
| 138 | }); |
| 139 | |
| 140 | if (!pick) { |
| 141 | throw new Error("Expo host type is not selected"); |
| 142 | } |
| 143 | |
| 144 | config.expoHostType = pick.type as ExpoHostType; |
| 145 | return config; |
| 146 | } |
| 147 | |
| 148 | public async selectBrowserTarget( |
| 149 | input: MultiStepInput<DebugConfigurationState>, |
| 150 | config: Partial<ILaunchRequestArgs>, |
| 151 | step: number, |
| 152 | totalSteps: number, |
| 153 | ): Promise<Partial<ILaunchRequestArgs>> { |
| 154 | const pick = await input.showQuickPick< |
| 155 | DebugConfigurationQuickPickItem, |
| 156 | IQuickPickParameters<DebugConfigurationQuickPickItem> |
| 157 | >({ |
| 158 | title: localize("BrowserTypeSelectionTitle", "Select type of browser"), |
| 159 | placeholder: localize("BrowserTypeSelectionPrompt", "Type of browser"), |
| 160 | step, |
| 161 | totalSteps, |
| 162 | items: browserTypePickConfig, |
| 163 | activeItem: browserTypePickConfig[0], |
| 164 | }); |
| 165 | |
| 166 | if (!pick) { |
| 167 | throw new Error(localize("NoBrowserTargetSelected", "Browser target is not selected")); |
| 168 | } |
| 169 | |
| 170 | config.browserTarget = pick.type as BROWSER_TYPES; |
| 171 | return config; |
| 172 | } |
| 173 | |
| 174 | public async configureAddress( |
| 175 | input: MultiStepInput<DebugConfigurationState>, |
| 176 | config: Partial<ILaunchRequestArgs>, |
| 177 | step: number, |
| 178 | totalSteps: number, |
| 179 | defaultAddress: string, |
| 180 | ): Promise<Partial<ILaunchRequestArgs>> { |
| 181 | delete config.address; |
| 182 | const address = await input.showInputBox({ |
| 183 | title: localize("AddressInputTitle", "The address of the host"), |
| 184 | step, |
| 185 | totalSteps, |
| 186 | value: defaultAddress, |
| 187 | prompt: localize("AddressInputPrompt", "Enter the address of the host"), |
| 188 | validate: value => |
| 189 | Promise.resolve( |
| 190 | value && value.trim().length > 0 |
| 191 | ? undefined |
| 192 | : localize("AddressInputInvalid", "Enter a valid host name or IP address"), |
| 193 | ), |
| 194 | }); |
| 195 | |
| 196 | if (address && address.trim() !== defaultAddress) { |
| 197 | config.address = address.trim(); |
| 198 | } |
| 199 | |
| 200 | return config; |
| 201 | } |
| 202 | |
| 203 | public async configurePort( |
| 204 | input: MultiStepInput<DebugConfigurationState>, |
| 205 | config: Partial<ILaunchRequestArgs>, |
| 206 | step: number, |
| 207 | totalSteps: number, |
| 208 | ): Promise<Partial<ILaunchRequestArgs>> { |
| 209 | delete config.port; |
| 210 | const defaultPort = String( |
| 211 | config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT && |
| 212 | config.platform === PlatformType.iOS && |
| 213 | !config.useHermesEngine |
| 214 | ? IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT |
| 215 | : Packager.DEFAULT_PORT, |
| 216 | ); |
| 217 | const portRegex = /^\d+$/; |
| 218 | |
| 219 | const portStr = await input.showInputBox({ |
| 220 | title: localize("PortInputTitle", "The port of the host"), |
| 221 | step, |
| 222 | totalSteps, |
| 223 | value: defaultPort, |
| 224 | prompt: localize( |
| 225 | "PortInputPrompt", |
| 226 | "Enter the port number that the debug server is listening on", |
| 227 | ), |
| 228 | validate: value => |
| 229 | Promise.resolve( |
| 230 | value && portRegex.test(value.trim()) |
| 231 | ? undefined |
| 232 | : localize("PortInputInvalid", "Enter a valid port number"), |
| 233 | ), |
| 234 | }); |
| 235 | |
| 236 | let portNumber: number | undefined; |
| 237 | if (portStr && portRegex.test(portStr.trim())) { |
| 238 | portNumber = parseInt(portStr, 10); |
| 239 | } |
| 240 | |
| 241 | if (portNumber && portNumber !== Packager.DEFAULT_PORT) { |
| 242 | config.port = portNumber; |
| 243 | } |
| 244 | |
| 245 | return config; |
| 246 | } |
| 247 | } |
| 248 | |