microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/xdlInterface.ts
121lines · modeblame
7059d307Patricio Beltran9 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
0a68f8dbArtem Egorov8 years ago | 4 | import {CommandExecutor, CommandVerbosity} from "../../common/commandExecutor"; |
| 5 | import {HostPlatform} from "../../common/hostPlatform"; | |
| 6 | import {OutputChannelLogger} from "../log/OutputChannelLogger"; | |
7059d307Patricio Beltran9 years ago | 7 | |
| 8 | import * as XDLPackage from "xdl"; | |
0d827d9bJimmy Thomson9 years ago | 9 | import * as path from "path"; |
7059d307Patricio Beltran9 years ago | 10 | import * as Q from "q"; |
| 11 | | |
0a68f8dbArtem Egorov8 years ago | 12 | const logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
| 13 | | |
94cd5149Artem Egorov8 years ago | 14 | const EXPO_DEPS: string[] = [ |
| 15 | "xdl", | |
| 16 | "@expo/ngrok", // devDependencies for xdl | |
| 17 | ]; | |
| 18 | | |
0d827d9bJimmy Thomson9 years ago | 19 | let xdlPackage: Q.Promise<typeof XDLPackage>; |
7059d307Patricio Beltran9 years ago | 20 | |
| 21 | function getPackage(): Q.Promise<typeof XDLPackage> { | |
| 22 | if (xdlPackage) { | |
0d827d9bJimmy Thomson9 years ago | 23 | return xdlPackage; |
7059d307Patricio Beltran9 years ago | 24 | } |
| 25 | // Don't do the require if we don't actually need it | |
| 26 | try { | |
0a68f8dbArtem Egorov8 years ago | 27 | logger.debug("Getting exponent dependecy."); |
0d827d9bJimmy Thomson9 years ago | 28 | const xdl = require("xdl"); |
| 29 | xdlPackage = Q(xdl); | |
| 30 | return xdlPackage; | |
a57e740bPatricio Beltran9 years ago | 31 | } catch (e) { |
b0af599cJimmy Thomson9 years ago | 32 | if (e.code === "MODULE_NOT_FOUND") { |
0a68f8dbArtem Egorov8 years ago | 33 | logger.debug("Dependency not present. Installing it..."); |
b0af599cJimmy Thomson9 years ago | 34 | } else { |
| 35 | throw e; | |
| 36 | } | |
7059d307Patricio Beltran9 years ago | 37 | } |
0a68f8dbArtem Egorov8 years ago | 38 | let commandExecutor = new CommandExecutor(path.dirname(require.resolve("../../../")), logger); |
0d827d9bJimmy Thomson9 years ago | 39 | xdlPackage = commandExecutor.spawnWithProgress(HostPlatform.getNpmCliCommand("npm"), |
833e37c7Vladimir Kotikov8 years ago | 40 | ["install", ...EXPO_DEPS, "--verbose", "--no-save"], |
0a68f8dbArtem Egorov8 years ago | 41 | { verbosity: CommandVerbosity.PROGRESS }) |
0d827d9bJimmy Thomson9 years ago | 42 | .then((): typeof XDLPackage => { |
| 43 | return require("xdl"); | |
7059d307Patricio Beltran9 years ago | 44 | }); |
0d827d9bJimmy Thomson9 years ago | 45 | return xdlPackage; |
7059d307Patricio Beltran9 years ago | 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 | | |
94cd5149Artem Egorov8 years ago | 57 | export function attachLoggerStream(rootPath: string, options?: XDLPackage.IBunyanStream | any): Q.Promise<void> { |
7059d307Patricio Beltran9 years ago | 58 | return getPackage() |
| 59 | .then((xdl) => | |
| 60 | xdl.ProjectUtils.attachLoggerStream(rootPath, options)); | |
| 61 | } | |
| 62 | | |
94cd5149Artem Egorov8 years ago | 63 | export function supportedVersions(): Q.Promise<string[]> { |
7059d307Patricio Beltran9 years ago | 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) => | |
4d1fa3fdNikita Matrosov9 years ago | 78 | xdl.User.loginAsync("user-pass", { username: username, password: password })); |
7059d307Patricio Beltran9 years ago | 79 | } |
| 80 | | |
| 81 | export function mapVersion(reactNativeVersion: string): Q.Promise<string> { | |
| 82 | return getPackage() | |
| 83 | .then((xdl) => | |
4d1fa3fdNikita Matrosov9 years ago | 84 | xdl.Versions.facebookReactNativeVersionToExpoVersionAsync(reactNativeVersion)); |
7059d307Patricio Beltran9 years ago | 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) => | |
4d1fa3fdNikita Matrosov9 years ago | 102 | xdl.Project.startExpoServerAsync(projectRoot)); |
7059d307Patricio Beltran9 years ago | 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 | } |