1
+ var fs = require ( "fs" ) ;
2
+ var glob = require ( "glob" ) ;
3
+ var inquirer = require ( 'inquirer' ) ;
4
+ var path = require ( "path" ) ;
5
+ var plist = require ( "plist" ) ;
6
+
7
+ var ignoreNodeModules = { ignore : "node_modules/**" } ;
8
+ var appDelegatePath = glob . sync ( "**/AppDelegate.m" , ignoreNodeModules ) [ 0 ] ;
9
+ // Glob only allows foward slashes in patterns: https://www.npmjs.com/package/glob#windows
10
+ var plistPath = glob . sync ( path . join ( path . dirname ( appDelegatePath ) , "*Info.plist" ) . replace ( / \\ / g, "/" ) , ignoreNodeModules ) [ 0 ] ;
11
+
12
+ var appDelegateContents = fs . readFileSync ( appDelegatePath , "utf8" ) ;
13
+ var plistContents = fs . readFileSync ( plistPath , "utf8" ) ;
14
+
15
+ // 1. Add the header import statement
16
+ var codePushHeaderImportStatement = `#import "CodePush.h"` ;
17
+ if ( ~ appDelegateContents . indexOf ( codePushHeaderImportStatement ) ) {
18
+ console . log ( `"CodePush.h" header already imported.` ) ;
19
+ } else {
20
+ var appDelegateHeaderImportStatement = `#import "AppDelegate.h"` ;
21
+ appDelegateContents = appDelegateContents . replace ( appDelegateHeaderImportStatement ,
22
+ `${ appDelegateHeaderImportStatement } \n${ codePushHeaderImportStatement } ` ) ;
23
+ }
24
+
25
+ // 2. Modify jsCodeLocation value assignment
26
+ var oldJsCodeLocationAssignmentStatement = appDelegateContents . match ( / ( j s C o d e L o c a t i o n = .* ) \n / ) [ 1 ] ;
27
+ var newJsCodeLocationAssignmentStatement = "jsCodeLocation = [CodePush bundleURL];" ;
28
+ if ( ~ appDelegateContents . indexOf ( newJsCodeLocationAssignmentStatement ) ) {
29
+ console . log ( `"jsCodeLocation" already pointing to "[CodePush bundleURL]".` ) ;
30
+ } else {
31
+ var jsCodeLocationPatch = `
32
+ #ifdef DEBUG
33
+ ${ oldJsCodeLocationAssignmentStatement }
34
+ #else
35
+ ${ newJsCodeLocationAssignmentStatement }
36
+ #endif` ;
37
+ appDelegateContents = appDelegateContents . replace ( oldJsCodeLocationAssignmentStatement ,
38
+ jsCodeLocationPatch ) ;
39
+ }
40
+
41
+ // 3. Add CodePushDeploymentKey to plist file
42
+ var parsedInfoPlist = plist . parse ( plistContents ) ;
43
+ if ( parsedInfoPlist . CodePushDeploymentKey ) {
44
+ console . log ( `"CodePushDeploymentKey" already specified in the plist file.` ) ;
45
+ writePatches ( ) ;
46
+ } else {
47
+ inquirer . prompt ( {
48
+ "type" : "input" ,
49
+ "name" : "iosDeploymentKey" ,
50
+ "message" : "What is your CodePush deployment key for iOS (hit <ENTER> to ignore)"
51
+ } ) . then ( function ( answer ) {
52
+ parsedInfoPlist . CodePushDeploymentKey = answer . iosDeploymentKey || "deployment-key-here" ;
53
+ plistContents = plist . build ( parsedInfoPlist ) ;
54
+
55
+ writePatches ( ) ;
56
+ } ) ;
57
+ }
58
+
59
+ function writePatches ( ) {
60
+ fs . writeFileSync ( appDelegatePath , appDelegateContents ) ;
61
+ fs . writeFileSync ( plistPath , plistContents ) ;
62
+ }
0 commit comments