@@ -2,88 +2,94 @@ var fs = require("fs");
2
2
var glob = require ( "glob" ) ;
3
3
var path = require ( "path" ) ;
4
4
5
- var ignoreFolders = { ignore : [ "node_modules/**" , "**/build/**" ] } ;
6
- var buildGradlePath = path . join ( "android" , "app" , "build.gradle" ) ;
7
- var manifestPath = glob . sync ( "**/AndroidManifest.xml" , ignoreFolders ) [ 0 ] ;
5
+ module . exports = ( ) => {
8
6
9
- function findMainApplication ( ) {
10
- if ( ! manifestPath ) {
11
- return null ;
12
- }
7
+ console . log ( "Running android postlink script" ) ;
13
8
14
- var manifest = fs . readFileSync ( manifestPath , "utf8" ) ;
9
+ var ignoreFolders = { ignore : [ "node_modules/**" , "**/build/**" ] } ;
10
+ var buildGradlePath = path . join ( "android" , "app" , "build.gradle" ) ;
11
+ var manifestPath = glob . sync ( "**/AndroidManifest.xml" , ignoreFolders ) [ 0 ] ;
15
12
16
- // Android manifest must include single 'application' element
17
- var matchResult = manifest . match ( / a p p l i c a t i o n \s + a n d r o i d : n a m e \s * = \s * " ( .* ?) " / ) ;
18
- if ( matchResult ) {
19
- var appName = matchResult [ 1 ] ;
20
- } else {
21
- return null ;
22
- }
23
-
24
- var nameParts = appName . split ( '.' ) ;
25
- var searchPath = glob . sync ( "**/" + nameParts [ nameParts . length - 1 ] + ".java" , ignoreFolders ) [ 0 ] ;
26
- return searchPath ;
27
- }
13
+ function findMainApplication ( ) {
14
+ if ( ! manifestPath ) {
15
+ return null ;
16
+ }
28
17
29
- var mainApplicationPath = findMainApplication ( ) || glob . sync ( "**/MainApplication.java" , ignoreFolders ) [ 0 ] ;
18
+ var manifest = fs . readFileSync ( manifestPath , "utf8" ) ;
30
19
31
- // 1. Add the getJSBundleFile override
32
- var getJSBundleFileOverride = `
33
- @Override
34
- protected String getJSBundleFile() {
35
- return CodePush.getJSBundleFile();
20
+ // Android manifest must include single 'application' element
21
+ var matchResult = manifest . match ( / a p p l i c a t i o n \s + a n d r o i d : n a m e \s * = \s * " ( .* ?) " / ) ;
22
+ if ( matchResult ) {
23
+ var appName = matchResult [ 1 ] ;
24
+ } else {
25
+ return null ;
26
+ }
27
+
28
+ var nameParts = appName . split ( '.' ) ;
29
+ var searchPath = glob . sync ( "**/" + nameParts [ nameParts . length - 1 ] + ".java" , ignoreFolders ) [ 0 ] ;
30
+ return searchPath ;
36
31
}
37
- ` ;
38
32
39
- function isAlreadyOverridden ( codeContents ) {
40
- return / @ O v e r r i d e \s * \n \s * p r o t e c t e d S t r i n g g e t J S B u n d l e F i l e \( \) \s * \{ [ \s \S ] * ?\} / . test ( codeContents ) ;
41
- }
33
+ var mainApplicationPath = findMainApplication ( ) || glob . sync ( "**/MainApplication.java" , ignoreFolders ) [ 0 ] ;
42
34
43
- if ( mainApplicationPath ) {
44
- var mainApplicationContents = fs . readFileSync ( mainApplicationPath , "utf8" ) ;
45
- if ( isAlreadyOverridden ( mainApplicationContents ) ) {
46
- console . log ( `"getJSBundleFile" is already overridden` ) ;
47
- } else {
48
- var reactNativeHostInstantiation = "new ReactNativeHost(this) {" ;
49
- mainApplicationContents = mainApplicationContents . replace ( reactNativeHostInstantiation ,
50
- `${ reactNativeHostInstantiation } \n${ getJSBundleFileOverride } ` ) ;
51
- fs . writeFileSync ( mainApplicationPath , mainApplicationContents ) ;
35
+ // 1. Add the getJSBundleFile override
36
+ var getJSBundleFileOverride = `
37
+ @Override
38
+ protected String getJSBundleFile() {
39
+ return CodePush.getJSBundleFile();
40
+ }
41
+ ` ;
42
+
43
+ function isAlreadyOverridden ( codeContents ) {
44
+ return / @ O v e r r i d e \s * \n \s * p r o t e c t e d S t r i n g g e t J S B u n d l e F i l e \( \) \s * \{ [ \s \S ] * ?\} / . test ( codeContents ) ;
52
45
}
53
- } else {
54
- var mainActivityPath = glob . sync ( "**/MainActivity.java" , ignoreFolders ) [ 0 ] ;
55
- if ( mainActivityPath ) {
56
- var mainActivityContents = fs . readFileSync ( mainActivityPath , "utf8" ) ;
57
- if ( isAlreadyOverridden ( mainActivityContents ) ) {
46
+
47
+ if ( mainApplicationPath ) {
48
+ var mainApplicationContents = fs . readFileSync ( mainApplicationPath , "utf8" ) ;
49
+ if ( isAlreadyOverridden ( mainApplicationContents ) ) {
58
50
console . log ( `"getJSBundleFile" is already overridden` ) ;
59
51
} else {
60
- var mainActivityClassDeclaration = "public class MainActivity extends ReactActivity {" ;
61
- mainActivityContents = mainActivityContents . replace ( mainActivityClassDeclaration ,
62
- `${ mainActivityClassDeclaration } \n${ getJSBundleFileOverride } ` ) ;
63
- fs . writeFileSync ( mainActivityPath , mainActivityContents ) ;
52
+ var reactNativeHostInstantiation = "new ReactNativeHost(this) {" ;
53
+ mainApplicationContents = mainApplicationContents . replace ( reactNativeHostInstantiation ,
54
+ `${ reactNativeHostInstantiation } \n${ getJSBundleFileOverride } ` ) ;
55
+ fs . writeFileSync ( mainApplicationPath , mainApplicationContents ) ;
64
56
}
65
57
} else {
66
- console . error ( `Couldn't find Android application entry point. You might need to update it manually. \
67
- Please refer to plugin configuration section for Android at \
68
- https://github.com/microsoft/react-native-code-push#plugin-configuration-android for more details` ) ;
58
+ var mainActivityPath = glob . sync ( "**/MainActivity.java" , ignoreFolders ) [ 0 ] ;
59
+ if ( mainActivityPath ) {
60
+ var mainActivityContents = fs . readFileSync ( mainActivityPath , "utf8" ) ;
61
+ if ( isAlreadyOverridden ( mainActivityContents ) ) {
62
+ console . log ( `"getJSBundleFile" is already overridden` ) ;
63
+ } else {
64
+ var mainActivityClassDeclaration = "public class MainActivity extends ReactActivity {" ;
65
+ mainActivityContents = mainActivityContents . replace ( mainActivityClassDeclaration ,
66
+ `${ mainActivityClassDeclaration } \n${ getJSBundleFileOverride } ` ) ;
67
+ fs . writeFileSync ( mainActivityPath , mainActivityContents ) ;
68
+ }
69
+ } else {
70
+ return Promise . reject ( `Couldn't find Android application entry point. You might need to update it manually. \
71
+ Please refer to plugin configuration section for Android at \
72
+ https://github.com/microsoft/react-native-code-push#plugin-configuration-android for more details` ) ;
73
+ }
69
74
}
70
- }
71
75
72
- if ( ! fs . existsSync ( buildGradlePath ) ) {
73
- console . error ( `Couldn't find build.gradle file. You might need to update it manually. \
74
- Please refer to plugin installation section for Android at \
75
- https://github.com/microsoft/react-native-code-push#plugin-installation-android---manual` ) ;
76
- return ;
77
- }
76
+ if ( ! fs . existsSync ( buildGradlePath ) ) {
77
+ return Promise . reject ( `Couldn't find build.gradle file. You might need to update it manually. \
78
+ Please refer to plugin installation section for Android at \
79
+ https://github.com/microsoft/react-native-code-push#plugin-installation-android---manual` ) ;
80
+ }
81
+
82
+ // 2. Add the codepush.gradle build task definitions
83
+ var buildGradleContents = fs . readFileSync ( buildGradlePath , "utf8" ) ;
84
+ var reactGradleLink = buildGradleContents . match ( / \n a p p l y f r o m : [ " ' ] .* ?r e a c t \. g r a d l e [ " ' ] / ) [ 0 ] ;
85
+ var codePushGradleLink = `apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"` ;
86
+ if ( ~ buildGradleContents . indexOf ( codePushGradleLink ) ) {
87
+ console . log ( `"codepush.gradle" is already linked in the build definition` ) ;
88
+ } else {
89
+ buildGradleContents = buildGradleContents . replace ( reactGradleLink ,
90
+ `${ reactGradleLink } \n${ codePushGradleLink } ` ) ;
91
+ fs . writeFileSync ( buildGradlePath , buildGradleContents ) ;
92
+ }
78
93
79
- // 2. Add the codepush.gradle build task definitions
80
- var buildGradleContents = fs . readFileSync ( buildGradlePath , "utf8" ) ;
81
- var reactGradleLink = buildGradleContents . match ( / \n a p p l y f r o m : [ " ' ] .* ?r e a c t \. g r a d l e [ " ' ] / ) [ 0 ] ;
82
- var codePushGradleLink = `apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"` ;
83
- if ( ~ buildGradleContents . indexOf ( codePushGradleLink ) ) {
84
- console . log ( `"codepush.gradle" is already linked in the build definition` ) ;
85
- } else {
86
- buildGradleContents = buildGradleContents . replace ( reactGradleLink ,
87
- `${ reactGradleLink } \n${ codePushGradleLink } ` ) ;
88
- fs . writeFileSync ( buildGradlePath , buildGradleContents ) ;
94
+ return Promise . resolve ( ) ;
89
95
}
0 commit comments