public async resolveMobileTarget()

in src/extension/generalMobilePlatform.ts [27:116]


    public async resolveMobileTarget(targetString: string): Promise<MobileTarget | undefined> {
        let collectTargetsCalled = false;

        let isAnyTarget = false;
        let isVirtualTarget: boolean;
        if (targetString.toLowerCase() === TargetType.Simulator) {
            isAnyTarget = true;
            isVirtualTarget = true;
        } else if (targetString.toLowerCase() === TargetType.Device) {
            isAnyTarget = true;
            isVirtualTarget = false;
        } else {
            await this.targetManager.collectTargets();
            collectTargetsCalled = true;
            isVirtualTarget = await this.targetManager.isVirtualTarget(targetString);
        }

        if (!collectTargetsCalled) {
            await this.targetManager.collectTargets(
                isVirtualTarget ? TargetType.Simulator : TargetType.Device,
            );
        }

        const cleanupTargetModifications = () => {
            // Use 'simulator' or 'device' in case we need to specify target
            this.runOptions.target = isVirtualTarget ? TargetType.Simulator : TargetType.Device;
            this.runArguments = this.getRunArguments();
        };

        try {
            this.target = await this.targetManager.selectAndPrepareTarget(target => {
                const conditionForNotAnyTarget = isAnyTarget
                    ? true
                    : target.name === targetString || target.id === targetString;
                const conditionForVirtualTarget = isVirtualTarget === target.isVirtualTarget;
                return conditionForVirtualTarget && conditionForNotAnyTarget;
            });

            if (!this.target) {
                this.logger.warning(
                    localize(
                        "CouldNotFindAnyDebuggableTarget",
                        "Could not find any debuggable target by specified target: {0}",
                        targetString,
                    ),
                );
                this.logger.warning(
                    localize(
                        "ContinueWithRnCliWorkflow",
                        "Continue using standard RN CLI workflow.",
                    ),
                );
                cleanupTargetModifications();
            } else {
                // For iOS we should pass exact target id,
                // because the “react-native run-ios” command does not check booted devices
                // and just launches the first device
                if (this instanceof IOSPlatform || (await this.needToPassTargetToRunArgs())) {
                    this.addTargetToRunArgs(this.target);
                } else {
                    cleanupTargetModifications();
                }
            }
        } catch (error) {
            if (
                error &&
                error.errorCode &&
                error.errorCode === InternalErrorCode.TargetSelectionError
            ) {
                TelemetryHelper.sendErrorEvent(
                    "TargetSelectionError",
                    ErrorHelper.getInternalError(InternalErrorCode.TargetSelectionError),
                );

                this.logger.warning(error);
                this.logger.warning(
                    localize(
                        "ContinueWithRnCliWorkflow",
                        "Continue using standard RN CLI workflow.",
                    ),
                );

                cleanupTargetModifications();
            } else {
                throw error;
            }
        }

        return this.target;
    }