11import  { ConfigPlugin ,  withDangerousMod }  from  '@expo/config-plugins' ; 
2+ import  { mergeContents }  from  '@expo/config-plugins/build/utils/generateCode' ; 
23import  *  as  fs  from  'fs/promises' ; 
34import  *  as  path  from  'path' ; 
45
@@ -27,11 +28,6 @@ type Props = {
2728  ) [ ] ; 
2829} ; 
2930
30- const  requireRegExp  = 
31-   / r e q u i r e   P o d : : E x e c u t a b l e \. e x e c u t e _ c o m m a n d \( ' n o d e ' ,   \[ ' - p ' , \s * ' r e q u i r e \. r e s o l v e \( \s * " r e a c t - n a t i v e \/ s c r i p t s \/ r e a c t _ n a t i v e _ p o d s \. r b " , \s * { p a t h s :   \[ p r o c e s s \. a r g v \[ 1 \] ] } ,\s * \) ' ,   _ _ d i r _ _ \] \) \. s t r i p / ; 
32- 
33- const  prepareRegExp  =  / p r e p a r e _ r e a c t _ n a t i v e _ p r o j e c t ! / ; 
34- 
3531const  withPermissions : ConfigPlugin < Props >  =  ( config ,  { iosPermissions =  [ ] } )  => 
3632  withDangerousMod ( config ,  [ 
3733    'ios' , 
@@ -42,41 +38,36 @@ const withPermissions: ConfigPlugin<Props> = (config, {iosPermissions = []}) =>
4238      if  ( iosPermissions . length  ===  0 )  { 
4339        return  config ; 
4440      } 
45-       if  ( ! requireRegExp . test ( contents )  ||  ! prepareRegExp . test ( contents ) )  { 
41+ 
42+       const  withRequire  =  mergeContents ( { 
43+         tag : 'require' , 
44+         src : contents , 
45+         anchor :
46+           / ^ r e q u i r e   F i l e \. j o i n \( F i l e \. d i r n a m e \( ` n o d e   - - p r i n t   " r e q u i r e \. r e s o l v e \( ' r e a c t - n a t i v e \/ p a c k a g e \. j s o n ' \) " ` \) ,   " s c r i p t s \/ r e a c t _ n a t i v e _ p o d s " \) $ / m, 
47+         newSrc : `require File.join(File.dirname(\`node --print "require.resolve('react-native-permissions/package.json')"\`), "scripts/setup")` , 
48+         offset : 1 , 
49+         comment : '#' , 
50+       } ) ; 
51+ 
52+       const  withSetup  =  mergeContents ( { 
53+         tag : 'setup' , 
54+         src : withRequire . contents , 
55+         anchor : / ^ p r e p a r e _ r e a c t _ n a t i v e _ p r o j e c t ! $ / m, 
56+         newSrc : `setup_permissions([ 
57+ ${ iosPermissions . map ( ( permission )  =>  `  '${ permission }  ',` ) . join ( '\n' ) } 
58+ ])` , 
59+         offset : 1 , 
60+         comment : '#' , 
61+       } ) ; 
62+ 
63+       if  ( ! withRequire . didMerge  ||  ! withSetup . didMerge )  { 
4664        console . error ( 
4765          "ERROR: Cannot add react-native-permissions to the project's ios/Podfile because it's malformed. Please report this with a copy of your project Podfile." , 
4866        ) ; 
4967        return  config ; 
5068      } 
5169
52-       const  nodeRequire  =  ` 
53- def node_require(script) 
54-   # Resolve script with node to allow for hoisting 
55-   require Pod::Executable.execute_command('node', ['-p', 
56-     "require.resolve( 
57-       '#{script}', 
58-       {paths: [process.argv[1]]}, 
59-     )", __dir__]).strip 
60- end 
61- 
62- node_require('react-native/scripts/react_native_pods.rb') 
63- node_require('react-native-permissions/scripts/setup.rb') 
64- ` . trim ( ) ; 
65- 
66-       const  setupFunction  =  ` 
67- prepare_react_native_project! 
68- 
69- setup_permissions([ 
70- ${ iosPermissions . map ( ( permission )  =>  `  '${ permission }  ',` ) . join ( '\n' ) } 
71- ]) 
72- ` . trim ( ) ; 
73- 
74-       await  fs . writeFile ( 
75-         file , 
76-         contents . replace ( requireRegExp ,  nodeRequire ) . replace ( prepareRegExp ,  setupFunction ) , 
77-         'utf-8' , 
78-       ) ; 
79- 
70+       await  fs . writeFile ( file ,  withSetup . contents ,  'utf-8' ) ; 
8071      return  config ; 
8172    } , 
8273  ] ) ; 
0 commit comments