@dynatrace/react-native-plugin 2.333.1 → 2.335.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.ensureRuntimeScriptApplied = exports.copyGradleConfigFile = exports.writeGradleConfig = exports.instrumentAndroidPlatform = exports.GRADLE_APPLY_RUNTIME_SCRIPT = exports.GRADLE_APPLY_BUILDSCRIPT = exports.GRADLE_DYNATRACE_FILE = void 0;
4
+ exports.ensureRuntimeScriptApplied = exports.writeGradleConfig = exports.instrumentAndroidPlatform = exports.GRADLE_APPLY_RUNTIME_SCRIPT = exports.GRADLE_APPLY_BUILDSCRIPT = exports.GRADLE_DYNATRACE_FILE = void 0;
5
5
  const path_1 = require("path");
6
6
  const Logger_1 = require("./Logger");
7
7
  const FileOperationHelper_1 = require("./FileOperationHelper");
8
8
  const PathsConstants_1 = require("./PathsConstants");
9
9
  const GRADLE_CONFIG_IDENTIFIER = '// AUTO - INSERTED';
10
10
  exports.GRADLE_DYNATRACE_FILE = `apply from: "./${PathsConstants_1.DYNATRACE_CONFIG_GRADLE_FILE}"`;
11
+ const LEGACY_GRADLE_DYNATRACE_NODE_MODULES = 'node_modules/@dynatrace/react-native-plugin/files/dynatrace.gradle';
11
12
  const GRADLE_BUILDSCRIPT_IDENTIFIER = 'buildscript';
12
13
  exports.GRADLE_APPLY_BUILDSCRIPT = 'apply from: "../node_modules/@dynatrace/react-native-plugin/files/plugin.gradle", to: buildscript';
13
14
  const GRADLE_REACT_NATIVE_PLUGIN = 'apply plugin: "com.facebook.react.rootproject"';
@@ -31,78 +32,91 @@ const removeOldDynatraceClasspath = (gradleFileContent) => {
31
32
  }
32
33
  return gradleFileContentLines.join('\n');
33
34
  };
