microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.3.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/exponent/exponentPlatform.ts

207lines · 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
ce5e88eeYuri Skorokhodov5 years ago33public 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
ce5e88eeYuri Skorokhodov5 years ago47return new Promise((resolve, reject) => {
34472878RedMickey5 years ago48TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => {
ce5e88eeYuri Skorokhodov5 years ago49return this.loginToExponentOrSkip(this.runOptions.expoHostType)
50.then(() =>
34472878RedMickey5 years ago51XDL.setOptions(this.projectPath, { packagerPort: this.packager.getPort() }),
ce5e88eeYuri Skorokhodov5 years ago52)
34472878RedMickey5 years ago53.then(() => XDL.startExponentServer(this.projectPath))
ce5e88eeYuri Skorokhodov5 years ago54.then(() => {
55if (this.runOptions.expoHostType !== "tunnel") {
56// the purpose of this is to save the same sequence of handling 'adb reverse' command execution as in Expo
57// https://github.com/expo/expo-cli/blob/1d515d21200841e181518358fd9dc4c7b24c7cd6/packages/xdl/src/Project.ts#L2226-L2370
58// we added this to be sure that our Expo launching logic doesn't have any negative side effects
59return XDL.stopAdbReverse(this.projectPath);
60}
61return XDL.startTunnels(this.projectPath);
62})
63.then(() => {
64if (this.runOptions.expoHostType !== "local") return 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
66return XDL.startAdbReverse(this.projectPath);
67})
34472878RedMickey5 years ago68.then(isAdbReversed => {
ce5e88eeYuri Skorokhodov5 years ago69switch (this.runOptions.expoHostType) {
70case "lan":
34472878RedMickey5 years ago71return XDL.getUrl(this.projectPath, {
72dev: true,
73minify: false,
74hostType: "lan",
75});
ce5e88eeYuri Skorokhodov5 years ago76case "local":
77if (isAdbReversed) {
34472878RedMickey5 years ago78this.logger.info(
79localize(
80"ExpoStartAdbReverseSuccess",
81"A device or an emulator was found, 'adb reverse' command successfully executed.",
82),
83);
ce5e88eeYuri Skorokhodov5 years ago84} else {
34472878RedMickey5 years ago85this.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),
90);
ce5e88eeYuri Skorokhodov5 years ago91}
62c4de22RedMickey6 years ago92
34472878RedMickey5 years ago93return XDL.getUrl(this.projectPath, {
94dev: true,
95minify: false,
96hostType: "localhost",
97});
ce5e88eeYuri Skorokhodov5 years ago98case "tunnel":
99default:
100return XDL.getUrl(this.projectPath, { dev: true, minify: false });
101}
102})
103.then(exponentUrl => {
104return "exp://" + url.parse(exponentUrl).host;
105})
106.then(exponentUrl => {
58882bb6JiglioNero5 years ago107if (this.runOptions.openExpoQR) {
34472878RedMickey5 years ago108let exponentPage = vscode.window.createWebviewPanel(
109"Expo QR Code",
110"Expo QR Code",
111vscode.ViewColumn.Two,
112{},
113);
114exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(
115vscode.Uri.parse(exponentUrl),
116);
58882bb6JiglioNero5 years ago117}
ce5e88eeYuri Skorokhodov5 years ago118return exponentUrl;
119})
120.then(exponentUrl => {
121if (!exponentUrl) {
34472878RedMickey5 years ago122return reject(
123ErrorHelper.getInternalError(
124InternalErrorCode.ExpectedExponentTunnelPath,
125),
126);
ce5e88eeYuri Skorokhodov5 years ago127}
128this.exponentTunnelPath = exponentUrl;
34472878RedMickey5 years ago129const outputMessage = localize(
130"ExponentServerIsRunningOpenToSeeIt",
131"Expo server is running. Open your Expo app at {0} to see it.",
132this.exponentTunnelPath,
133);
ce5e88eeYuri Skorokhodov5 years ago134this.logger.info(outputMessage);
031832ffArtem Egorov8 years ago135
58882bb6JiglioNero5 years ago136const copyButton = localize("CopyToClipboard", "Copy to clipboard");
137
34472878RedMickey5 years ago138vscode.window
139.showInformationMessage(outputMessage, copyButton)
140.then(selection => {
141if (selection === copyButton) {
142vscode.env.clipboard.writeText(exponentUrl);
143}
144});
58882bb6JiglioNero5 years ago145
ce5e88eeYuri Skorokhodov5 years ago146return resolve();
147})
148.catch(reason => {
149return reject(reason);
150});
151});
4787ec09Artem Egorov8 years ago152});
153}
154
5471436aRedMickey5 years ago155public loginToExponentOrSkip(expoHostType?: ExpoHostType): Promise<any> {
62c4de22RedMickey6 years ago156if (expoHostType !== "tunnel") {
34472878RedMickey5 years ago157return Promise.resolve();
62c4de22RedMickey6 years ago158}
159return this.exponentHelper.loginToExponent(
160(message, password) => {
ce5e88eeYuri Skorokhodov5 years ago161return new Promise((resolve, reject) => {
34472878RedMickey5 years ago162vscode.window
163.showInputBox({ placeHolder: message, password: password })
62c4de22RedMickey6 years ago164.then(login => {
165resolve(login || "");
166}, reject);
167});
168},
34472878RedMickey5 years ago169message => {
ce5e88eeYuri Skorokhodov5 years ago170return new Promise((resolve, reject) => {
34472878RedMickey5 years ago171const okButton = { title: "Ok" };
172const cancelButton = { title: "Cancel", isCloseAffordance: true };
173vscode.window
174.showInformationMessage(message, { modal: true }, okButton, cancelButton)
62c4de22RedMickey6 years ago175.then(answer => {
176if (answer === cancelButton) {
34472878RedMickey5 years ago177reject(
178ErrorHelper.getInternalError(
179InternalErrorCode.UserCancelledExpoLogin,
180),
181);
62c4de22RedMickey6 years ago182}
183resolve("");
184}, reject);
185});
34472878RedMickey5 years ago186},
62c4de22RedMickey6 years ago187);
188}
189
ce5e88eeYuri Skorokhodov5 years ago190public beforeStartPackager(): Promise<void> {
4787ec09Artem Egorov8 years ago191return this.exponentHelper.configureExponentEnvironment();
0a68f8dbArtem Egorov8 years ago192}
193
ce5e88eeYuri Skorokhodov5 years ago194public enableJSDebuggingMode(): Promise<void> {
34472878RedMickey5 years ago195this.logger.info(
196localize(
197"ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging",
198"Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging.",
199),
200);
ce5e88eeYuri Skorokhodov5 years ago201return Promise.resolve();
0a68f8dbArtem Egorov8 years ago202}
db6fd42aRuslan Bikkinin7 years ago203
cbc7ac5bArtem Egorov7 years ago204public getRunArguments(): string[] {
db6fd42aRuslan Bikkinin7 years ago205return [];
206}
0a68f8dbArtem Egorov8 years ago207}