This commit is contained in:
Yamozha
2021-04-02 02:24:13 +03:00
parent c23950b545
commit 7256d79e2c
31493 changed files with 3036630 additions and 0 deletions

View File

@ -0,0 +1,174 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.downloadProfile = downloadProfile;
function _child_process() {
const data = require("child_process");
_child_process = function () {
return data;
};
return data;
}
function _cliTools() {
const data = require("@react-native-community/cli-tools");
_cliTools = function () {
return data;
};
return data;
}
function _fs() {
const data = _interopRequireDefault(require("fs"));
_fs = function () {
return data;
};
return data;
}
function _path() {
const data = _interopRequireDefault(require("path"));
_path = function () {
return data;
};
return data;
}
function _os() {
const data = _interopRequireDefault(require("os"));
_os = function () {
return data;
};
return data;
}
function _hermesProfileTransformer() {
const data = _interopRequireDefault(require("hermes-profile-transformer"));
_hermesProfileTransformer = function () {
return data;
};
return data;
}
var _sourcemapUtils = require("./sourcemapUtils");
function _cliPlatformAndroid() {
const data = require("@react-native-community/cli-platform-android");
_cliPlatformAndroid = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Get the last modified hermes profile
* @param packageName
*/
function getLatestFile(packageName) {
try {
const file = (0, _child_process().execSync)(`adb shell run-as ${packageName} ls cache/ -tp | grep -v /$ | egrep '\.cpuprofile' | head -1
`);
return file.toString().trim();
} catch (e) {
throw new Error(e);
}
}
function execSyncWithLog(command) {
_cliTools().logger.debug(`${command}`);
return (0, _child_process().execSync)(command);
}
/**
* Pull and convert a Hermes tracing profile to Chrome tracing profile
* @param ctx
* @param dstPath
* @param fileName
* @param sourceMapPath
* @param raw
* @param generateSourceMap
*/
async function downloadProfile(ctx, dstPath, filename, sourcemapPath, raw, shouldGenerateSourcemap, port) {
try {
const androidProject = (0, _cliPlatformAndroid().getAndroidProject)(ctx);
const packageName = (0, _cliPlatformAndroid().getPackageName)(androidProject); // If file name is not specified, pull the latest file from device
const file = filename || getLatestFile(packageName);
if (!file) {
throw new (_cliTools().CLIError)('There is no file in the cache/ directory. Did you record a profile from the developer menu?');
}
_cliTools().logger.info(`File to be pulled: ${file}`); // If destination path is not specified, pull to the current directory
dstPath = dstPath || ctx.root;
_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
execSyncWithLog(`adb shell run-as ${packageName} cp cache/${file} /sdcard`); // If --raw, pull the hermes profile to dstPath
if (raw) {
execSyncWithLog(`adb pull /sdcard/${file} ${dstPath}`);
_cliTools().logger.success(`Successfully pulled the file to ${dstPath}/${file}`);
} // Else: transform the profile to Chrome format and pull it to dstPath
else {
const osTmpDir = _os().default.tmpdir();
const tempFilePath = _path().default.join(osTmpDir, file);
execSyncWithLog(`adb pull /sdcard/${file} ${tempFilePath}`); // If path to source map is not given
if (!sourcemapPath) {
// Get or generate the source map
if (shouldGenerateSourcemap) {
sourcemapPath = await (0, _sourcemapUtils.generateSourcemap)(port);
} else {
sourcemapPath = await (0, _sourcemapUtils.findSourcemap)(ctx, port);
} // Run without source map
if (!sourcemapPath) {
_cliTools().logger.warn('Cannot find source maps, running the transformer without it');
_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.');
}
} // Run transformer tool to convert from Hermes to Chrome format
const events = await (0, _hermesProfileTransformer().default)(tempFilePath, sourcemapPath, 'index.bundle');
const transformedFilePath = `${dstPath}/${_path().default.basename(file, '.cpuprofile')}-converted.json`;
_fs().default.writeFileSync(transformedFilePath, JSON.stringify(events, undefined, 4), 'utf-8');
_cliTools().logger.success(`Successfully converted to Chrome tracing format and pulled the file to ${transformedFilePath}`);
}
} catch (e) {
throw e;
}
}
//# sourceMappingURL=downloadProfile.js.map

View File

@ -0,0 +1,62 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _cliTools() {
const data = require("@react-native-community/cli-tools");
_cliTools = function () {
return data;
};
return data;
}
var _downloadProfile = require("./downloadProfile");
async function profileHermes([dstPath], ctx, options) {
try {
_cliTools().logger.info('Downloading a Hermes Sampling Profiler from your Android device...');
if (!options.filename) {
_cliTools().logger.info('No filename is provided, pulling latest file');
}
await (0, _downloadProfile.downloadProfile)(ctx, dstPath, options.filename, options.sourcemapPath, options.raw, options.generateSourcemap, options.port);
} catch (err) {
throw err;
}
}
var _default = {
name: 'profile-hermes [destinationDir]',
description: 'Pull and convert a Hermes tracing profile to Chrome tracing profile, then store it in the directory <destinationDir> of the local machine',
func: profileHermes,
options: [{
name: '--filename [string]',
description: 'File name of the profile to be downloaded, eg. sampling-profiler-trace8593107139682635366.cpuprofile'
}, {
name: '--raw',
description: 'Pulls the original Hermes tracing profile without any transformation'
}, {
name: '--sourcemap-path [string]',
description: 'The local path to your source map file, eg. /tmp/sourcemap.json'
}, {
name: '--generate-sourcemap',
description: 'Generates the JS bundle and source map'
}, {
name: '--port [number]',
default: process.env.RCT_METRO_PORT || 8081,
parse: val => String(val)
}],
examples: [{
desc: 'Download the Hermes Sampling Profiler to the directory <destinationDir> on the local machine',
cmd: 'profile-hermes /tmp'
}]
};
exports.default = _default;
//# sourceMappingURL=index.js.map

View File

@ -0,0 +1,150 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.generateSourcemap = generateSourcemap;
exports.findSourcemap = findSourcemap;
function _cliTools() {
const data = require("@react-native-community/cli-tools");
_cliTools = function () {
return data;
};
return data;
}
function _fs() {
const data = _interopRequireDefault(require("fs"));
_fs = function () {
return data;
};
return data;
}
function _path() {
const data = _interopRequireDefault(require("path"));
_path = function () {
return data;
};
return data;
}
function _os() {
const data = _interopRequireDefault(require("os"));
_os = function () {
return data;
};
return data;
}
function _ip() {
const data = _interopRequireDefault(require("ip"));
_ip = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function getTempFilePath(filename) {
return _path().default.join(_os().default.tmpdir(), filename);
}
function writeJsonSync(targetPath, data) {
let json;
try {
json = JSON.stringify(data);
} catch (e) {
throw new (_cliTools().CLIError)(`Failed to serialize data to json before writing to ${targetPath}`, e);
}
try {
_fs().default.writeFileSync(targetPath, json, 'utf-8');
} catch (e) {
throw new (_cliTools().CLIError)(`Failed to write json to ${targetPath}`, e);
}
}
async function getSourcemapFromServer(port) {
_cliTools().logger.debug('Getting source maps from Metro packager server');
const DEBUG_SERVER_PORT = port || '8081';
const IP_ADDRESS = _ip().default.address();
const PLATFORM = 'android';
const requestURL = `http://${IP_ADDRESS}:${DEBUG_SERVER_PORT}/index.map?platform=${PLATFORM}&dev=true`;
try {
const {
data
} = await (0, _cliTools().fetch)(requestURL);
return data;
} catch (e) {
_cliTools().logger.debug(`Failed to fetch source map from "${requestURL}"`);
return undefined;
}
}
/**
* Generate a sourcemap by fetching it from a running metro server
*/
async function generateSourcemap(port) {
// Fetch the source map to a temp directory
const sourceMapPath = getTempFilePath('index.map');
const sourceMapResult = await getSourcemapFromServer(port);
if (sourceMapResult) {
_cliTools().logger.debug('Using source maps from Metro packager server');
writeJsonSync(sourceMapPath, sourceMapResult);
_cliTools().logger.debug(`Successfully obtained the source map and stored it in ${sourceMapPath}`);
return sourceMapPath;
} else {
_cliTools().logger.error('Cannot obtain source maps from Metro packager server');
return undefined;
}
}
/**
*
* @param ctx
*/
async function findSourcemap(ctx, port) {
const intermediateBuildPath = _path().default.join(ctx.root, 'android', 'app', 'build', 'intermediates', 'sourcemaps', 'react', 'debug', 'index.android.bundle.packager.map');
const generatedBuildPath = _path().default.join(ctx.root, 'android', 'app', 'build', 'generated', 'sourcemaps', 'react', 'debug', 'index.android.bundle.map');
if (_fs().default.existsSync(generatedBuildPath)) {
_cliTools().logger.debug(`Getting the source map from ${generateSourcemap}`);
return generatedBuildPath;
} else if (_fs().default.existsSync(intermediateBuildPath)) {
_cliTools().logger.debug(`Getting the source map from ${intermediateBuildPath}`);
return intermediateBuildPath;
} else {
return generateSourcemap(port);
}
}
//# sourceMappingURL=sourcemapUtils.js.map