Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions expo/plugin/__tests__/withCodePushAndroid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,7 @@ describe("withAndroidMainApplicationDependency", () => {
const packageListIndex = modifiedContent.indexOf("packageList =");
const jsBundleIndex = modifiedContent.indexOf("jsBundleFilePath = CodePush.getJSBundleFile()");
expect(jsBundleIndex).toBeGreaterThan(packageListIndex);
// Ensure plugin normalized the missing comma after packageList block.
expect(modifiedContent).toContain(" },\n jsBundleFilePath = CodePush.getJSBundleFile(),");
});
});
31 changes: 15 additions & 16 deletions expo/plugin/withCodePushAndroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ function androidMainApplicationApplyImplementation(mainApplication, find, add, r
}

function addJsBundleFilePathArgument(mainApplication) {
if (mainApplication.includes(JS_BUNDLE_FILE_PATH_ARGUMENT)) {
return mainApplication;
}

const packageListArgumentPattern = /(packageList\s*=\s*\n\s*PackageList\(this\)[\s\S]+?\},?\s*\n)/;
// Capture packageList block plus optional existing jsBundleFilePath line.
// This allows us to normalize comma placement before injecting the new argument.
const packageListArgumentPattern = /(packageList\s*=\s*\n\s*PackageList\(this\)[\s\S]+?\},?\s*\n)(\s*jsBundleFilePath\s*=\s*CodePush\.getJSBundleFile\(\),\s*\n)?/;

if (!packageListArgumentPattern.test(mainApplication)) {
WarningAggregator.addWarningAndroid(
Expand All @@ -47,12 +45,15 @@ function addJsBundleFilePathArgument(mainApplication) {
return mainApplication;
}

return mainApplication.replace(packageListArgumentPattern, (match) => {
if (match.includes('jsBundleFilePath')) {
return match;
}

return `${match} ${JS_BUNDLE_FILE_PATH_ARGUMENT},\n`;
return mainApplication.replace(packageListArgumentPattern, (match, packageListArgument, existingJsBundleArgument) => {
const trimmedMatch = packageListArgument.replace(/\s+$/, '');
// Kotlin call args need a trailing comma before the next named argument.
const hasTrailingComma = /,\s*$/.test(trimmedMatch);
const packageListArgumentWithComma = hasTrailingComma ? trimmedMatch : `${trimmedMatch},`;
const jsBundleArgument = existingJsBundleArgument
? existingJsBundleArgument.replace(/\s+$/, '')
: ` ${JS_BUNDLE_FILE_PATH_ARGUMENT},`;
return `${packageListArgumentWithComma}\n${jsBundleArgument}\n`;
});
}

Expand All @@ -64,10 +65,9 @@ const withAndroidMainApplicationDependency = (config) => {
IMPORT_CODE_PUSH,
);

if (!action.modResults.contents.includes('CodePush.getJSBundleFile()')) {
if (action.modResults.contents.includes(RN_082_MARKER)) {
action.modResults.contents = addJsBundleFilePathArgument(action.modResults.contents);
} else {
if (action.modResults.contents.includes(RN_082_MARKER)) {
action.modResults.contents = addJsBundleFilePathArgument(action.modResults.contents);
} else if (!action.modResults.contents.includes('CodePush.getJSBundleFile()')) {
// https://github.com/Soomgo-Mobile/react-native-code-push/issues/97
const isExpoSDK54 = config.sdkVersion?.startsWith('54.') ?? false;
const addingCode = isExpoSDK54
Expand All @@ -81,7 +81,6 @@ const withAndroidMainApplicationDependency = (config) => {
'object : DefaultReactNativeHost(this) {',
addingCode,
);
}
}

return action;
Expand Down