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