microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commands/installExpoGoApplication.ts
120lines · 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 assert from "assert"; |
| 5 | import * as https from "https"; |
| 6 | import * as os from "os"; |
| 7 | import * as vscode from "vscode"; |
| 8 | import * as nls from "vscode-nls"; |
| 9 | import { OutputChannelLogger } from "../log/OutputChannelLogger"; |
| 10 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 11 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 12 | import { downloadExpoGo } from "../../common/downloadHelper"; |
| 13 | import { getTimestamp } from "../../common/utils"; |
| 14 | import { Command } from "./util/command"; |
| 15 | |
| 16 | nls.config({ |
| 17 | messageFormat: nls.MessageFormat.bundle, |
| 18 | bundleFormat: nls.BundleFormat.standalone, |
| 19 | })(); |
| 20 | const localize = nls.loadMessageBundle(); |
| 21 | const logger = OutputChannelLogger.getMainChannel(); |
| 22 | |
| 23 | export class InstallExpoGoApplication extends Command { |
| 24 | codeName = "installExpoGoApplication"; |
| 25 | label = "Download and install Expo Go on simulator or device"; |
| 26 | error = ErrorHelper.getInternalError(InternalErrorCode.FailedToInstallExpoGo); |
| 27 | |
| 28 | async baseFn(): Promise<void> { |
| 29 | assert(this.project); |
| 30 | const item = await vscode.window.showQuickPick(["Android", "iOS"], { |
| 31 | placeHolder: "Select type for mobile OS", |
| 32 | }); |
| 33 | const expoHelper = this.project.getExponentHelper(); |
| 34 | logger.info(localize("CheckExpoEnvironment", "Checking Expo project environment.")); |
| 35 | const isExpo = await expoHelper.isExpoManagedApp(true); |
| 36 | |
| 37 | const expoGoListAPI = "https://api.expo.dev/v2/versions"; |
| 38 | const apiJson = await fetchJson(expoGoListAPI); |
| 39 | const jsonContent = JSON.parse(apiJson); |
| 40 | |
| 41 | if (isExpo) { |
| 42 | const currentSdkVersion = await expoHelper.exponentSdk(true); |
| 43 | const expoUrlInfo = jsonContent.sdkVersions[currentSdkVersion]; |
| 44 | |
| 45 | if (item == "Android") { |
| 46 | void vscode.window.showInformationMessage("Downloading Expo Go for Android."); |
| 47 | logger.logStream( |
| 48 | localize("DownloadAndroidExpoGo", "\nDownloading Expo Go for Android. \n"), |
| 49 | ); |
| 50 | |
| 51 | const targetUrl = expoUrlInfo.androidClientUrl; |
| 52 | const androidClientVersion = expoUrlInfo.androidClientVersion as string; |
| 53 | try { |
| 54 | await downloadExpoGo( |
| 55 | targetUrl, |
| 56 | `${this.project |
| 57 | .getPackager() |
| 58 | .getProjectPath()}/expogo_${androidClientVersion}_${getTimestamp()}.apk`, |
| 59 | ); |
| 60 | } catch { |
| 61 | throw new Error( |
| 62 | localize("FailedToDownloadExpoGo", "Failed to download Expo Go."), |
| 63 | ); |
| 64 | } |
| 65 | } else if (item == "iOS") { |
| 66 | if (os.platform() != "darwin") { |
| 67 | logger.warning( |
| 68 | localize( |
| 69 | "NotDarwinPlatform", |
| 70 | "Current OS may not support iOS installer. The Expo Go may not be installed.\n", |
| 71 | ), |
| 72 | ); |
| 73 | } |
| 74 | void vscode.window.showInformationMessage("Downloading Expo Go for iOS."); |
| 75 | logger.logStream( |
| 76 | localize("DownloadiOSExpoGo", "\nDownloading Expo Go for iOS. \n"), |
| 77 | ); |
| 78 | |
| 79 | const targetUrl = expoUrlInfo.iosClientUrl; |
| 80 | const iOSClientVersion = expoUrlInfo.iosClientVersion as string; |
| 81 | try { |
| 82 | await downloadExpoGo( |
| 83 | targetUrl, |
| 84 | `${this.project |
| 85 | .getPackager() |
| 86 | .getProjectPath()}/expogo_${iOSClientVersion}_${getTimestamp()}.tar.gz`, |
| 87 | ); |
| 88 | } catch { |
| 89 | throw new Error( |
| 90 | localize("FailedToDownloadExpoGo", "Failed to download Expo Go."), |
| 91 | ); |
| 92 | } |
| 93 | } else { |
| 94 | return; |
| 95 | } |
| 96 | } else { |
| 97 | throw new Error(localize("NotExpoProject", "Current project is not Expo managed.")); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | async function fetchJson(url: string): Promise<string> { |
| 103 | return new Promise<string>((fulfill, reject) => { |
| 104 | const requestOptions: https.RequestOptions = {}; |
| 105 | requestOptions.rejectUnauthorized = false; // CodeQL [js/disabling-certificate-validation] Debug extension does not need to verify certificate |
| 106 | |
| 107 | const request = https.get(url, requestOptions, response => { |
| 108 | let data = ""; |
| 109 | response.setEncoding("utf8"); |
| 110 | response.on("data", (chunk: string) => { |
| 111 | data += chunk; |
| 112 | }); |
| 113 | response.on("end", () => fulfill(data)); |
| 114 | response.on("error", reject); |
| 115 | }); |
| 116 | |
| 117 | request.on("error", reject); |
| 118 | request.end(); |
| 119 | }); |
| 120 | } |
| 121 | |