microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.7.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/exponent/exponentPlatform.ts

195lines · 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
031832ffArtem Egorov8 years ago4import { ErrorHelper } from "../../common/error/errorHelper";
5import { InternalErrorCode } from "../../common/error/internalErrorCode";
5471436aRedMickey5 years ago6import { ExpoHostType, IExponentRunOptions, PlatformType } from "../launchArgs";
031832ffArtem Egorov8 years ago7import { GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform";
8import { ExponentHelper } from "./exponentHelper";
9import { TelemetryHelper } from "../../common/telemetryHelper";
3c871141Yuri Skorokhodov7 years ago10import { QRCodeContentProvider } from "../qrCodeContentProvider";
0a68f8dbArtem Egorov8 years ago11
12import * as vscode from "vscode";
4787ec09Artem Egorov8 years ago13import * as XDL from "./xdlInterface";
14import * as url from "url";
d7d405aeYuri Skorokhodov7 years ago15import * as nls from "vscode-nls";
34472878RedMickey5 years ago16nls.config({
17messageFormat: nls.MessageFormat.bundle,
18bundleFormat: nls.BundleFormat.standalone,
19})();
d7d405aeYuri Skorokhodov7 years ago20const localize = nls.loadMessageBundle();
0a68f8dbArtem Egorov8 years ago21
22export class ExponentPlatform extends GeneralMobilePlatform {
23private exponentTunnelPath: string | null;
2e432a9eArtem Egorov8 years ago24private exponentHelper: ExponentHelper;
3c871141Yuri Skorokhodov7 years ago25private qrCodeContentProvider: QRCodeContentProvider = new QRCodeContentProvider();
0a68f8dbArtem Egorov8 years ago26
62c4de22RedMickey6 years ago27constructor(runOptions: IExponentRunOptions, platformDeps: MobilePlatformDeps = {}) {
0a68f8dbArtem Egorov8 years ago28super(runOptions, platformDeps);
c036b0d3JiglioNero6 years ago29this.exponentHelper = this.packager.getExponentHelper();
0a68f8dbArtem Egorov8 years ago30this.exponentTunnelPath = null;
31}
32
0d77292aJiglioNero4 years ago33public async runApp(): Promise<void> {
ba953e9fRedMickey6 years ago34let extProps = {
031832ffArtem Egorov8 years ago35platform: {
259c018fYuri Skorokhodov5 years ago36value: PlatformType.Exponent,
031832ffArtem Egorov8 years ago37isPii: false,
4787ec09Artem Egorov8 years ago38},
031832ffArtem Egorov8 years ago39};
4787ec09Artem Egorov8 years ago40
34472878RedMickey5 years ago41extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
42this.runOptions,
43this.runOptions.reactNativeVersions,
44extProps,
45);
ba953e9fRedMickey6 years ago46
0d77292aJiglioNero4 years ago47await TelemetryHelper.generate("ExponentPlatform.runApp", extProps, async () => {
48await this.loginToExponentOrSkip(this.runOptions.expoHostType);
49await XDL.setOptions(this.projectPath, { packagerPort: this.packager.getPort() });
50await XDL.startExponentServer(this.projectPath);
51
52// the purpose of this is to save the same sequence of handling 'adb reverse' command execution as in Expo
53// https://github.com/expo/expo-cli/blob/1d515d21200841e181518358fd9dc4c7b24c7cd6/packages/xdl/src/Project.ts#L2226-L2370
54// we added this to be sure that our Expo launching logic doesn't have any negative side effects
55
56if (this.runOptions.expoHostType === "tunnel") {
57await this.prepareExpoTunnels();
58} else {
59await XDL.stopAdbReverse(this.projectPath);
60}
61
62const isAdbReversed =
63this.runOptions.expoHostType !== "local"
64? false
65: // 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
66await XDL.startAdbReverse(this.projectPath);
67let exponentUrl = "";
68switch (this.runOptions.expoHostType) {
69case "lan":
70exponentUrl = await XDL.getUrl(this.projectPath, {
71dev: true,
72minify: false,
73hostType: "lan",
74});
75break;
76case "local":
77if (isAdbReversed) {
78this.logger.info(
79localize(
80"ExpoStartAdbReverseSuccess",
81"A device or an emulator was found, 'adb reverse' command successfully executed.",
82),
83);
84} else {
85this.logger.warning(
86localize(
87"ExpoStartAdbReverseFailure",
88"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.",
89),
34472878RedMickey5 years ago90);
0d77292aJiglioNero4 years ago91}
92
93exponentUrl = await XDL.getUrl(this.projectPath, {
94dev: true,
95minify: false,
96hostType: "localhost",
ce5e88eeYuri Skorokhodov5 years ago97});
0d77292aJiglioNero4 years ago98break;
99case "tunnel":
100default:
101exponentUrl = await XDL.getUrl(this.projectPath, { dev: true, minify: false });
102}
103exponentUrl = "exp://" + url.parse(exponentUrl).host;
104
105if (!exponentUrl) {
106throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath);
107}
108
109if (this.runOptions.openExpoQR) {
110let exponentPage = vscode.window.createWebviewPanel(
111"Expo QR Code",
112"Expo QR Code",
113vscode.ViewColumn.Two,
114{},
115);
116exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(
117vscode.Uri.parse(exponentUrl),
118);
119}
120
121this.exponentTunnelPath = exponentUrl;
122const outputMessage = localize(
123"ExponentServerIsRunningOpenToSeeIt",
124"Expo server is running. Open your Expo app at {0} to see it.",
125this.exponentTunnelPath,
126);
127this.logger.info(outputMessage);
128
129const copyButton = localize("CopyToClipboard", "Copy to clipboard");
130
131vscode.window.showInformationMessage(outputMessage, copyButton).then(selection => {
132if (selection === copyButton) {
133vscode.env.clipboard.writeText(exponentUrl);
134}
ce5e88eeYuri Skorokhodov5 years ago135});
4787ec09Artem Egorov8 years ago136});
137}
138
0d77292aJiglioNero4 years ago139public async loginToExponentOrSkip(expoHostType?: ExpoHostType): Promise<any> {
62c4de22RedMickey6 years ago140if (expoHostType !== "tunnel") {
0d77292aJiglioNero4 years ago141return;
62c4de22RedMickey6 years ago142}
0d77292aJiglioNero4 years ago143
144return await this.exponentHelper.loginToExponent(
145async (message, password) => {
146return (
147(await vscode.window.showInputBox({
148placeHolder: message,
149password: password,
150})) || ""
151);
62c4de22RedMickey6 years ago152},
0d77292aJiglioNero4 years ago153async message => {
154const okButton = { title: "Ok" };
155const cancelButton = { title: "Cancel", isCloseAffordance: true };
156const answer = await vscode.window.showInformationMessage(
157message,
158{ modal: true },
159okButton,
160cancelButton,
161);
162if (answer === cancelButton) {
163throw ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin);
164}
165return "";
34472878RedMickey5 years ago166},
62c4de22RedMickey6 years ago167);
168}
169
0d77292aJiglioNero4 years ago170public async beforeStartPackager(): Promise<void> {
4787ec09Artem Egorov8 years ago171return this.exponentHelper.configureExponentEnvironment();
0a68f8dbArtem Egorov8 years ago172}
173
0d77292aJiglioNero4 years ago174public async enableJSDebuggingMode(): Promise<void> {
34472878RedMickey5 years ago175this.logger.info(
176localize(
177"ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging",
178"Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging.",
179),
180);
0a68f8dbArtem Egorov8 years ago181}
db6fd42aRuslan Bikkinin7 years ago182
cbc7ac5bArtem Egorov7 years ago183public getRunArguments(): string[] {
db6fd42aRuslan Bikkinin7 years ago184return [];
185}
efb436fcRedMickey5 years ago186
0d77292aJiglioNero4 years ago187private async prepareExpoTunnels(): Promise<void> {
188try {
189await this.exponentHelper.findOrInstallNgrokGlobally();
190await XDL.startTunnels(this.projectPath);
191} finally {
192this.exponentHelper.removeNodeModulesPathFromEnvIfWasSet();
193}
efb436fcRedMickey5 years ago194}
0a68f8dbArtem Egorov8 years ago195}