protected async attachRequest()

in src/debugger/rnDebugSession.ts [88:206]


    protected async attachRequest(
        response: DebugProtocol.AttachResponse,
        attachArgs: IAttachRequestArgs,
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        request?: DebugProtocol.Request,
    ): Promise<void> {
        let extProps = {
            platform: {
                value: attachArgs.platform,
                isPii: false,
            },
        };

        this.previousAttachArgs = attachArgs;

        return new Promise<void>(async (resolve, reject) => {
            try {
                await this.initializeSettings(attachArgs);
                logger.log("Attaching to the application");
                logger.verbose(
                    `Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`,
                );

                const versions = await ProjectVersionHelper.getReactNativeVersions(
                    this.projectRootPath,
                    ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs),
                );
                extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
                    attachArgs,
                    versions,
                    extProps,
                );

                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                await TelemetryHelper.generate("attach", extProps, async generator => {
                    attachArgs.port =
                        attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);

                    const cdpProxy = this.appLauncher.getRnCdpProxy();
                    await cdpProxy.stopServer();
                    await cdpProxy.initializeServer(
                        new RnCDPMessageHandler(),
                        this.cdpProxyLogLevel,
                        this.cancellationTokenSource.token,
                    );

                    if (attachArgs.request === "attach") {
                        await this.preparePackagerBeforeAttach(attachArgs, versions);
                    }

                    logger.log(
                        localize("StartingDebuggerAppWorker", "Starting debugger app worker."),
                    );

                    const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
                    // Create folder if not exist to avoid problems if
                    // RN project root is not a ${workspaceFolder}
                    mkdirp.sync(sourcesStoragePath);

                    // If launch is invoked first time, appWorker is undefined, so create it here
                    this.appWorker = new MultipleLifetimesAppWorker(
                        attachArgs,
                        sourcesStoragePath,
                        this.projectRootPath,
                        this.cancellationTokenSource.token,
                        undefined,
                    );
                    this.appLauncher.setAppWorker(this.appWorker);

                    this.appWorker.on("connected", (port: number) => {
                        if (this.cancellationTokenSource.token.isCancellationRequested) {
                            return this.appWorker?.stop();
                        }

                        logger.log(
                            localize(
                                "DebuggerWorkerLoadedRuntimeOnPort",
                                "Debugger worker loaded runtime on port {0}",
                                port,
                            ),
                        );

                        cdpProxy.setApplicationTargetPort(port);

                        if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
                            return;
                        }

                        if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) {
                            this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending;
                            this.establishDebugSession(attachArgs, resolve);
                        } else if (
                            this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed
                        ) {
                            if (this.nodeSession) {
                                this.debugSessionStatus = DebugSessionStatus.ConnectionPending;
                                void this.nodeSession.customRequest(this.terminateCommand);
                            }
                        }
                    });

                    if (this.cancellationTokenSource.token.isCancellationRequested) {
                        return this.appWorker.stop();
                    }
                    return await this.appWorker.start();
                });
            } catch (error) {
                reject(error);
            }
        }).catch(err =>
            this.showError(
                ErrorHelper.getInternalError(
                    InternalErrorCode.CouldNotAttachToDebugger,
                    err.message || err,
                ),
                response,
            ),
        );
    }