Skip to content

Commit 1c1e36e

Browse files
fix(core/plugin): Ensure android plugin type safety (#5098)
* fix(core/plugin): Ensure android plugin type safety * Add test --------- Co-authored-by: LucasZF <[email protected]>
1 parent 680d1e2 commit 1c1e36e

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

packages/core/plugin/src/withSentryAndroid.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import * as path from 'path';
55
import { warnOnce, writeSentryPropertiesTo } from './utils';
66

77
export const withSentryAndroid: ConfigPlugin<string> = (config, sentryProperties: string) => {
8-
const cfg = withAppBuildGradle(config, config => {
9-
if (config.modResults.language === 'groovy') {
10-
config.modResults.contents = modifyAppBuildGradle(config.modResults.contents);
8+
const cfg = withAppBuildGradle(config, appBuildGradle => {
9+
if (appBuildGradle.modResults.language === 'groovy') {
10+
appBuildGradle.modResults.contents = modifyAppBuildGradle(appBuildGradle.modResults.contents);
1111
} else {
1212
throw new Error('Cannot configure Sentry in the app gradle because the build.gradle is not groovy');
1313
}
14-
return config;
14+
return appBuildGradle;
1515
});
1616
return withDangerousMod(cfg, [
1717
'android',
18-
config => {
19-
writeSentryPropertiesTo(path.resolve(config.modRequest.projectRoot, 'android'), sentryProperties);
20-
return config;
18+
dangerousMod => {
19+
writeSentryPropertiesTo(path.resolve(dangerousMod.modRequest.projectRoot, 'android'), sentryProperties);
20+
return dangerousMod;
2121
},
2222
]);
2323
};

packages/core/plugin/src/withSentryAndroidGradlePlugin.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { withAppBuildGradle, withProjectBuildGradle } from '@expo/config-plugins';
2+
import type { ExpoConfig } from '@expo/config-types';
23

34
import { warnOnce } from './utils';
45

@@ -20,7 +21,7 @@ export const sentryAndroidGradlePluginVersion = '5.9.0';
2021
* https://docs.sentry.io/platforms/react-native/manual-setup/manual-setup/#enable-sentry-agp
2122
*/
2223
export function withSentryAndroidGradlePlugin(
23-
config: any,
24+
config: ExpoConfig,
2425
{
2526
includeProguardMapping = true,
2627
dexguardEnabled = false,
@@ -30,40 +31,34 @@ export function withSentryAndroidGradlePlugin(
3031
includeNativeSources = true,
3132
includeSourceContext = false,
3233
}: SentryAndroidGradlePluginOptions = {},
33-
): any {
34+
): ExpoConfig {
3435
// Modify android/build.gradle
35-
const withSentryProjectBuildGradle = (config: any): any => {
36-
return withProjectBuildGradle(config, (projectBuildGradle: any) => {
37-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
36+
const withSentryProjectBuildGradle = (config: ExpoConfig): ExpoConfig => {
37+
return withProjectBuildGradle(config, projectBuildGradle => {
3838
if (!projectBuildGradle.modResults || !projectBuildGradle.modResults.contents) {
3939
warnOnce('android/build.gradle content is missing or undefined.');
40-
return config;
40+
return projectBuildGradle;
4141
}
42-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
4342
if (projectBuildGradle.modResults.language !== 'groovy') {
4443
warnOnce('Cannot configure Sentry in android/build.gradle because it is not in Groovy.');
45-
return config;
44+
return projectBuildGradle;
4645
}
4746

4847
const dependency = `classpath("io.sentry:sentry-android-gradle-plugin:${sentryAndroidGradlePluginVersion}")`;
4948

50-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
5149
if (projectBuildGradle.modResults.contents.includes(dependency)) {
5250
warnOnce('sentry-android-gradle-plugin dependency in already in android/build.gradle.');
53-
return config;
51+
return projectBuildGradle;
5452
}
5553

5654
try {
57-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
5855
const updatedContents = projectBuildGradle.modResults.contents.replace(
5956
/dependencies\s*{/,
6057
`dependencies {\n ${dependency}`,
6158
);
62-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
6359
if (updatedContents === projectBuildGradle.modResults.contents) {
6460
warnOnce('Failed to inject the dependency. Could not find `dependencies` in build.gradle.');
6561
} else {
66-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
6762
projectBuildGradle.modResults.contents = updatedContents;
6863
}
6964
} catch (error) {
@@ -74,12 +69,11 @@ export function withSentryAndroidGradlePlugin(
7469
};
7570

7671
// Modify android/app/build.gradle
77-
const withSentryAppBuildGradle = (config: any): any => {
78-
return withAppBuildGradle(config, (config: any) => {
79-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
80-
if (config.modResults.language !== 'groovy') {
72+
const withSentryAppBuildGradle = (config: ExpoConfig): ExpoConfig => {
73+
return withAppBuildGradle(config, appBuildGradle => {
74+
if (appBuildGradle.modResults.language !== 'groovy') {
8175
warnOnce('Cannot configure Sentry in android/app/build.gradle because it is not in Groovy.');
82-
return config;
76+
return appBuildGradle;
8377
}
8478
const sentryPlugin = `apply plugin: "io.sentry.android.gradle"`;
8579
const sentryConfig = `
@@ -99,22 +93,18 @@ export function withSentryAndroidGradlePlugin(
9993
}
10094
}`;
10195

102-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
103-
let contents = config.modResults.contents;
96+
let contents = appBuildGradle.modResults.contents;
10497

105-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
10698
if (!contents.includes(sentryPlugin)) {
10799
contents = `${sentryPlugin}\n${contents}`;
108100
}
109101

110-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
111102
if (!contents.includes('sentry {')) {
112103
contents = `${contents}\n${sentryConfig}`;
113104
}
114105

115-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
116-
config.modResults.contents = contents;
117-
return config;
106+
appBuildGradle.modResults.contents = contents;
107+
return appBuildGradle;
118108
});
119109
};
120110

packages/core/test/expo-plugin/withSentryAndroidGradlePlugin.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,22 @@ describe('withSentryAndroidGradlePlugin', () => {
7575
const includedBuildGradle = `dependencies { classpath("io.sentry:sentry-android-gradle-plugin:${sentryAndroidGradlePluginVersion}")}`;
7676
const options: SentryAndroidGradlePluginOptions = { enableAndroidGradlePlugin: true };
7777

78+
const projectBuildGradle = {
79+
modResults: { language: 'groovy', contents: includedBuildGradle },
80+
};
81+
7882
(withProjectBuildGradle as jest.Mock).mockImplementation((config, callback) => {
79-
callback({ modResults: { language: 'groovy', contents: includedBuildGradle } });
83+
callback(projectBuildGradle);
8084
});
8185

8286
withSentryAndroidGradlePlugin(mockConfig, options);
8387

88+
const calledCallback = (withProjectBuildGradle as jest.Mock).mock.calls[0][1];
89+
const modifiedGradle = calledCallback(projectBuildGradle);
90+
91+
expect(modifiedGradle).toEqual({
92+
modResults: { language: 'groovy', contents: includedBuildGradle },
93+
});
8494
expect(warnOnce).toHaveBeenCalledWith(
8595
'sentry-android-gradle-plugin dependency in already in android/build.gradle.',
8696
);

0 commit comments

Comments
 (0)