microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.9.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/exponent/exponentPlatform.ts

193lines · modeblame

0a68f8dbArtem Egorov8 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
09f6024fHeniker4 years ago4import * as url from "url";
5import * as vscode from "vscode";
6import * as nls from "vscode-nls";
031832ffArtem Egorov8 years ago7import { ErrorHelper } from "../../common/error/errorHelper";
8import { InternalErrorCode } from "../../common/error/internalErrorCode";
5471436aRedMickey5 years ago9import { ExpoHostType, IExponentRunOptions, PlatformType } from "../launchArgs";
4cd25962JiglioNero4 years ago10import { GeneralPlatform, MobilePlatformDeps } from "../generalPlatform";
031832ffArtem Egorov8 years ago11import { TelemetryHelper } from "../../common/telemetryHelper";
3c871141Yuri Skorokhodov7 years ago12import { QRCodeContentProvider } from "../qrCodeContentProvider";
09f6024fHeniker4 years ago13import { ExponentHelper } from "./exponentHelper";
0a68f8dbArtem Egorov8 years ago14
4787ec09Artem Egorov8 years ago15import * as XDL from "./xdlInterface";
09f6024fHeniker4 years ago16
34472878RedMickey5 years ago17nls.config({
18messageFormat: nls.MessageFormat.bundle,
19bundleFormat: nls.BundleFormat.standalone,
20})();
d7d405aeYuri Skorokhodov7 years ago21const localize = nls.loadMessageBundle();
0a68f8dbArtem Egorov8 years ago22
4cd25962JiglioNero4 years ago23export class ExponentPlatform extends GeneralPlatform {
0a68f8dbArtem Egorov8 years ago24private exponentTunnelPath: string | null;
2e432a9eArtem Egorov8 years ago25private exponentHelper: ExponentHelper;
3c871141Yuri Skorokhodov7 years ago26private qrCodeContentProvider: QRCodeContentProvider = new QRCodeContentProvider();
0a68f8dbArtem Egorov8 years ago27
62c4de22RedMickey6 years ago28constructor(runOptions: IExponentRunOptions, platformDeps: MobilePlatformDeps = {}) {
0a68f8dbArtem Egorov8 years ago29super(runOptions, platformDeps);
c036b0d3JiglioNero6 years ago30this.exponentHelper = this.packager.getExponentHelper();
0a68f8dbArtem Egorov8 years ago31this.exponentTunnelPath = null;
32}
33
0d77292aJiglioNero4 years ago34public async runApp(): Promise<void> {
ba953e9fRedMickey6 years ago35let extProps = {
031832ffArtem Egorov8 years ago36platform: {
259c018fYuri Skorokhodov5 years ago37value: PlatformType.Exponent,
031832ffArtem Egorov8 years ago38isPii: false,
4787ec09Artem Egorov8 years ago39},
031832ffArtem Egorov8 years ago40};
4787ec09Artem Egorov8 years ago41
34472878RedMickey5 years ago42extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
43this.runOptions,
44this.runOptions.reactNativeVersions,
45extProps,
46);
ba953e9fRedMickey6 years ago47
0d77292aJiglioNero4 years ago48await TelemetryHelper.generate("ExponentPlatform.runApp", extProps, async () => {
49await this.loginToExponentOrSkip(this.runOptions.expoHostType);
50await XDL.setOptions(this.projectPath, { packagerPort: this.packager.getPort() });
51await XDL.startExponentServer(this.projectPath);
52
53// the purpose of this is to save the same sequence of handling 'adb reverse' command execution as in Expo
54// https://github.com/expo/expo-cli/blob/1d515d21200841e181518358fd9dc4c7b24c7cd6/packages/xdl/src/Project.ts#L2226-L2370
55// we added this to be sure that our Expo launching logic doesn't have any negative side effects
56
57if (this.runOptions.expoHostType === "tunnel") {
58await this.prepareExpoTunnels();
59} else {
60await XDL.stopAdbReverse(this.projectPath);
61}
62
63const isAdbReversed =
64this.runOptions.expoHostType !== "local"
65? false
66: // we need to execute 'adb reverse' command to bind ports used by Expo and RN of local machine to ports of a connected Android device or a running emulator
67await XDL.startAdbReverse(this.projectPath);
68let exponentUrl = "";
69switch (this.runOptions.expoHostType) {
70case "lan":
71exponentUrl = await XDL.getUrl(this.projectPath, {
72dev: true,
73minify: false,
74hostType: "lan",
75});
76break;
77case "local":
78if (isAdbReversed) {
79this.logger.info(
80localize(
81"ExpoStartAdbReverseSuccess",
82"A device or an emulator was found, 'adb reverse' command successfully executed.",
83),
84);
85} else {
86this.logger.warning(
87localize(
88"ExpoStartAdbReverseFailure",
89"Adb reverse command failed. Couldn't find connected over usb device or running emulator. Also please make sure that there is only one currently connected device or running emulator.",
90),
34472878RedMickey5 years ago91);
0d77292aJiglioNero4 years ago92}
93
94exponentUrl = await XDL.getUrl(this.projectPath, {
95dev: true,
96minify: false,
97hostType: "localhost",
ce5e88eeYuri Skorokhodov5 years ago98});
0d77292aJiglioNero4 years ago99break;
100case "tunnel":
101default:
102exponentUrl = await XDL.getUrl(this.projectPath, { dev: true, minify: false });
103}
09f6024fHeniker4 years ago104exponentUrl = `exp://${String(url.parse(exponentUrl).host)}`;
0d77292aJiglioNero4 years ago105
106if (!exponentUrl) {
107throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath);
108}
109
110if (this.runOptions.openExpoQR) {
09f6024fHeniker4 years ago111const exponentPage = vscode.window.createWebviewPanel(
0d77292aJiglioNero4 years ago112"Expo QR Code",
113"Expo QR Code",
114vscode.ViewColumn.Two,
115{},
116);
117exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(
118vscode.Uri.parse(exponentUrl),
119);
120}
121
122this.exponentTunnelPath = exponentUrl;
123const outputMessage = localize(
124"ExponentServerIsRunningOpenToSeeIt",
125"Expo server is running. Open your Expo app at {0} to see it.",
126this.exponentTunnelPath,
127);
128this.logger.info(outputMessage);
129
130const copyButton = localize("CopyToClipboard", "Copy to clipboard");
131
09f6024fHeniker4 years ago132void vscode.window.showInformationMessage(outputMessage, copyButton).then(selection => {
0d77292aJiglioNero4 years ago133if (selection === copyButton) {
09f6024fHeniker4 years ago134void vscode.env.clipboard.writeText(exponentUrl);
0d77292aJiglioNero4 years ago135}
ce5e88eeYuri Skorokhodov5 years ago136});
4787ec09Artem Egorov8 years ago137});
138}
139
0d77292aJiglioNero4 years ago140public async loginToExponentOrSkip(expoHostType?: ExpoHostType): Promise<any> {
62c4de22RedMickey6 years ago141if (expoHostType !== "tunnel") {
0d77292aJiglioNero4 years ago142return;
62c4de22RedMickey6 years ago143}
0d77292aJiglioNero4 years ago144
145return await this.exponentHelper.loginToExponent(
09f6024fHeniker4 years ago146async (message, password) =>
147(await vscode.window.showInputBox({
148placeHolder: message,
149password,
150})) || "",
0d77292aJiglioNero4 years ago151async message => {
152const okButton = { title: "Ok" };
153const cancelButton = { title: "Cancel", isCloseAffordance: true };
154const answer = await vscode.window.showInformationMessage(
155message,
156{ modal: true },
157okButton,
158cancelButton,
159);
160if (answer === cancelButton) {
161throw ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin);
162}
163return "";
34472878RedMickey5 years ago164},
62c4de22RedMickey6 years ago165);
166}
167
0d77292aJiglioNero4 years ago168public async beforeStartPackager(): Promise<void> {
4787ec09Artem Egorov8 years ago169return this.exponentHelper.configureExponentEnvironment();
0a68f8dbArtem Egorov8 years ago170}
171
0d77292aJiglioNero4 years ago172public async enableJSDebuggingMode(): Promise<void> {
34472878RedMickey5 years ago173this.logger.info(
174localize(
175"ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging",
176"Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging.",
177),
178);
0a68f8dbArtem Egorov8 years ago179}
db6fd42aRuslan Bikkinin7 years ago180
cbc7ac5bArtem Egorov7 years ago181public getRunArguments(): string[] {
db6fd42aRuslan Bikkinin7 years ago182return [];
183}
efb436fcRedMickey5 years ago184
0d77292aJiglioNero4 years ago185private async prepareExpoTunnels(): Promise<void> {
186try {
187await this.exponentHelper.findOrInstallNgrokGlobally();
188await XDL.startTunnels(this.projectPath);
189} finally {
190this.exponentHelper.removeNodeModulesPathFromEnvIfWasSet();
191}
efb436fcRedMickey5 years ago192}
0a68f8dbArtem Egorov8 years ago193}