34
- const changeReactNativeBuildGradleFile = (pathToGradle, remove) => {
35
- const gradleFileContent = FileOperationHelper_1.default.readTextFromFileSync(pathToGradle);
36
- const modifiedFileContent = removeOldDynatraceClasspath(gradleFileContent);
37
- const gradleFileContentLines = modifiedFileContent.split('\n');
38
- let gradlePluginFileIndex = -1;
39
- let gradleDynatraceFileIndex = -1;
40
- for (let i = 0; i < gradleFileContentLines.length && (gradleDynatraceFileIndex === -1 || gradlePluginFileIndex === -1); i++) {
41
- if (gradleFileContentLines[i].indexOf('plugin.gradle') > -1) {
42
- gradlePluginFileIndex = i;
35
+ const findGradleLineIndices = (lines) => {
36
+ let pluginFileIndex = -1;
37
+ let dynatraceFileIndex = -1;
38
+ for (let i = 0; i < lines.length && (dynatraceFileIndex === -1 || pluginFileIndex === -1); i++) {
39
+ if (lines[i].indexOf('plugin.gradle') > -1) {
40
+ pluginFileIndex = i;
43
41
  }
44
- else if (gradleFileContentLines[i].indexOf(PathsConstants_1.DYNATRACE_CONFIG_GRADLE_FILE) > -1) {
45
- gradleDynatraceFileIndex = i;
42
+ else if (lines[i].indexOf(PathsConstants_1.DYNATRACE_CONFIG_GRADLE_FILE) > -1) {
43
+ dynatraceFileIndex = i;
46
44
  }
47
45
  }
46
+ return { pluginFileIndex, dynatraceFileIndex };
47
+ };
48
+ const removeDynatraceFromGradle = (lines, indices, pathToGradle) => {
48
49
  let modified = false;
49
- if (remove) {
50
- if (gradlePluginFileIndex !== -1) {
51
- gradleFileContentLines.splice(gradlePluginFileIndex, 1);
52
- modified = true;
53
- }
54
- if (gradleDynatraceFileIndex !== -1) {
55
- gradleFileContentLines.splice(gradleDynatraceFileIndex - (modified ? 1 : 0), 1);
56
- modified = true;
57
- }
58
- if (modified) {
59
- Logger_1.default.logMessageSync('Removed Dynatrace modifications from build.gradle: ' + pathToGradle, Logger_1.default.INFO);
50
+ if (indices.pluginFileIndex !== -1) {
51
+ lines.splice(indices.pluginFileIndex, 1);
52
+ modified = true;
53
+ }
54
+ if (indices.dynatraceFileIndex !== -1) {
55
+ lines.splice(indices.dynatraceFileIndex - (modified ? 1 : 0), 1);
56
+ modified = true;
57
+ }
58
+ if (modified) {
59
+ Logger_1.default.logMessageSync('Removed Dynatrace modifications from build.gradle: ' + pathToGradle, Logger_1.default.INFO);
60
+ }
61
+ return modified;
62
+ };
63
+ const addDynatraceToGradle = (lines, indices, pathToGradle) => {
64
+ let modified = false;
65
+ if (indices.dynatraceFileIndex !== -1 &&
66
+ lines[indices.dynatraceFileIndex].includes(LEGACY_GRADLE_DYNATRACE_NODE_MODULES)) {
67
+ lines[indices.dynatraceFileIndex] = exports.GRADLE_DYNATRACE_FILE;
68
+ Logger_1.default.logMessageSync('Migrated old node_modules dynatrace.gradle path to local path in build.gradle', Logger_1.default.INFO);
69
+ modified = true;
70
+ }
71
+ if (indices.pluginFileIndex === -1) {
72
+ const buildscriptIndex = lines.findIndex((line) => line.startsWith(GRADLE_BUILDSCRIPT_IDENTIFIER));
73
+ if (buildscriptIndex === -1) {
74
+ throw new Error('Could not find Buildscript block in build.gradle.');
60
75
  }
76
+ lines.splice(buildscriptIndex + 1, 0, exports.GRADLE_APPLY_BUILDSCRIPT);
77
+ modified = true;
78
+ }
79
+ if (indices.dynatraceFileIndex === -1) {
80
+ const pluginLineIndex = lines.findIndex((line) => line.includes(GRADLE_REACT_NATIVE_PLUGIN));
81
+ lines.splice(pluginLineIndex !== -1 ? pluginLineIndex : lines.length, 0, exports.GRADLE_DYNATRACE_FILE);
82
+ modified = true;
83
+ }
84
+ if (modified) {
85
+ Logger_1.default.logMessageSync('Added Dynatrace plugin.gradle to the build.gradle: ' + pathToGradle, Logger_1.default.INFO);
61
86
  }
62
87
  else {
63
- if (gradlePluginFileIndex === -1) {
64
- let gradleFileReactIndex = -1;
65
- for (let i = 0; i < gradleFileContentLines.length; i++) {
66
- if (gradleFileContentLines[i].startsWith(GRADLE_BUILDSCRIPT_IDENTIFIER)) {
67
- gradleFileReactIndex = i;
68
- break;
69
- }
70
- }
71
- if (gradleFileReactIndex === -1) {
72
- throw new Error('Could not find Buildscript block in build.gradle.');
73
- }
74
- gradleFileContentLines.splice(gradleFileReactIndex + 1, 0, exports.GRADLE_APPLY_BUILDSCRIPT);
75
- modified = true;
76
- }
77
- if (gradleDynatraceFileIndex === -1) {
78
- let pluginLineIndex = -1;
79
- for (let i = 0; i < gradleFileContentLines.length; i++) {
80
- if (gradleFileContentLines[i].includes(GRADLE_REACT_NATIVE_PLUGIN)) {
81
- pluginLineIndex = i;
82
- break;
83
- }
84
- }
85
- gradleFileContentLines.splice(pluginLineIndex !== -1 ? pluginLineIndex : gradleFileContentLines.length, 0, exports.GRADLE_DYNATRACE_FILE);
86
- modified = true;
87
- }
88
- if (modified) {
89
- Logger_1.default.logMessageSync('Added Dynatrace plugin.gradle to the build.gradle: ' + pathToGradle, Logger_1.default.INFO);
90
- }
91
- else {
92
- Logger_1.default.logMessageSync('Dynatrace plugin & agent already added to build.gradle', Logger_1.default.INFO);
93
- }
88
+ Logger_1.default.logMessageSync('Dynatrace plugin & agent already added to build.gradle', Logger_1.default.INFO);
94
89
  }
90
+ return modified;
91
+ };
92
+ const changeReactNativeBuildGradleFile = (pathToGradle, remove) => {
93
+ const gradleFileContent = FileOperationHelper_1.default.readTextFromFileSync(pathToGradle);
94
+ const modifiedFileContent = removeOldDynatraceClasspath(gradleFileContent);
95
+ const gradleFileContentLines = modifiedFileContent.split('\n');
96
+ const indices = findGradleLineIndices(gradleFileContentLines);
97
+ const modified = remove
98
+ ? removeDynatraceFromGradle(gradleFileContentLines, indices, pathToGradle)
99
+ : addDynatraceToGradle(gradleFileContentLines, indices, pathToGradle);
95
100
  if (modified) {
96
101
  const fullGradleFile = gradleFileContentLines.join('\n');
97
102
  FileOperationHelper_1.default.writeTextToFileSync(pathToGradle, fullGradleFile);
98
103
  }
99
104
  };
100
- const writeGradleConfig = (androidConfig) => {
105
+ const writeGradleConfig = (androidConfig, pathToGradle) => {
101
106
  if (androidConfig === undefined || androidConfig.config === undefined) {
102
107
  Logger_1.default.logMessageSync("Can't write configuration of Android agent because it is missing!", Logger_1.default.WARNING);
103
108
  return;
104
109
  }
105
- const gradleFileContent = FileOperationHelper_1.default.readTextFromFileSync(PathsConstants_1.default.getDynatraceGradleFile());
110
+ const gradleDirectory = (0, path_1.dirname)(pathToGradle);
111
+ const targetPath = (0, path_1.join)(gradleDirectory, PathsConstants_1.DYNATRACE_CONFIG_GRADLE_FILE);
112
+ let gradleFileContent;
113
+ try {
114
+ FileOperationHelper_1.default.checkIfFileExistsSync(targetPath);
115
+ gradleFileContent = FileOperationHelper_1.default.readTextFromFileSync(targetPath);
116
+ }
117
+ catch (_a) {
118
+ gradleFileContent = FileOperationHelper_1.default.readTextFromFileSync(PathsConstants_1.default.getDynatraceGradleFile());
119
+ }
106
120
  const gradleFileContentLines = removeOldGradleConfig(gradleFileContent);
107
121
  let gradleFileIndex = -1;
108
122
  for (let i = 0; i < gradleFileContentLines.length; i++) {
@@ -113,15 +127,14 @@ const writeGradleConfig = (androidConfig) => {
113
127
  }
114
128
  gradleFileContentLines.splice(gradleFileIndex + 1, 0, androidConfig.config);
115
129
  const fullGradleFile = gradleFileContentLines.join('\n');
116
- FileOperationHelper_1.default.writeTextToFileSync(PathsConstants_1.default.getDynatraceGradleFile(), fullGradleFile);
130
+ if (fullGradleFile === gradleFileContent) {
131
+ Logger_1.default.logMessageSync(`Configuration in ${PathsConstants_1.DYNATRACE_CONFIG_GRADLE_FILE} is already up to date`, Logger_1.default.INFO);
132
+ return;
133
+ }
134
+ FileOperationHelper_1.default.writeTextToFileSync(targetPath, fullGradleFile);
117
135
  Logger_1.default.logMessageSync(`Replaced old configuration with current configuration in ${PathsConstants_1.DYNATRACE_CONFIG_GRADLE_FILE}`, Logger_1.default.INFO);
118
136
  };
119
137
  exports.writeGradleConfig = writeGradleConfig;
120
- const copyGradleConfigFile = (pathToGradle) => {
121
- const gradleDirectory = (0, path_1.dirname)(pathToGradle);
122
- FileOperationHelper_1.default.copyFileSync(PathsConstants_1.default.getDynatraceGradleFile(), (0, path_1.join)(gradleDirectory, PathsConstants_1.DYNATRACE_CONFIG_GRADLE_FILE));
123
- };
124
- exports.copyGradleConfigFile = copyGradleConfigFile;
125
138
  const removeOldGradleConfig = (gradleFileContent) => {
126
139
  const gradleFileContentLines = gradleFileContent.split('\n');
127
140
  const gradleConfigIndex = [];
package/scripts/Config.js CHANGED
@@ -10,10 +10,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  });
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.checkConfiguration = exports.addDefaultConfigs = exports.readConfig = exports.defaultConfig = exports.ERROR_CONFIG_NOT_AVAILABLE = void 0;
13
+ exports.checkConfiguration = exports.addDefaultConfigs = exports.readConfig = exports.readConfigDefault = exports.defaultConfig = exports.ERROR_CONFIG_NOT_AVAILABLE = void 0;
14
14
  const FileOperationHelper_1 = require("./FileOperationHelper");
15
15
  const Logger_1 = require("./Logger");
16
16
  const PathsConstants_1 = require("./PathsConstants");
17
+ const CustomArgumentUtil_1 = require("./util/CustomArgumentUtil");
18
+ const path = require("path");
17
19
  exports.ERROR_CONFIG_NOT_AVAILABLE = '-1';
18
20
  exports.defaultConfig = {
19
21
  react: {
@@ -41,6 +43,14 @@ exports.defaultConfig = {
41
43
  },
42
44
  },
43
45
  };
46
+ const readConfigDefault = () => {
47
+ const customArguments = (0, CustomArgumentUtil_1.readCustomArguments)();
48
+ const configPath = customArguments.isCustomConfigurationPathSet()
49
+ ? path.join(PathsConstants_1.default.getApplicationPath(), customArguments.getCustomConfigurationPath())
50
+ : PathsConstants_1.default.getConfigFilePath();
51
+ return (0, exports.readConfig)(configPath);
52
+ };
53
+ exports.readConfigDefault = readConfigDefault;
44
54
  const readConfig = (pathToConfig) => {
45
55
  patchMalformedConfiguration(pathToConfig);
46
56
  const readConfig = require(pathToConfig);
@@ -85,10 +85,9 @@ const instrumentCommand = () => {
85
85
  Logger_1.default.logMessageSync('⏳ Starting Android Instrumentation with Dynatrace!', Logger_1.default.INFO);
86
86
  Logger_1.default.withPrefix({ info: ' ℹ️ ', warning: ' ⚠️ ', error: ' ❌ ' }, () => {
87
87
  android.instrumentAndroidPlatform(pathToGradle, false);
88
- android.writeGradleConfig(configAgent.android);
88
+ android.writeGradleConfig(configAgent.android, pathToGradle);
89
89
  android.ensureRuntimeScriptApplied(pathToAppGradle);
90
90
  });
91
- android.copyGradleConfigFile(pathToGradle);
92
91
  Logger_1.default.logMessageSync(' ✅ Finished Android Instrumentation with Dynatrace!', Logger_1.default.INFO);
93
92
  }
94
93
  catch (e) {
@@ -16,11 +16,7 @@ const diff_1 = require("diff");
16
16
  const Logger_1 = require("../Logger");
17
17
  const InstrumentUtil_1 = require("../util/InstrumentUtil");
18
18
  const FileOperationHelper_1 = require("../../scripts/FileOperationHelper");
19
- const ReactOptions_1 = require("../util/ReactOptions");
20
- const config = require("../../scripts/Config");
21
- const CustomArgumentUtil_1 = require("../../scripts/util/CustomArgumentUtil");
22
- const PathsConstants_1 = require("../../scripts/PathsConstants");
23
- const customArguments = (0, CustomArgumentUtil_1.readCustomArguments)();
19
+ const Config_1 = require("../../scripts/Config");
24
20
  class LineOffsetAnalyzer {
25
21
  constructor(options) {
26
22
  this.rootDir = (0, path_1.resolve)(options.projectRoot);
@@ -29,10 +25,15 @@ class LineOffsetAnalyzer {
29
25
  this.logFile = (0, path_1.resolve)(this.instrumentedDir, 'debug.log');
30
26
  this.appBundleInfo = options.appBundleInfo;
31
27
  this.sourcemapPath = options.sourcemapPath;
28
+ this.reactOptions = (0, Config_1.readConfigDefault)();
32
29
  }
33
30
  run() {
34
31
  var _a;
35
32
  return __awaiter(this, void 0, void 0, function* () {
33
+ if (!this.reactOptions.react.useLegacyJscodeshift) {
34
+ console.info('ℹ️ Skipping line offset analysis since this it not needed anymore for the new babel architecture.');
35
+ return;
36
+ }
36
37
  try {
37
38
  const buildExists = yield FileOperationHelper_1.default.checkIfFileExists(this.instrumentedDir);
38
39
  if (!buildExists) {
@@ -79,20 +80,13 @@ class LineOffsetAnalyzer {
79
80
  };
80
81
  yield fs.writeFile(this.outputFile, JSON.stringify(finalOutput, null, 2));
81
82
  yield this.log(`✅ Line offsets written to ${this.outputFile}`, true);
82
- let reactOptions;
83
- if (customArguments.isCustomConfigurationPathSet()) {
84
- reactOptions = config.readConfig((0, path_1.join)(PathsConstants_1.default.getApplicationPath(), customArguments.getCustomConfigurationPath()));
85
- }
86
- else {
87
- reactOptions = config.readConfig((0, path_1.join)(PathsConstants_1.default.getConfigFilePath()));
88
- }
89
- if (!this.sourcemapPath && !reactOptions.react.sourcemap.enabled) {
83
+ if (!this.sourcemapPath && !this.reactOptions.react.sourcemap.enabled) {
90
84
  const message = 'ℹ️ Automatic sourcemap patching disabled. Skipping sourcemap patching.';
91
85
  yield this.log(message);
92
86
  console.info(message);
93
87
  return;
94
88
  }
95
- const loadedMap = yield this.loadSourceMap((_a = this.sourcemapPath) !== null && _a !== void 0 ? _a : reactOptions.react.sourcemap.androidSourcemapLocation);
89
+ const loadedMap = yield this.loadSourceMap((_a = this.sourcemapPath) !== null && _a !== void 0 ? _a : this.reactOptions.react.sourcemap.androidSourcemapLocation);
96
90
  if (loadedMap) {
97
91
  const { map, path: originalMapPath } = loadedMap;
98
92
  map.x_dynatrace_offset = mappings;
@@ -126,7 +120,7 @@ class LineOffsetAnalyzer {
126
120
  }
127
121
  log(msg, logOnScreen = false) {
128
122
  return __awaiter(this, void 0, void 0, function* () {
129
- if (ReactOptions_1.reactOptions && ReactOptions_1.reactOptions.react.debug && logOnScreen) {
123
+ if (this.reactOptions && this.reactOptions.react.debug && logOnScreen) {
130
124
  Logger_1.default.logMessageSync(msg, Logger_1.default.INFO);
131
125
  }
132
126
  yield fs.appendFile(this.logFile, msg + '\n');
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.reactOptions = void 0;
4
- const nodePath = require("path");
5
- const PathsConstants_1 = require("../../scripts/PathsConstants");
6
- const CustomArgumentUtil_1 = require("../util/CustomArgumentUtil");
7
- const config = require("../../scripts/Config");
8
- const customArguments = (0, CustomArgumentUtil_1.readCustomArguments)();
9
- let reactOptions;
10
- exports.reactOptions = reactOptions;
11
- try {
12
- if (customArguments.isCustomConfigurationPathSet()) {
13
- exports.reactOptions = reactOptions = config.readConfig(nodePath.join(PathsConstants_1.default.getApplicationPath(), customArguments.getCustomConfigurationPath()));
14
- }
15
- else {
16
- exports.reactOptions = reactOptions = config.readConfig(nodePath.join(PathsConstants_1.default.getConfigFilePath()));
17
- }
18
- }
19
- catch (_a) {
20
- exports.reactOptions = reactOptions = undefined;
21
- }