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