downloadProfile.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.downloadProfile = downloadProfile;
  6. function _child_process() {
  7. const data = require("child_process");
  8. _child_process = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _cliTools() {
  14. const data = require("@react-native-community/cli-tools");
  15. _cliTools = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _fs() {
  21. const data = _interopRequireDefault(require("fs"));
  22. _fs = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _path() {
  28. const data = _interopRequireDefault(require("path"));
  29. _path = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _os() {
  35. const data = _interopRequireDefault(require("os"));
  36. _os = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _hermesProfileTransformer() {
  42. const data = _interopRequireDefault(require("hermes-profile-transformer"));
  43. _hermesProfileTransformer = function () {
  44. return data;
  45. };
  46. return data;
  47. }
  48. var _sourcemapUtils = require("./sourcemapUtils");
  49. function _cliPlatformAndroid() {
  50. const data = require("@react-native-community/cli-platform-android");
  51. _cliPlatformAndroid = function () {
  52. return data;
  53. };
  54. return data;
  55. }
  56. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  57. /**
  58. * Get the last modified hermes profile
  59. * @param packageName
  60. */
  61. function getLatestFile(packageName) {
  62. try {
  63. const file = (0, _child_process().execSync)(`adb shell run-as ${packageName} ls cache/ -tp | grep -v /$ | egrep '\.cpuprofile' | head -1
  64. `);
  65. return file.toString().trim();
  66. } catch (e) {
  67. throw new Error(e);
  68. }
  69. }
  70. function execSyncWithLog(command) {
  71. _cliTools().logger.debug(`${command}`);
  72. return (0, _child_process().execSync)(command);
  73. }
  74. /**
  75. * Pull and convert a Hermes tracing profile to Chrome tracing profile
  76. * @param ctx
  77. * @param dstPath
  78. * @param fileName
  79. * @param sourceMapPath
  80. * @param raw
  81. * @param generateSourceMap
  82. */
  83. async function downloadProfile(ctx, dstPath, filename, sourcemapPath, raw, shouldGenerateSourcemap, port) {
  84. try {
  85. const androidProject = (0, _cliPlatformAndroid().getAndroidProject)(ctx);
  86. const packageName = (0, _cliPlatformAndroid().getPackageName)(androidProject); // If file name is not specified, pull the latest file from device
  87. const file = filename || getLatestFile(packageName);
  88. if (!file) {
  89. throw new (_cliTools().CLIError)('There is no file in the cache/ directory. Did you record a profile from the developer menu?');
  90. }
  91. _cliTools().logger.info(`File to be pulled: ${file}`); // If destination path is not specified, pull to the current directory
  92. dstPath = dstPath || ctx.root;
  93. _cliTools().logger.debug('Internal commands run to pull the file:'); // Copy the file from device's data to sdcard, then pull the file to a temp directory
  94. execSyncWithLog(`adb shell run-as ${packageName} cp cache/${file} /sdcard`); // If --raw, pull the hermes profile to dstPath
  95. if (raw) {
  96. execSyncWithLog(`adb pull /sdcard/${file} ${dstPath}`);
  97. _cliTools().logger.success(`Successfully pulled the file to ${dstPath}/${file}`);
  98. } // Else: transform the profile to Chrome format and pull it to dstPath
  99. else {
  100. const osTmpDir = _os().default.tmpdir();
  101. const tempFilePath = _path().default.join(osTmpDir, file);
  102. execSyncWithLog(`adb pull /sdcard/${file} ${tempFilePath}`); // If path to source map is not given
  103. if (!sourcemapPath) {
  104. // Get or generate the source map
  105. if (shouldGenerateSourcemap) {
  106. sourcemapPath = await (0, _sourcemapUtils.generateSourcemap)(port);
  107. } else {
  108. sourcemapPath = await (0, _sourcemapUtils.findSourcemap)(ctx, port);
  109. } // Run without source map
  110. if (!sourcemapPath) {
  111. _cliTools().logger.warn('Cannot find source maps, running the transformer without it');
  112. _cliTools().logger.info('Instructions on how to get source maps: set `bundleInDebug: true` in your app/build.gradle file, inside the `project.ext.react` map.');
  113. }
  114. } // Run transformer tool to convert from Hermes to Chrome format
  115. const events = await (0, _hermesProfileTransformer().default)(tempFilePath, sourcemapPath, 'index.bundle');
  116. const transformedFilePath = `${dstPath}/${_path().default.basename(file, '.cpuprofile')}-converted.json`;
  117. _fs().default.writeFileSync(transformedFilePath, JSON.stringify(events, undefined, 4), 'utf-8');
  118. _cliTools().logger.success(`Successfully converted to Chrome tracing format and pulled the file to ${transformedFilePath}`);
  119. }
  120. } catch (e) {
  121. throw e;
  122. }
  123. }
  124. //# sourceMappingURL=downloadProfile.js.map