import { Config } from '@react-native-community/cli-types' ; import { execSync } from 'child_process' ; import { logger , CLIError } from '@react-native-community/cli-tools' ; import fs from 'fs' ; import path from 'path' ; import os from 'os' ; import transformer from 'hermes-profile-transformer' ; import { findSourcemap , generateSourcemap } from './sourcemapUtils' ; import { getAndroidProject , getPackageName , } from '@react-native-community/cli-platform-android' ; param packageName function getLatestFile ( packageName ) { try { const file = execSync ( `adb shell run-as ${ packageName } ls cache/ -tp | grep -v /$ | grep -E '.cpuprofile' | head -1
        ` ) ; return file . toString ( ) . trim ( ) ; } catch ( e ) { throw new Error ( e ) ; } } function execSyncWithLog ( command ) { logger . debug ( `${ command }` ) ; return execSync ( command ) ; } param ctx param dstPath param fileName param sourceMapPath param raw param generateSourceMap export async function downloadProfile ( ctx , dstPath , filename ? , sourcemapPath ? , raw ? , shouldGenerateSourcemap ? , port ? ) { try { const androidProject = getAndroidProject ( ctx ) ; const packageName = getPackageName ( androidProject ) ; const file = filename || getLatestFile ( packageName ) ; if ( ! file ) { throw new CLIError ( 'There is no file in the cache/ directory. Did you record a profile from the developer menu?' ) ; } logger . info ( `File to be pulled: ${ file }` ) ; dstPath = dstPath || ctx . root ; logger . debug ( 'Internal commands run to pull the file:' ) ; if ( raw ) { execSyncWithLog ( `adb shell run-as ${ packageName } cat cache/${ file } > ${ dstPath }/${ file }` ) ; logger . success ( `Successfully pulled the file to ${ dstPath }/${ file }` ) ; } else { const osTmpDir = os . tmpdir ( ) ; const tempFilePath = path . join ( osTmpDir , file ) ; execSyncWithLog ( `adb shell run-as ${ packageName } cat cache/${ file } > ${ tempFilePath }` ) ; if ( ! sourcemapPath ) { if ( shouldGenerateSourcemap ) { sourcemapPath = await generateSourcemap ( port ) ; } else { sourcemapPath = await findSourcemap ( ctx , port ) ; } if ( ! sourcemapPath ) { logger . warn ( 'Cannot find source maps, running the transformer without it' ) ; logger . info ( 'Instructions on how to get source maps: set `bundleInDebug: true` in your app/build.gradle file, inside the `project.ext.react` map.' ) ; } } const events = await transformer ( tempFilePath , sourcemapPath , 'index.bundle' ) ; const transformedFilePath = `${ dstPath }/${ path . basename ( file , '.cpuprofile' ) }-converted.json` ; fs . writeFileSync ( transformedFilePath , JSON . stringify ( events , undefined , 4 ) , 'utf-8' ) ; logger . success ( `Successfully converted to Chrome tracing format and pulled the file to ${ transformedFilePath }` ) ; } } catch ( e ) { throw e ; } }