@dynatrace/react-native-plugin
Version:
This plugin gives you the ability to use the Dynatrace Mobile agent in your react native application.
127 lines (124 loc) • 7.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.instrumentCommand = void 0;
const path_1 = require("path");
const Logger_1 = require("../Logger");
const Config_1 = require("../Config");
const android = require("../Android");
const PathsConstants_1 = require("../PathsConstants");
const nodePath = require("path");
const Ios_1 = require("../Ios");
const InstrumentUtil_1 = require("../util/InstrumentUtil");
const CustomArgumentUtil_1 = require("../util/CustomArgumentUtil");
const Platform_1 = require("../api/model/Platform");
const SourceMapUtil_1 = require("../util/SourceMapUtil");
const instrumentCommand = () => {
if (process.argv.includes('--help')) {
Logger_1.default.logMessageSync(`
Usage: npx instrumentDynatrace [options]
Options:
--help Show this help message and exit.
config="/custom/path/to/dynatrace.config.js"
Specify a custom path to the Dynatrace configuration file.
Default: ${PathsConstants_1.default.getConfigFilePath()}
gradle="/custom/path/to/build.gradle(.kts)"
Specify a custom path to the top level build.gradle file of your android project.
Default: ${PathsConstants_1.default.getAndroidGradleFile(PathsConstants_1.default.getAndroidFolder())}
plist="/custom/path/to/Info.plist"
Specify a custom path to the Info.plist file of your ios project.
Default: Extracts <appName> from app.json and then tries
${nodePath.join(PathsConstants_1.default.getIOSFolder(), "<appName>", 'Info.plist')}
and ${nodePath.join(PathsConstants_1.default.getIOSFolder(), "<appName>", "Supporting", "Info.plist")}
in that order
Examples:
npx instrumentDynatrace
npx instrumentDynatrace config="/custom/path/to/dynatrace.config.js"
npx instrumentDynatrace gradle="/custom/path/to/build.gradle"
npx instrumentDynatrace plist="/custom/path/to/Info.plist"
`, Logger_1.default.INFO);
return;
}
Logger_1.default.logMessageSync('⏳ Starting instrumentation of React Native application ..', Logger_1.default.INFO);
Logger_1.default.withPrefix({ info: ' ℹ️ ', warning: ' ⚠️ ' }, () => (0, InstrumentUtil_1.showVersionOfPlugin)());
let pathToConfig = PathsConstants_1.default.getConfigFilePath();
let pathToGradle = PathsConstants_1.default.getAndroidGradleFile(PathsConstants_1.default.getAndroidFolder());
const pathToAppGradle = PathsConstants_1.default.getAndroidAppGradleFile(PathsConstants_1.default.getAndroidFolder());
let androidAvailable = true;
let pathToPList;
let iosAvailable = true;
const argv = (0, CustomArgumentUtil_1.parseCommandLine)(process.argv.slice(2));
if (argv.isEmpty()) {
(0, CustomArgumentUtil_1.clearCustomArguments)();
}
else {
(0, CustomArgumentUtil_1.writeCustomArguments)(argv);
}
if (argv.isCustomConfigurationPathSet()) {
pathToConfig = argv.getCustomConfigurationPath();
}
Logger_1.default.withPrefix({ info: ' ℹ️ ', warning: ' ⚠️ ', error: ' ❌ ' }, () => {
if (argv.isCustomGradlePathSet()) {
pathToGradle = argv.getCustomGradlePath();
androidAvailable = (0, InstrumentUtil_1.isPlatformAvailable)(pathToGradle, Platform_1.Platform.Android);
}
else {
androidAvailable = (0, InstrumentUtil_1.isPlatformAvailable)(PathsConstants_1.default.getAndroidFolder(), Platform_1.Platform.Android);
}
if (argv.isCustomPlistPathSet()) {
pathToPList = (0, path_1.resolve)(argv.getCustomPlistPath());
iosAvailable = (0, InstrumentUtil_1.isPlatformAvailable)(pathToPList, Platform_1.Platform.IOS);
}
else {
iosAvailable = (0, InstrumentUtil_1.isPlatformAvailable)(PathsConstants_1.default.getIOSFolder(), Platform_1.Platform.IOS);
}
});
pathToConfig = (0, path_1.resolve)(pathToConfig);
pathToGradle = (0, path_1.resolve)(pathToGradle);
if (iosAvailable || androidAvailable) {
try {
Logger_1.default.logMessageSync('⏳ Trying to read configuration file: ' + pathToConfig, Logger_1.default.INFO);
const configAgent = (0, Config_1.readConfig)(pathToConfig);
if (androidAvailable) {
try {
Logger_1.default.logMessageSync('⏳ Starting Android Instrumentation with Dynatrace!', Logger_1.default.INFO);
Logger_1.default.withPrefix({ info: ' ℹ️ ', warning: ' ⚠️ ', error: ' ❌ ' }, () => {
android.instrumentAndroidPlatform(pathToGradle, false);
android.writeGradleConfig(configAgent.android);
android.ensureRuntimeScriptApplied(pathToAppGradle);
});
android.copyGradleConfigFile(pathToGradle);
Logger_1.default.logMessageSync(' ✅ Finished Android Instrumentation with Dynatrace!', Logger_1.default.INFO);
}
catch (e) {
if (e instanceof Error) {
Logger_1.default.logMessageSync(' ❌ Android Instrumentation failed: ' + e.message, Logger_1.default.ERROR);
}
}
}
if (iosAvailable) {
try {
Logger_1.default.logMessageSync('⏳ Starting iOS Instrumentation with Dynatrace!', Logger_1.default.INFO);
Logger_1.default.withPrefix({ info: ' ℹ️ ', warning: ' ⚠️ ', error: ' ❌ ' }, () => Ios_1.default.modifyPListFile(pathToPList, configAgent.ios, false));
Logger_1.default.logMessageSync(' ✅ Finished iOS Instrumentation with Dynatrace!', Logger_1.default.INFO);
}
catch (e) {
if (e instanceof Error) {
Logger_1.default.logMessageSync(' ❌ iOS Instrumentation failed: ' + e.message, Logger_1.default.ERROR);
}
}
}
(0, SourceMapUtil_1.patchMetroSourceMap)();
Logger_1.default.logMessageSync('🎉 Finished instrumentation of React Native application ..', Logger_1.default.INFO);
}
catch (e) {
if (e instanceof Error) {
Logger_1.default.logMessageSync('❌ Instrumentation failed: ' + e.message, Logger_1.default.ERROR);
}
}
}
else {
Logger_1.default.logMessageSync('⚠️ Both Android and iOS Folder are not available - Skip instrumentation.', Logger_1.default.WARNING);
}
Logger_1.default.closeLogFile();
};
exports.instrumentCommand = instrumentCommand;