microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
indexed-sourcemap-null-section-issue

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/exponent/xdlInterface.ts

168lines · modeblame

7059d307Patricio Beltran9 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
65e55367RedMickey4 years ago4import { join as pathJoin } from "path";
7059d307Patricio Beltran9 years ago5import * as XDLPackage from "xdl";
242759feJiglioNero5 years ago6import * as MetroConfigPackage from "metro-config";
716f31d0JiglioNero5 years ago7import { PackageLoader, PackageConfig } from "../../common/packageLoader";
65e55367RedMickey4 years ago8import { removeModuleFromRequireCacheByName } from "../../common/utils";
716f31d0JiglioNero5 years ago9import { SettingsHelper } from "../settingsHelper";
0a68f8dbArtem Egorov8 years ago10
780e0237Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago11// Defensive helper: test environments or partial activation may load this module
12// before VS Code settings are fully available. Directly calling
13// SettingsHelper.getExpoDependencyVersion could throw if SettingsHelper is
14// undefined or method missing. We guard to return undefined (unversioned install)
15// instead of crashing the whole test run.
16function safeExpoVersion(packageName: string): string | undefined {
17try {
18if (
19SettingsHelper &&
20typeof (SettingsHelper as any).getExpoDependencyVersion === "function"
21) {
22return (SettingsHelper as any).getExpoDependencyVersion(packageName);
23}
24} catch {
25// Swallow any unexpected errors; unversioned dependency is acceptable.
26}
27return undefined;
28}
29
00a26245RedMickey5 years ago30const XDL_PACKAGE = "xdl";
242759feJiglioNero5 years ago31const METRO_CONFIG_PACKAGE = "@expo/metro-config";
32
780e0237Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago33const xdlPackageConfig = new PackageConfig(XDL_PACKAGE, safeExpoVersion(XDL_PACKAGE));
716f31d0JiglioNero5 years ago34const metroConfigPackageConfig = new PackageConfig(
35METRO_CONFIG_PACKAGE,
780e0237Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago36safeExpoVersion(METRO_CONFIG_PACKAGE),
716f31d0JiglioNero5 years ago37);
38
39const ngrokPackageConfig = new PackageConfig(
40xdlPackageConfig.getPackageName(),
41xdlPackageConfig.getVersion(),
42"build/start/resolveNgrok",
43);
44
45// There is the problem with '--no-save' flag for 'npm install' command for npm v6.
46// Installing npm dependencies with the `--no-save` flag will remove
47// other dependencies that were installed previously in the same manner (https://github.com/npm/cli/issues/1460).
48// So we should workaround it passing all packages for install to only one npm install command
49const EXPO_DEPS: PackageConfig[] = [xdlPackageConfig, metroConfigPackageConfig];
242759feJiglioNero5 years ago50
09f6024fHeniker4 years ago51export const getXDLPackage: () => Promise<typeof XDLPackage> =
52PackageLoader.getInstance().generateGetPackageFunction<typeof XDLPackage>(
53xdlPackageConfig,
54...EXPO_DEPS,
55);
56export const getMetroConfigPackage: () => Promise<typeof MetroConfigPackage> =
57PackageLoader.getInstance().generateGetPackageFunction<typeof MetroConfigPackage>(
58metroConfigPackageConfig,
59...EXPO_DEPS,
60);
61export const getNgrokResolver: () => Promise<XDLPackage.ResolveNgrok> =
62PackageLoader.getInstance().generateGetPackageFunction<XDLPackage.ResolveNgrok>(
63ngrokPackageConfig,
64...EXPO_DEPS,
65);
7059d307Patricio Beltran9 years ago66
67export type IUser = XDLPackage.IUser;
68
0d77292aJiglioNero4 years ago69export async function configReactNativeVersionWarnings(): Promise<void> {
50a7f6ecbenjaminbi2 years ago70const xdlPackage = await getXDLPackage();
71if (xdlPackage.Config.validation !== undefined) {
72xdlPackage.Config.validation.reactNativeVersionWarnings = false;
73}
7059d307Patricio Beltran9 years ago74}
75
0d77292aJiglioNero4 years ago76export async function attachLoggerStream(
34472878RedMickey5 years ago77rootPath: string,
78options?: XDLPackage.IBunyanStream | any,
79): Promise<void> {
0d77292aJiglioNero4 years ago80(await getXDLPackage()).ProjectUtils.attachLoggerStream(rootPath, options);
7059d307Patricio Beltran9 years ago81}
82
0d77292aJiglioNero4 years ago83export async function currentUser(): Promise<XDLPackage.IUser> {
84const xdl = await getXDLPackage();
85return await (xdl.User
86? xdl.User.getCurrentUserAsync()
87: xdl.UserManager.getCurrentUserAsync());
7059d307Patricio Beltran9 years ago88}
89
0d77292aJiglioNero4 years ago90export async function login(username: string, password: string): Promise<XDLPackage.IUser> {
91const xdl = await getXDLPackage();
92return await (xdl.User
09f6024fHeniker4 years ago93? xdl.User.loginAsync("user-pass", { username, password })
0d77292aJiglioNero4 years ago94: xdl.UserManager.loginAsync("user-pass", {
09f6024fHeniker4 years ago95username,
96password,
0d77292aJiglioNero4 years ago97}));
7059d307Patricio Beltran9 years ago98}
99
0d77292aJiglioNero4 years ago100export async function getExpoSdkVersions(): Promise<XDLPackage.SDKVersions> {
101return (await getXDLPackage()).Versions.sdkVersionsAsync();
8f50947fRedMickey5 years ago102}
103
0d77292aJiglioNero4 years ago104export async function getReleasedExpoSdkVersions(): Promise<XDLPackage.SDKVersions> {
105return (await getXDLPackage()).Versions.releasedSdkVersionsAsync();
7059d307Patricio Beltran9 years ago106}
107
0d77292aJiglioNero4 years ago108export async function publish(
34472878RedMickey5 years ago109projectRoot: string,
110options?: XDLPackage.IPublishOptions,
111): Promise<XDLPackage.IPublishResponse> {
0d77292aJiglioNero4 years ago112return (await getXDLPackage()).Project.publishAsync(projectRoot, options);
7059d307Patricio Beltran9 years ago113}
114
ec230406RedMickey4 years ago115export async function setOptions(projectRoot: string, options: XDLPackage.IOptions): Promise<void> {
116await (await getXDLPackage()).ProjectSettings.setPackagerInfoAsync(projectRoot, options);
7059d307Patricio Beltran9 years ago117}
118
0d77292aJiglioNero4 years ago119export async function startExponentServer(projectRoot: string): Promise<void> {
120await (await getXDLPackage()).Project.startExpoServerAsync(projectRoot);
7059d307Patricio Beltran9 years ago121}
122
0d77292aJiglioNero4 years ago123export async function startTunnels(projectRoot: string): Promise<void> {
124await (await getXDLPackage()).Project.startTunnelsAsync(projectRoot);
7059d307Patricio Beltran9 years ago125}
126
0d77292aJiglioNero4 years ago127export async function getUrl(
128projectRoot: string,
129options?: XDLPackage.IUrlOptions,
130): Promise<string> {
131return (await getXDLPackage()).UrlUtils.constructManifestUrlAsync(projectRoot, options);
7059d307Patricio Beltran9 years ago132}
133
0d77292aJiglioNero4 years ago134export async function stopAll(projectRoot: string): Promise<void> {
135await (await getXDLPackage()).Project.stopAsync(projectRoot);
7059d307Patricio Beltran9 years ago136}
62c4de22RedMickey6 years ago137
0d77292aJiglioNero4 years ago138export async function startAdbReverse(projectRoot: string): Promise<boolean> {
139return (await getXDLPackage()).Android.startAdbReverseAsync(projectRoot);
62c4de22RedMickey6 years ago140}
141
0d77292aJiglioNero4 years ago142export async function stopAdbReverse(projectRoot: string): Promise<void> {
143await (await getXDLPackage()).Android.stopAdbReverseAsync(projectRoot);
242759feJiglioNero5 years ago144}
145
0d77292aJiglioNero4 years ago146export async function getMetroConfig(
147projectRoot: string,
148): Promise<MetroConfigPackage.IMetroConfig> {
149return (await getMetroConfigPackage()).loadAsync(projectRoot);
62c4de22RedMickey6 years ago150}
efb436fcRedMickey5 years ago151
0d77292aJiglioNero4 years ago152export async function isNgrokInstalled(projectRoot: string): Promise<boolean> {
153const ngrokResolver = await getNgrokResolver();
154try {
155const ngrok = await ngrokResolver.resolveNgrokAsync(projectRoot, {
156shouldPrompt: false,
157autoInstall: false,
65e55367RedMickey4 years ago158});
0d77292aJiglioNero4 years ago159return !!ngrok;
160} catch (err) {
161// If unsupported version of the "@expo/ngrok" package was detected, we need to update the package.
162// Since the "require" method used to parse the "ngrok⁄package.json" file in the "xdl" package caches
163// all processed modules, we have to remove this file from cache to be able to require a new version
164// of that file after the update of the "@expo/ngrok" package
165removeModuleFromRequireCacheByName(pathJoin("ngrok", "package.json"));
166throw err;
167}
efb436fcRedMickey5 years ago168}