@@ -2,6 +2,16 @@ const path = require('path');
22const fs = require ( 'fs' ) ;
33const { EOL } = require ( 'os' ) ;
44
5+ function modifyMainApplicationKt ( mainApplicationContent ) {
6+ if ( mainApplicationContent . includes ( 'CodePush.getJSBundleFile()' ) ) {
7+ console . log ( 'MainApplication.kt already has CodePush initialized.' ) ;
8+ return mainApplicationContent ;
9+ }
10+ return mainApplicationContent
11+ . replace ( 'import com.facebook.react.ReactApplication' , `import com.facebook.react.ReactApplication${ EOL } import com.microsoft.codepush.react.CodePush` )
12+ . replace ( 'override fun getJSMainModuleName(): String = "index"' , `override fun getJSMainModuleName(): String = "index"${ EOL } override fun getJSBundleFile(): String = CodePush.getJSBundleFile()` )
13+ }
14+
515async function initAndroid ( ) {
616 console . log ( 'Running Android setup...' ) ;
717 await applyMainApplication ( ) ;
@@ -10,42 +20,30 @@ async function initAndroid() {
1020async function applyMainApplication ( ) {
1121 const mainApplicationPath = await findMainApplication ( ) ;
1222 if ( ! mainApplicationPath ) {
13- console . log ( 'Could not find MainApplication.java or MainApplication. kt' ) ;
23+ console . log ( 'Could not find MainApplication.kt' ) ;
1424 return ;
1525 }
1626
17- const mainApplicationContent = fs . readFileSync ( mainApplicationPath , 'utf-8' ) ;
18-
1927 if ( mainApplicationPath . endsWith ( '.java' ) ) {
20- if ( mainApplicationContent . includes ( 'CodePush.getJSBundleFile()' ) ) {
21- console . log ( 'MainApplication.java already has CodePush initialized.' ) ;
22- return ;
23- }
24- const newContent = mainApplicationContent
25- . replace ( 'import com.facebook.react.ReactApplication;' , `import com.facebook.react.ReactApplication;${ EOL } import com.microsoft.codepush.react.CodePush;` )
26- . replace ( 'new DefaultReactNativeHost(this) {' , `new DefaultReactNativeHost(this) {${ EOL } @Override${ EOL } protected String getJSBundleFile() {${ EOL } return CodePush.getJSBundleFile();${ EOL } }${ EOL } ` )
27- fs . writeFileSync ( mainApplicationPath , newContent ) ;
28- console . log ( 'Successfully updated MainApplication.java.' ) ;
29- } else if ( mainApplicationPath . endsWith ( '.kt' ) ) {
30- if ( mainApplicationContent . includes ( 'CodePush.getJSBundleFile()' ) ) {
31- console . log ( 'MainApplication.kt already has CodePush initialized.' ) ;
32- return ;
33- }
34- const newContent = mainApplicationContent
35- . replace ( 'import com.facebook.react.ReactApplication' , `import com.facebook.react.ReactApplication${ EOL } import com.microsoft.codepush.react.CodePush` )
36- . replace ( 'override fun getJSMainModuleName(): String = "index"' , `override fun getJSMainModuleName(): String = "index"${ EOL } override fun getJSBundleFile(): String = CodePush.getJSBundleFile()` )
37- fs . writeFileSync ( mainApplicationPath , newContent ) ;
38- console . log ( 'Successfully updated MainApplication.kt.' ) ;
28+ console . log ( 'MainApplication.java is not supported. Please migrate to MainApplication.kt.' ) ;
29+ return ;
3930 }
31+
32+ const mainApplicationContent = fs . readFileSync ( mainApplicationPath , 'utf-8' ) ;
33+ const newContent = modifyMainApplicationKt ( mainApplicationContent ) ;
34+ fs . writeFileSync ( mainApplicationPath , newContent ) ;
35+ console . log ( 'Successfully updated MainApplication.kt.' ) ;
4036}
4137
4238async function findMainApplication ( ) {
4339 const searchPath = path . join ( process . cwd ( ) , 'android' , 'app' , 'src' , 'main' , 'java' ) ;
4440 const files = fs . readdirSync ( searchPath , { recursive : true } ) ;
45- const mainApplicationFile = files . find ( file => file . endsWith ( 'MainApplication.java' ) || file . endsWith ( 'MainApplication.kt' ) ) ;
41+ // Support only .kt files
42+ const mainApplicationFile = files . find ( file => file . endsWith ( 'MainApplication.kt' ) ) ;
4643 return mainApplicationFile ? path . join ( searchPath , mainApplicationFile ) : null ;
4744}
4845
4946module . exports = {
50- initAndroid : initAndroid
47+ initAndroid : initAndroid ,
48+ modifyMainApplicationKt,
5149} ;
0 commit comments