microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fix-ts-error1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/configurationProviderHelper.ts

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