{"version":3,"names":["getLatestFile","packageNameWithSuffix","file","execSync","toString","trim","e","execSyncWithLog","command","logger","debug","downloadProfile","ctx","dstPath","filename","sourcemapPath","raw","shouldGenerateSourcemap","port","appId","appIdSuffix","androidProject","getAndroidProject","packageName","filter","Boolean","join","CLIError","info","root","success","osTmpDir","os","tmpdir","tempFilePath","path","generateSourcemap","findSourcemap","warn","events","transformer","transformedFilePath","basename","fs","writeFileSync","JSON","stringify","undefined"],"sources":["../../src/profileHermes/downloadProfile.ts"],"sourcesContent":["import {Config} from '@react-native-community/cli-types';\nimport {execSync} from 'child_process';\nimport {logger, CLIError} from '@react-native-community/cli-tools';\nimport fs from 'fs';\nimport path from 'path';\nimport os from 'os';\nimport transformer from 'hermes-profile-transformer';\nimport {findSourcemap, generateSourcemap} from './sourcemapUtils';\nimport {getAndroidProject} from '@react-native-community/cli-platform-android';\n/**\n * Get the last modified hermes profile\n * @param packageNameWithSuffix\n */\nfunction getLatestFile(packageNameWithSuffix: string): string {\n try {\n const file = execSync(`adb shell run-as ${packageNameWithSuffix} ls cache/ -tp | grep -v /$ | grep -E '.cpuprofile' | head -1\n `);\n return file.toString().trim();\n } catch (e) {\n throw e;\n }\n}\n\nfunction execSyncWithLog(command: string) {\n logger.debug(`${command}`);\n return execSync(command);\n}\n\n/**\n * Pull and convert a Hermes tracing profile to Chrome tracing profile\n * @param ctx\n * @param dstPath\n * @param fileName\n * @param sourceMapPath\n * @param raw\n * @param generateSourceMap\n * @param appId\n * @param appIdSuffix\n */\nexport async function downloadProfile(\n ctx: Config,\n dstPath: string,\n filename?: string,\n sourcemapPath?: string,\n raw?: boolean,\n shouldGenerateSourcemap?: boolean,\n port?: string,\n appId?: string,\n appIdSuffix?: string,\n) {\n try {\n const androidProject = getAndroidProject(ctx);\n const packageNameWithSuffix = [\n appId || androidProject.packageName,\n appIdSuffix,\n ]\n .filter(Boolean)\n .join('.');\n\n // If file name is not specified, pull the latest file from device\n const file = filename || getLatestFile(packageNameWithSuffix);\n if (!file) {\n throw new CLIError(\n 'There is no file in the cache/ directory. Did you record a profile from the developer menu?',\n );\n }\n\n logger.info(`File to be pulled: ${file}`);\n\n // If destination path is not specified, pull to the current directory\n dstPath = dstPath || ctx.root;\n\n logger.debug('Internal commands run to pull the file:');\n\n // If --raw, pull the hermes profile to dstPath\n if (raw) {\n execSyncWithLog(\n `adb shell run-as ${packageNameWithSuffix} cat cache/${file} > ${dstPath}/${file}`,\n );\n logger.success(`Successfully pulled the file to ${dstPath}/${file}`);\n }\n\n // Else: transform the profile to Chrome format and pull it to dstPath\n else {\n const osTmpDir = os.tmpdir();\n const tempFilePath = path.join(osTmpDir, file);\n\n execSyncWithLog(\n `adb shell run-as ${packageNameWithSuffix} cat cache/${file} > ${tempFilePath}`,\n );\n // If path to source map is not given\n if (!sourcemapPath) {\n // Get or generate the source map\n if (shouldGenerateSourcemap) {\n sourcemapPath = await generateSourcemap(port);\n } else {\n sourcemapPath = await findSourcemap(ctx, port);\n }\n\n // Run without source map\n if (!sourcemapPath) {\n logger.warn(\n 'Cannot find source maps, running the transformer without it',\n );\n logger.info(\n 'Instructions on how to get source maps: set `bundleInDebug: true` in your app/build.gradle file, inside the `project.ext.react` map.',\n );\n }\n }\n\n // Run transformer tool to convert from Hermes to Chrome format\n const events = await transformer(\n tempFilePath,\n sourcemapPath,\n 'index.bundle',\n );\n\n const transformedFilePath = `${dstPath}/${path.basename(\n file,\n '.cpuprofile',\n )}-converted.json`;\n fs.writeFileSync(\n transformedFilePath,\n JSON.stringify(events, undefined, 4),\n 'utf-8',\n );\n logger.success(\n `Successfully converted to Chrome tracing format and pulled the file to ${transformedFilePath}`,\n );\n }\n } catch (e) {\n throw e;\n }\n}\n"],"mappings":";;;;;;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAC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