@@ -4,6 +4,7 @@ var inquirer = require('inquirer');
44var path = require ( "path" ) ;
55var plist = require ( "plist" ) ;
66var xcode = require ( "xcode" ) ;
7+ var semver = require ( 'semver' ) ;
78
89var package = require ( '../../../../../package.json' ) ;
910
@@ -40,36 +41,56 @@ module.exports = () => {
4041 }
4142
4243 // 2. Modify jsCodeLocation value assignment
43- var jsCodeLocations = appDelegateContents . match ( / ( j s C o d e L o c a t i o n = . * ) / g ) ;
44+ var reactNativeVersion = package && package . dependencies && package . dependencies [ "react-native" ] ;
4445
45- if ( ! jsCodeLocations ) {
46- console . log ( 'Couldn\'t find jsCodeLocation setting in AppDelegate.' ) ;
47- }
48- var newJsCodeLocationAssignmentStatement = "jsCodeLocation = [CodePush bundleURL];" ;
49- if ( ~ appDelegateContents . indexOf ( newJsCodeLocationAssignmentStatement ) ) {
50- console . log ( `"jsCodeLocation" already pointing to "[CodePush bundleURL]".` ) ;
46+ if ( ! reactNativeVersion ) {
47+ console . log ( `Can't take react-native version from package.json` ) ;
48+ } else if ( semver . gte ( semver . coerce ( reactNativeVersion ) , "0.59.0" ) ) {
49+ var oldBundleUrl = "[[NSBundle mainBundle] URLForResource:@\"main\" withExtension:@\"jsbundle\"]" ;
50+ var codePushBundleUrl = "[CodePush bundleURL]" ;
51+
52+ if ( ~ appDelegateContents . indexOf ( codePushBundleUrl ) ) {
53+ console . log ( `"BundleUrl" already pointing to "[CodePush bundleURL]".` ) ;
54+ } else {
55+ if ( ~ appDelegateContents . indexOf ( oldBundleUrl ) ) {
56+ appDelegateContents = appDelegateContents . replace ( oldBundleUrl , codePushBundleUrl ) ;
57+ } else {
58+ console . log ( `AppDelegate isn't compatible for linking` ) ;
59+ }
60+ }
5161 } else {
52- if ( jsCodeLocations . length === 1 ) {
53- // If there is one `jsCodeLocation` it means that react-native app version is lower than 0.57.8
54- // and we should replace this line with DEBUG ifdef statement and add CodePush call for Release case
55-
56- var oldJsCodeLocationAssignmentStatement = jsCodeLocations [ 0 ] ;
57- var jsCodeLocationPatch = `
58- #ifdef DEBUG
59- ${ oldJsCodeLocationAssignmentStatement }
60- #else
61- ${ newJsCodeLocationAssignmentStatement }
62- #endif` ;
63- appDelegateContents = appDelegateContents . replace ( oldJsCodeLocationAssignmentStatement ,
64- jsCodeLocationPatch ) ;
65- } else if ( jsCodeLocations . length === 2 ) {
66- // If there are two `jsCodeLocation` it means that react-native app version is higher than 0.57.8 or equal
67- // and we should replace the second one(Release case) with CodePush call
68-
69- appDelegateContents = appDelegateContents . replace ( jsCodeLocations [ 1 ] ,
70- newJsCodeLocationAssignmentStatement ) ;
62+ var jsCodeLocations = appDelegateContents . match ( / ( j s C o d e L o c a t i o n = .* ) / g) ;
63+
64+ if ( ! jsCodeLocations ) {
65+ console . log ( 'Couldn\'t find jsCodeLocation setting in AppDelegate.' ) ;
66+ }
67+
68+ var newJsCodeLocationAssignmentStatement = "jsCodeLocation = [CodePush bundleURL];" ;
69+ if ( ~ appDelegateContents . indexOf ( newJsCodeLocationAssignmentStatement ) ) {
70+ console . log ( `"jsCodeLocation" already pointing to "[CodePush bundleURL]".` ) ;
7171 } else {
72- console . log ( `AppDelegate isn't compatible for linking` ) ;
72+ if ( jsCodeLocations . length === 1 ) {
73+ // If there is one `jsCodeLocation` it means that react-native app version is lower than 0.57.8
74+ // and we should replace this line with DEBUG ifdef statement and add CodePush call for Release case
75+
76+ var oldJsCodeLocationAssignmentStatement = jsCodeLocations [ 0 ] ;
77+ var jsCodeLocationPatch = `
78+ #ifdef DEBUG
79+ ${ oldJsCodeLocationAssignmentStatement }
80+ #else
81+ ${ newJsCodeLocationAssignmentStatement }
82+ #endif` ;
83+ appDelegateContents = appDelegateContents . replace ( oldJsCodeLocationAssignmentStatement ,
84+ jsCodeLocationPatch ) ;
85+ } else if ( jsCodeLocations . length === 2 ) {
86+ // If there are two `jsCodeLocation` it means that react-native app version is higher than 0.57.8 or equal
87+ // and we should replace the second one(Release case) with CodePush call
88+
89+ appDelegateContents = appDelegateContents . replace ( jsCodeLocations [ 1 ] ,
90+ newJsCodeLocationAssignmentStatement ) ;
91+ } else {
92+ console . log ( `AppDelegate isn't compatible for linking` ) ;
93+ }
7394 }
7495 }
7596
@@ -111,7 +132,7 @@ module.exports = () => {
111132 // Helper that filters an array with AppDelegate.m paths for a path with the app name inside it
112133 // Should cover nearly all cases
113134 function findFileByAppName ( array , appName ) {
114- if ( array . length === 0 || ! appName ) return null ;
135+ if ( array . length === 0 || ! appName ) return null ;
115136
116137 for ( var i = 0 ; i < array . length ; i ++ ) {
117138 var path = array [ i ] ;
@@ -206,4 +227,4 @@ module.exports = () => {
206227 //(see https://github.com/Microsoft/react-native-code-push/issues/534#issuecomment-302069326 for details)
207228 return path . resolve ( path . dirname ( xcodeProjectPath ) , '..' , plistPathValue . replace ( / ^ " ( .* ) " $ / , '$1' ) ) ;
208229 }
209- }
230+ }
0 commit comments