microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/xdlInterface.ts
128lines · 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 { CommandExecutor, CommandVerbosity } from "../../common/commandExecutor"; |
| 5 | import { HostPlatform } from "../../common/hostPlatform"; |
| 6 | import { OutputChannelLogger } from "../log/OutputChannelLogger"; |
| 7 | |
| 8 | import * as XDLPackage from "xdl"; |
| 9 | import * as path from "path"; |
| 10 | import { findFileInFolderHierarchy } from "../../common/extensionHelper"; |
| 11 | import customRequire from "../../common/customRequire"; |
| 12 | |
| 13 | const logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
| 14 | |
| 15 | const XDL_PACKAGE = "@expo/xdl"; |
| 16 | const EXPO_DEPS: string[] = [ |
| 17 | XDL_PACKAGE, |
| 18 | "@expo/ngrok", // devDependencies for xdl |
| 19 | ]; |
| 20 | |
| 21 | let xdlPackage: Promise<typeof XDLPackage>; |
| 22 | |
| 23 | function getPackage(): Promise<typeof XDLPackage> { |
| 24 | if (xdlPackage) { |
| 25 | return xdlPackage; |
| 26 | } |
| 27 | // Don't do the require if we don't actually need it |
| 28 | try { |
| 29 | logger.debug("Getting exponent dependency."); |
| 30 | const xdl = customRequire(XDL_PACKAGE); |
| 31 | xdlPackage = Promise.resolve(xdl); |
| 32 | return xdlPackage; |
| 33 | } catch (e) { |
| 34 | if (e.code === "MODULE_NOT_FOUND") { |
| 35 | logger.debug("Dependency not present. Installing it..."); |
| 36 | } else { |
| 37 | throw e; |
| 38 | } |
| 39 | } |
| 40 | let commandExecutor = new CommandExecutor( |
| 41 | path.dirname(findFileInFolderHierarchy(__dirname, "package.json") || __dirname), |
| 42 | logger, |
| 43 | ); |
| 44 | xdlPackage = commandExecutor |
| 45 | .spawnWithProgress( |
| 46 | HostPlatform.getNpmCliCommand("npm"), |
| 47 | ["install", ...EXPO_DEPS, "--verbose", "--no-save"], |
| 48 | { verbosity: CommandVerbosity.PROGRESS }, |
| 49 | ) |
| 50 | .then((): typeof XDLPackage => { |
| 51 | return customRequire(XDL_PACKAGE); |
| 52 | }); |
| 53 | return xdlPackage; |
| 54 | } |
| 55 | |
| 56 | export type IUser = XDLPackage.IUser; |
| 57 | |
| 58 | export function configReactNativeVersionWargnings(): Promise<void> { |
| 59 | return getPackage().then(xdl => { |
| 60 | xdl.Config.validation.reactNativeVersionWarnings = false; |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | export function attachLoggerStream( |
| 65 | rootPath: string, |
| 66 | options?: XDLPackage.IBunyanStream | any, |
| 67 | ): Promise<void> { |
| 68 | return getPackage().then(xdl => xdl.ProjectUtils.attachLoggerStream(rootPath, options)); |
| 69 | } |
| 70 | |
| 71 | export function supportedVersions(): Promise<string[]> { |
| 72 | return getPackage().then(xdl => xdl.Versions.facebookReactNativeVersionsAsync()); |
| 73 | } |
| 74 | |
| 75 | export function currentUser(): Promise<XDLPackage.IUser> { |
| 76 | return getPackage().then(xdl => |
| 77 | xdl.User ? xdl.User.getCurrentUserAsync() : xdl.UserManager.getCurrentUserAsync(), |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | export function login(username: string, password: string): Promise<XDLPackage.IUser> { |
| 82 | return getPackage().then(xdl => |
| 83 | xdl.User |
| 84 | ? xdl.User.loginAsync("user-pass", { username: username, password: password }) |
| 85 | : xdl.UserManager.loginAsync("user-pass", { username: username, password: password }), |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | export function mapVersion(reactNativeVersion: string): Promise<string> { |
| 90 | return getPackage().then(xdl => |
| 91 | xdl.Versions.facebookReactNativeVersionToExpoVersionAsync(reactNativeVersion), |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | export function publish( |
| 96 | projectRoot: string, |
| 97 | options?: XDLPackage.IPublishOptions, |
| 98 | ): Promise<XDLPackage.IPublishResponse> { |
| 99 | return getPackage().then(xdl => xdl.Project.publishAsync(projectRoot, options)); |
| 100 | } |
| 101 | |
| 102 | export function setOptions(projectRoot: string, options?: XDLPackage.IOptions): Promise<void> { |
| 103 | return getPackage().then(xdl => xdl.Project.setOptionsAsync(projectRoot, options)); |
| 104 | } |
| 105 | |
| 106 | export function startExponentServer(projectRoot: string): Promise<void> { |
| 107 | return getPackage().then(xdl => xdl.Project.startExpoServerAsync(projectRoot)); |
| 108 | } |
| 109 | |
| 110 | export function startTunnels(projectRoot: string): Promise<void> { |
| 111 | return getPackage().then(xdl => xdl.Project.startTunnelsAsync(projectRoot)); |
| 112 | } |
| 113 | |
| 114 | export function getUrl(projectRoot: string, options?: XDLPackage.IUrlOptions): Promise<string> { |
| 115 | return getPackage().then(xdl => xdl.UrlUtils.constructManifestUrlAsync(projectRoot, options)); |
| 116 | } |
| 117 | |
| 118 | export function stopAll(projectRoot: string): Promise<void> { |
| 119 | return getPackage().then(xdl => xdl.Project.stopAsync(projectRoot)); |
| 120 | } |
| 121 | |
| 122 | export function startAdbReverse(projectRoot: string): Promise<boolean> { |
| 123 | return getPackage().then(xdl => xdl.Android.startAdbReverseAsync(projectRoot)); |
| 124 | } |
| 125 | |
| 126 | export function stopAdbReverse(projectRoot: string): Promise<void> { |
| 127 | return getPackage().then(xdl => xdl.Android.stopAdbReverseAsync(projectRoot)); |
| 128 | } |
| 129 | |