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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
### @config-plugins/react-native-codepush 1.0.2

- Update interim README

### @config-plugins/react-native-codepush 1.0.5

- Expo SDK 50 support
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
Config plugin to auto-configure [`react-native-code-push`][lib] when the native code is generated (`npx expo prebuild`).

### ⚠️ Remove Expo updates completely ⚠️

Before installing this package, you need completely remove Expo updates from your project:

- Expo updates configurations
- Expo updates execution code in your app
- Expo updates package `npm uninstall -s expo-updates` or `yarn remove expo-updates`
- Any other thing you have done with Expo updates

### Add the package to your npm dependencies

> Tested against Expo SDK 49
> Tested against Expo SDK 50

```
yarn add react-native-code-push react-native-code-push-plugin
Expand Down
5 changes: 5 additions & 0 deletions build/android/buildscriptDependency.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/android/buildscriptDependency.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 29 additions & 7 deletions build/android/mainApplicationDependency.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/android/mainApplicationDependency.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions build/utils/addBelowAnchorIfNotFound.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/utils/addBelowAnchorIfNotFound.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-code-push-plugin",
"version": "1.0.3",
"version": "1.0.5",
"description": "Config plugin to auto configure react-native-code-push on prebuild",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down Expand Up @@ -29,10 +29,10 @@
"expo"
],
"peerDependencies": {
"expo": "^49"
"expo": ">=49.0.0"
},
"devDependencies": {
"expo": "^49",
"expo": ">=49.0.0",
"expo-module-scripts": "^3.0.3"
},
"upstreamPackage": "react-native-code-push"
Expand Down
10 changes: 10 additions & 0 deletions src/android/buildscriptDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ function applyImplementation(appBuildGradle: string) {
return appBuildGradle;
}

// The default on Expo 50
const reactNative73Include = `apply from: new File(["node", "--print", "require.resolve('@react-native-community/cli-platform-android/package.json', { paths: [require.resolve('react-native/package.json')] })"].execute(null, rootDir).text.trim(), "../native_modules.gradle");`;
if (appBuildGradle.includes(reactNative73Include)) {
return addBelowAnchorIfNotFound(
appBuildGradle,
reactNative73Include,
codePushImplementation
);
}

// Seems to be the default on Expo 49
const reactNative71Include = `apply from: new File(["node", "--print", "require.resolve('@react-native-community/cli-platform-android/package.json')"].execute(null, rootDir).text.trim(), "../native_modules.gradle");`;
if (appBuildGradle.includes(reactNative71Include)) {
Expand Down
64 changes: 49 additions & 15 deletions src/android/mainApplicationDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,82 @@ export const withAndroidMainApplicationDependency: ConfigPlugin<
> = (config) => {
return withMainApplication(config, (mainApplicationProps) => {
// Import the plugin class.
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
mainApplicationProps.modResults.contents,
"import expo.modules.ReactNativeHostWrapper;",
"import com.microsoft.codepush.react.CodePush;"
);
const hostWrapperClass = "import expo.modules.ReactNativeHostWrapper";
const codePushClass = "import com.microsoft.codepush.react.CodePush";

// Expo 50 uses Kotlin and does not require the ;
if (mainApplicationProps.modResults.contents.includes(hostWrapperClass)) {
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
mainApplicationProps.modResults.contents,
hostWrapperClass,
codePushClass
);
}

// Expo 49 uses Java and requires the ;
if (
mainApplicationProps.modResults.contents.includes(`${hostWrapperClass};`)
) {
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
mainApplicationProps.modResults.contents,
`${hostWrapperClass};`,
`${codePushClass};`
);
}

/**
* Override the getJSBundleFile method in order to let
* the CodePush runtime determine where to get the JS
* bundle location from on each app start
*/
const getJSBundleFileOverride = `

// The default on Expo 50, which uses kotlin
const kotlinAnchor = `override fun getJSMainModuleName(): String = ".expo/.virtual-metro-entry"`;
if (mainApplicationProps.modResults.contents.includes(kotlinAnchor)) {
const kotlinJSBundleFileOverride = `
override fun getJSBundleFile(): String? {
return CodePush.getJSBundleFile()
}
`;
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
mainApplicationProps.modResults.contents,
kotlinAnchor,
kotlinJSBundleFileOverride
);
return mainApplicationProps;
}

const javaJSBundleFileOverride = `
@Override
protected String getJSBundleFile() {
return CodePush.getJSBundleFile();
}\n`;

// This seems to be the default on Expo 49
// The default on Expo 49
const defaultReactNativeAnchor = "new DefaultReactNativeHost(this) {";
if (
mainApplicationProps.modResults.contents.includes(
"new DefaultReactNativeHost(this) {"
defaultReactNativeAnchor
)
) {
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
mainApplicationProps.modResults.contents,
`new DefaultReactNativeHost(this) {`,
getJSBundleFileOverride
defaultReactNativeAnchor,
javaJSBundleFileOverride
);

return mainApplicationProps;
}

// This is for compatibility, as it follows the Codepush instructions up-to-spec.
const reactNativeHostAnchor = "new ReactNativeHost(this) {";
if (
mainApplicationProps.modResults.contents.includes(
"new ReactNativeHost(this) {"
)
mainApplicationProps.modResults.contents.includes(reactNativeHostAnchor)
) {
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
mainApplicationProps.modResults.contents,
`new ReactNativeHost(this) {`,
getJSBundleFileOverride
reactNativeHostAnchor,
javaJSBundleFileOverride
);

return mainApplicationProps;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/addBelowAnchorIfNotFound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ export function addBelowAnchorIfNotFound(
return originalString.replace(anchor, `${anchor}\n${stringToBeAdded}`);
}

if (!originalString.includes(anchor)) {
throw new Error(
`The anchor string "${anchor}" was not found in the original string.`
);
}

return originalString;
}