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