microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/xdlInterface.ts
168lines · modeblame
7059d307Patricio Beltran9 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
65e55367RedMickey4 years ago | 4 | import { join as pathJoin } from "path"; |
7059d307Patricio Beltran9 years ago | 5 | import * as XDLPackage from "xdl"; |
242759feJiglioNero5 years ago | 6 | import * as MetroConfigPackage from "metro-config"; |
716f31d0JiglioNero5 years ago | 7 | import { PackageLoader, PackageConfig } from "../../common/packageLoader"; |
65e55367RedMickey4 years ago | 8 | import { removeModuleFromRequireCacheByName } from "../../common/utils"; |
716f31d0JiglioNero5 years ago | 9 | import { SettingsHelper } from "../settingsHelper"; |
0a68f8dbArtem Egorov8 years ago | 10 | |
780e0237Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago | 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 | | |
00a26245RedMickey5 years ago | 30 | const XDL_PACKAGE = "xdl"; |
242759feJiglioNero5 years ago | 31 | const METRO_CONFIG_PACKAGE = "@expo/metro-config"; |
| 32 | | |
780e0237Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago | 33 | const xdlPackageConfig = new PackageConfig(XDL_PACKAGE, safeExpoVersion(XDL_PACKAGE)); |
716f31d0JiglioNero5 years ago | 34 | const metroConfigPackageConfig = new PackageConfig( |
| 35 | METRO_CONFIG_PACKAGE, | |
780e0237Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago | 36 | safeExpoVersion(METRO_CONFIG_PACKAGE), |
716f31d0JiglioNero5 years ago | 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]; | |
242759feJiglioNero5 years ago | 50 | |
09f6024fHeniker4 years ago | 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 | ); | |
7059d307Patricio Beltran9 years ago | 66 | |
| 67 | export type IUser = XDLPackage.IUser; | |
| 68 | | |
0d77292aJiglioNero4 years ago | 69 | export async function configReactNativeVersionWarnings(): Promise<void> { |
50a7f6ecbenjaminbi2 years ago | 70 | const xdlPackage = await getXDLPackage(); |
| 71 | if (xdlPackage.Config.validation !== undefined) { | |
| 72 | xdlPackage.Config.validation.reactNativeVersionWarnings = false; | |
| 73 | } | |
7059d307Patricio Beltran9 years ago | 74 | } |
| 75 | | |
0d77292aJiglioNero4 years ago | 76 | export async function attachLoggerStream( |
34472878RedMickey5 years ago | 77 | rootPath: string, |
| 78 | options?: XDLPackage.IBunyanStream | any, | |
| 79 | ): Promise<void> { | |
0d77292aJiglioNero4 years ago | 80 | (await getXDLPackage()).ProjectUtils.attachLoggerStream(rootPath, options); |
7059d307Patricio Beltran9 years ago | 81 | } |
| 82 | | |
0d77292aJiglioNero4 years ago | 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()); | |
7059d307Patricio Beltran9 years ago | 88 | } |
| 89 | | |
0d77292aJiglioNero4 years ago | 90 | export async function login(username: string, password: string): Promise<XDLPackage.IUser> { |
| 91 | const xdl = await getXDLPackage(); | |
| 92 | return await (xdl.User | |
09f6024fHeniker4 years ago | 93 | ? xdl.User.loginAsync("user-pass", { username, password }) |
0d77292aJiglioNero4 years ago | 94 | : xdl.UserManager.loginAsync("user-pass", { |
09f6024fHeniker4 years ago | 95 | username, |
| 96 | password, | |
0d77292aJiglioNero4 years ago | 97 | })); |
7059d307Patricio Beltran9 years ago | 98 | } |
| 99 | | |
0d77292aJiglioNero4 years ago | 100 | export async function getExpoSdkVersions(): Promise<XDLPackage.SDKVersions> { |
| 101 | return (await getXDLPackage()).Versions.sdkVersionsAsync(); | |
8f50947fRedMickey5 years ago | 102 | } |
| 103 | | |
0d77292aJiglioNero4 years ago | 104 | export async function getReleasedExpoSdkVersions(): Promise<XDLPackage.SDKVersions> { |
| 105 | return (await getXDLPackage()).Versions.releasedSdkVersionsAsync(); | |
7059d307Patricio Beltran9 years ago | 106 | } |
| 107 | | |
0d77292aJiglioNero4 years ago | 108 | export async function publish( |
34472878RedMickey5 years ago | 109 | projectRoot: string, |
| 110 | options?: XDLPackage.IPublishOptions, | |
| 111 | ): Promise<XDLPackage.IPublishResponse> { | |
0d77292aJiglioNero4 years ago | 112 | return (await getXDLPackage()).Project.publishAsync(projectRoot, options); |
7059d307Patricio Beltran9 years ago | 113 | } |
| 114 | | |
ec230406RedMickey4 years ago | 115 | export async function setOptions(projectRoot: string, options: XDLPackage.IOptions): Promise<void> { |
| 116 | await (await getXDLPackage()).ProjectSettings.setPackagerInfoAsync(projectRoot, options); | |
7059d307Patricio Beltran9 years ago | 117 | } |
| 118 | | |
0d77292aJiglioNero4 years ago | 119 | export async function startExponentServer(projectRoot: string): Promise<void> { |
| 120 | await (await getXDLPackage()).Project.startExpoServerAsync(projectRoot); | |
7059d307Patricio Beltran9 years ago | 121 | } |
| 122 | | |
0d77292aJiglioNero4 years ago | 123 | export async function startTunnels(projectRoot: string): Promise<void> { |
| 124 | await (await getXDLPackage()).Project.startTunnelsAsync(projectRoot); | |
7059d307Patricio Beltran9 years ago | 125 | } |
| 126 | | |
0d77292aJiglioNero4 years ago | 127 | export async function getUrl( |
| 128 | projectRoot: string, | |
| 129 | options?: XDLPackage.IUrlOptions, | |
| 130 | ): Promise<string> { | |
| 131 | return (await getXDLPackage()).UrlUtils.constructManifestUrlAsync(projectRoot, options); | |
7059d307Patricio Beltran9 years ago | 132 | } |
| 133 | | |
0d77292aJiglioNero4 years ago | 134 | export async function stopAll(projectRoot: string): Promise<void> { |
| 135 | await (await getXDLPackage()).Project.stopAsync(projectRoot); | |
7059d307Patricio Beltran9 years ago | 136 | } |
62c4de22RedMickey6 years ago | 137 | |
0d77292aJiglioNero4 years ago | 138 | export async function startAdbReverse(projectRoot: string): Promise<boolean> { |
| 139 | return (await getXDLPackage()).Android.startAdbReverseAsync(projectRoot); | |
62c4de22RedMickey6 years ago | 140 | } |
| 141 | | |
0d77292aJiglioNero4 years ago | 142 | export async function stopAdbReverse(projectRoot: string): Promise<void> { |
| 143 | await (await getXDLPackage()).Android.stopAdbReverseAsync(projectRoot); | |
242759feJiglioNero5 years ago | 144 | } |
| 145 | | |
0d77292aJiglioNero4 years ago | 146 | export async function getMetroConfig( |
| 147 | projectRoot: string, | |
| 148 | ): Promise<MetroConfigPackage.IMetroConfig> { | |
| 149 | return (await getMetroConfigPackage()).loadAsync(projectRoot); | |
62c4de22RedMickey6 years ago | 150 | } |
efb436fcRedMickey5 years ago | 151 | |
0d77292aJiglioNero4 years ago | 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, | |
65e55367RedMickey4 years ago | 158 | }); |
0d77292aJiglioNero4 years ago | 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 | } | |
efb436fcRedMickey5 years ago | 168 | } |