microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
540a7341e493b10d83a6a442c2b0ea616336b6b4

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/android/androidPlatform.ts

76lines · 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
4import * as Q from "q";
5import {IAppPlatform} from "../platformResolver";
6import {IRunOptions} from "../../common/launchArgs";
7import {CommandExecutor} from "../../common/commandExecutor";
8import {Package} from "../../common/node/package";
9import {PackageNameResolver} from "../../common/android/packageNameResolver";
10import {DeviceHelper, IDevice} from "../../common/android/deviceHelper";
11
12/**
13 * Android specific platform implementation for debugging RN applications.
14 */
15export class AndroidPlatform implements IAppPlatform {
16
17 private debugTarget: string;
18 private packageName: string;
19 private deviceHelper: DeviceHelper;
20
21 constructor() {
22 this.deviceHelper = new DeviceHelper();
23 }
24
25 public runApp(runOptions: IRunOptions): Q.Promise<void> {
26 let pkg = new Package(runOptions.projectRoot);
27 let cexec = new CommandExecutor(runOptions.projectRoot);
28 return cexec.spawnAndWaitReactCommand("run-android")
29 .then(() => pkg.name())
30 .then(appName => new PackageNameResolver(appName).resolvePackageName(runOptions.projectRoot))
31 .then(packageName => {
32 this.packageName = packageName;
33 return this.deviceHelper.getConnectedDevices()
34 .then((devices: IDevice[]) => {
35 if (devices.length > 1) {
36 /* more than one device or emulator */
37 this.debugTarget = this.getTargetEmulator(runOptions, devices);
38 if (this.debugTarget) {
39 /* Launching is needed only if we have more than one device active */
40 return this.deviceHelper.launchApp(runOptions.projectRoot, packageName, this.debugTarget);
41 }
42 }
43 });
44 });
45 }
46
47 public enableJSDebuggingMode(runOptions: IRunOptions): Q.Promise<void> {
48 return this.deviceHelper.reloadAppInDebugMode(runOptions.projectRoot, this.packageName, this.debugTarget);
49 }
50
51 /**
52 * Returns the target emulator, using the following logic:
53 * * If an emulator is specified and it is connected, use that one.
54 * * Otherwise, use the first one in the list.
55 */
56 private getTargetEmulator(runOptions: IRunOptions, devices: IDevice[]): string {
57 let activeFilterFunction = (device: IDevice) => {
58 return device.isOnline;
59 };
60
61 let targetFilterFunction = (device: IDevice) => {
62 return device.id === runOptions.target && activeFilterFunction(device);
63 };
64
65 if (runOptions && runOptions.target && devices) {
66 /* check if the specified target is active */
67 if (devices.some(targetFilterFunction)) {
68 return runOptions.target;
69 }
70 }
71
72 /* return the first active device in the list */
73 let activeDevices = devices && devices.filter(activeFilterFunction);
74 return activeDevices && activeDevices[0] && activeDevices[0].id;
75 }
76}