microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/xdlInterface.ts
135lines · 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(path.dirname(findFileInFolderHierarchy(__dirname, "package.json") || __dirname), logger); |
| 41 | xdlPackage = commandExecutor.spawnWithProgress(HostPlatform.getNpmCliCommand("npm"), |
| 42 | ["install", ...EXPO_DEPS, "--verbose", "--no-save"], |
| 43 | { verbosity: CommandVerbosity.PROGRESS }) |
| 44 | .then((): typeof XDLPackage => { |
| 45 | return customRequire(XDL_PACKAGE); |
| 46 | }); |
| 47 | return xdlPackage; |
| 48 | } |
| 49 | |
| 50 | export type IUser = XDLPackage.IUser; |
| 51 | |
| 52 | export function configReactNativeVersionWargnings(): Promise<void> { |
| 53 | return getPackage() |
| 54 | .then((xdl) => { |
| 55 | xdl.Config.validation.reactNativeVersionWarnings = false; |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | export function attachLoggerStream(rootPath: string, options?: XDLPackage.IBunyanStream | any): Promise<void> { |
| 60 | return getPackage() |
| 61 | .then((xdl) => |
| 62 | xdl.ProjectUtils.attachLoggerStream(rootPath, options)); |
| 63 | } |
| 64 | |
| 65 | export function supportedVersions(): Promise<string[]> { |
| 66 | return getPackage() |
| 67 | .then((xdl) => |
| 68 | xdl.Versions.facebookReactNativeVersionsAsync()); |
| 69 | } |
| 70 | |
| 71 | export function currentUser(): Promise<XDLPackage.IUser> { |
| 72 | return getPackage() |
| 73 | .then((xdl) => |
| 74 | xdl.User ? xdl.User.getCurrentUserAsync() : xdl.UserManager.getCurrentUserAsync()); |
| 75 | } |
| 76 | |
| 77 | export function login(username: string, password: string): Promise<XDLPackage.IUser> { |
| 78 | return getPackage() |
| 79 | .then((xdl) => |
| 80 | xdl.User ? xdl.User.loginAsync("user-pass", { username: username, password: password }) : xdl.UserManager.loginAsync("user-pass", { username: username, password: password })); |
| 81 | } |
| 82 | |
| 83 | export function mapVersion(reactNativeVersion: string): Promise<string> { |
| 84 | return getPackage() |
| 85 | .then((xdl) => |
| 86 | xdl.Versions.facebookReactNativeVersionToExpoVersionAsync(reactNativeVersion)); |
| 87 | } |
| 88 | |
| 89 | export function publish(projectRoot: string, options?: XDLPackage.IPublishOptions): Promise<XDLPackage.IPublishResponse> { |
| 90 | return getPackage() |
| 91 | .then((xdl) => |
| 92 | xdl.Project.publishAsync(projectRoot, options)); |
| 93 | } |
| 94 | |
| 95 | export function setOptions(projectRoot: string, options?: XDLPackage.IOptions): Promise<void> { |
| 96 | return getPackage() |
| 97 | .then((xdl) => |
| 98 | xdl.Project.setOptionsAsync(projectRoot, options)); |
| 99 | } |
| 100 | |
| 101 | export function startExponentServer(projectRoot: string): Promise<void> { |
| 102 | return getPackage() |
| 103 | .then((xdl) => |
| 104 | xdl.Project.startExpoServerAsync(projectRoot)); |
| 105 | } |
| 106 | |
| 107 | export function startTunnels(projectRoot: string): Promise<void> { |
| 108 | return getPackage() |
| 109 | .then((xdl) => |
| 110 | xdl.Project.startTunnelsAsync(projectRoot)); |
| 111 | } |
| 112 | |
| 113 | export function getUrl(projectRoot: string, options?: XDLPackage.IUrlOptions): Promise<string> { |
| 114 | return getPackage() |
| 115 | .then((xdl) => |
| 116 | xdl.UrlUtils.constructManifestUrlAsync(projectRoot, options)); |
| 117 | } |
| 118 | |
| 119 | export function stopAll(projectRoot: string): Promise<void> { |
| 120 | return getPackage() |
| 121 | .then((xdl) => |
| 122 | xdl.Project.stopAsync(projectRoot)); |
| 123 | } |
| 124 | |
| 125 | export function startAdbReverse(projectRoot: string): Promise<boolean> { |
| 126 | return getPackage() |
| 127 | .then((xdl) => |
| 128 | xdl.Android.startAdbReverseAsync(projectRoot)); |
| 129 | } |
| 130 | |
| 131 | export function stopAdbReverse(projectRoot: string): Promise<void> { |
| 132 | return getPackage() |
| 133 | .then((xdl) => |
| 134 | xdl.Android.stopAdbReverseAsync(projectRoot)); |
| 135 | } |
| 136 | |