microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/xdlInterface.ts
168lines · 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 { join as pathJoin } from "path"; |
| 5 | import * as XDLPackage from "xdl"; |
| 6 | import * as MetroConfigPackage from "metro-config"; |
| 7 | import { PackageLoader, PackageConfig } from "../../common/packageLoader"; |
| 8 | import { removeModuleFromRequireCacheByName } from "../../common/utils"; |
| 9 | import { SettingsHelper } from "../settingsHelper"; |
| 10 | |
| 11 | // Defensive helper: test environments or partial activation may load this module |
| 12 | // before VS Code settings are fully available. Directly calling |
| 13 | // SettingsHelper.getExpoDependencyVersion could throw if SettingsHelper is |
| 14 | // undefined or method missing. We guard to return undefined (unversioned install) |
| 15 | // instead of crashing the whole test run. |
| 16 | function safeExpoVersion(packageName: string): string | undefined { |
| 17 | try { |
| 18 | if ( |
| 19 | SettingsHelper && |
| 20 | typeof (SettingsHelper as any).getExpoDependencyVersion === "function" |
| 21 | ) { |
| 22 | return (SettingsHelper as any).getExpoDependencyVersion(packageName); |
| 23 | } |
| 24 | } catch { |
| 25 | // Swallow any unexpected errors; unversioned dependency is acceptable. |
| 26 | } |
| 27 | return undefined; |
| 28 | } |
| 29 | |
| 30 | const XDL_PACKAGE = "xdl"; |
| 31 | const METRO_CONFIG_PACKAGE = "@expo/metro-config"; |
| 32 | |
| 33 | const xdlPackageConfig = new PackageConfig(XDL_PACKAGE, safeExpoVersion(XDL_PACKAGE)); |
| 34 | const metroConfigPackageConfig = new PackageConfig( |
| 35 | METRO_CONFIG_PACKAGE, |
| 36 | safeExpoVersion(METRO_CONFIG_PACKAGE), |
| 37 | ); |
| 38 | |
| 39 | const ngrokPackageConfig = new PackageConfig( |
| 40 | xdlPackageConfig.getPackageName(), |
| 41 | xdlPackageConfig.getVersion(), |
| 42 | "build/start/resolveNgrok", |
| 43 | ); |
| 44 | |
| 45 | // There is the problem with '--no-save' flag for 'npm install' command for npm v6. |
| 46 | // Installing npm dependencies with the `--no-save` flag will remove |
| 47 | // other dependencies that were installed previously in the same manner (https://github.com/npm/cli/issues/1460). |
| 48 | // So we should workaround it passing all packages for install to only one npm install command |
| 49 | const EXPO_DEPS: PackageConfig[] = [xdlPackageConfig, metroConfigPackageConfig]; |
| 50 | |
| 51 | export const getXDLPackage: () => Promise<typeof XDLPackage> = |
| 52 | PackageLoader.getInstance().generateGetPackageFunction<typeof XDLPackage>( |
| 53 | xdlPackageConfig, |
| 54 | ...EXPO_DEPS, |
| 55 | ); |
| 56 | export const getMetroConfigPackage: () => Promise<typeof MetroConfigPackage> = |
| 57 | PackageLoader.getInstance().generateGetPackageFunction<typeof MetroConfigPackage>( |
| 58 | metroConfigPackageConfig, |
| 59 | ...EXPO_DEPS, |
| 60 | ); |
| 61 | export const getNgrokResolver: () => Promise<XDLPackage.ResolveNgrok> = |
| 62 | PackageLoader.getInstance().generateGetPackageFunction<XDLPackage.ResolveNgrok>( |
| 63 | ngrokPackageConfig, |
| 64 | ...EXPO_DEPS, |
| 65 | ); |
| 66 | |
| 67 | export type IUser = XDLPackage.IUser; |
| 68 | |
| 69 | export async function configReactNativeVersionWarnings(): Promise<void> { |
| 70 | const xdlPackage = await getXDLPackage(); |
| 71 | if (xdlPackage.Config.validation !== undefined) { |
| 72 | xdlPackage.Config.validation.reactNativeVersionWarnings = false; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | export async function attachLoggerStream( |
| 77 | rootPath: string, |
| 78 | options?: XDLPackage.IBunyanStream | any, |
| 79 | ): Promise<void> { |
| 80 | (await getXDLPackage()).ProjectUtils.attachLoggerStream(rootPath, options); |
| 81 | } |
| 82 | |
| 83 | export async function currentUser(): Promise<XDLPackage.IUser> { |
| 84 | const xdl = await getXDLPackage(); |
| 85 | return await (xdl.User |
| 86 | ? xdl.User.getCurrentUserAsync() |
| 87 | : xdl.UserManager.getCurrentUserAsync()); |
| 88 | } |
| 89 | |
| 90 | export async function login(username: string, password: string): Promise<XDLPackage.IUser> { |
| 91 | const xdl = await getXDLPackage(); |
| 92 | return await (xdl.User |
| 93 | ? xdl.User.loginAsync("user-pass", { username, password }) |
| 94 | : xdl.UserManager.loginAsync("user-pass", { |
| 95 | username, |
| 96 | password, |
| 97 | })); |
| 98 | } |
| 99 | |
| 100 | export async function getExpoSdkVersions(): Promise<XDLPackage.SDKVersions> { |
| 101 | return (await getXDLPackage()).Versions.sdkVersionsAsync(); |
| 102 | } |
| 103 | |
| 104 | export async function getReleasedExpoSdkVersions(): Promise<XDLPackage.SDKVersions> { |
| 105 | return (await getXDLPackage()).Versions.releasedSdkVersionsAsync(); |
| 106 | } |
| 107 | |
| 108 | export async function publish( |
| 109 | projectRoot: string, |
| 110 | options?: XDLPackage.IPublishOptions, |
| 111 | ): Promise<XDLPackage.IPublishResponse> { |
| 112 | return (await getXDLPackage()).Project.publishAsync(projectRoot, options); |
| 113 | } |
| 114 | |
| 115 | export async function setOptions(projectRoot: string, options: XDLPackage.IOptions): Promise<void> { |
| 116 | await (await getXDLPackage()).ProjectSettings.setPackagerInfoAsync(projectRoot, options); |
| 117 | } |
| 118 | |
| 119 | export async function startExponentServer(projectRoot: string): Promise<void> { |
| 120 | await (await getXDLPackage()).Project.startExpoServerAsync(projectRoot); |
| 121 | } |
| 122 | |
| 123 | export async function startTunnels(projectRoot: string): Promise<void> { |
| 124 | await (await getXDLPackage()).Project.startTunnelsAsync(projectRoot); |
| 125 | } |
| 126 | |
| 127 | export async function getUrl( |
| 128 | projectRoot: string, |
| 129 | options?: XDLPackage.IUrlOptions, |
| 130 | ): Promise<string> { |
| 131 | return (await getXDLPackage()).UrlUtils.constructManifestUrlAsync(projectRoot, options); |
| 132 | } |
| 133 | |
| 134 | export async function stopAll(projectRoot: string): Promise<void> { |
| 135 | await (await getXDLPackage()).Project.stopAsync(projectRoot); |
| 136 | } |
| 137 | |
| 138 | export async function startAdbReverse(projectRoot: string): Promise<boolean> { |
| 139 | return (await getXDLPackage()).Android.startAdbReverseAsync(projectRoot); |
| 140 | } |
| 141 | |
| 142 | export async function stopAdbReverse(projectRoot: string): Promise<void> { |
| 143 | await (await getXDLPackage()).Android.stopAdbReverseAsync(projectRoot); |
| 144 | } |
| 145 | |
| 146 | export async function getMetroConfig( |
| 147 | projectRoot: string, |
| 148 | ): Promise<MetroConfigPackage.IMetroConfig> { |
| 149 | return (await getMetroConfigPackage()).loadAsync(projectRoot); |
| 150 | } |
| 151 | |
| 152 | export async function isNgrokInstalled(projectRoot: string): Promise<boolean> { |
| 153 | const ngrokResolver = await getNgrokResolver(); |
| 154 | try { |
| 155 | const ngrok = await ngrokResolver.resolveNgrokAsync(projectRoot, { |
| 156 | shouldPrompt: false, |
| 157 | autoInstall: false, |
| 158 | }); |
| 159 | return !!ngrok; |
| 160 | } catch (err) { |
| 161 | // If unsupported version of the "@expo/ngrok" package was detected, we need to update the package. |
| 162 | // Since the "require" method used to parse the "ngrok⁄package.json" file in the "xdl" package caches |
| 163 | // all processed modules, we have to remove this file from cache to be able to require a new version |
| 164 | // of that file after the update of the "@expo/ngrok" package |
| 165 | removeModuleFromRequireCacheByName(pathJoin("ngrok", "package.json")); |
| 166 | throw err; |
| 167 | } |
| 168 | } |
| 169 | |