11import { withAppDelegate , ConfigPlugin } from '@expo/config-plugins' ;
2- import { isExpo53OrLater } from '../expo-version' ;
2+ import { assertExpo53OrLater , isExpo53OrLater } from '../expo-version' ;
33
44const codeModIOs = require ( '@expo/config-plugins/build/ios/codeMod' ) ;
55
6- const withAppDelegateSwift : ConfigPlugin = rootConfig => {
7- return withAppDelegate ( rootConfig , config => {
8- let { contents } = config . modResults ;
9-
10- if ( ! contents . includes ( 'RNAppAuthAuthorizationFlowManager' ) ) {
11- const replaceText = 'class AppDelegate: ExpoAppDelegate' ;
12- contents = contents . replace ( replaceText , `${ replaceText } , RNAppAuthAuthorizationFlowManager` ) ;
13-
14- const replaceText2 =
15- 'return super.application(app, open: url, options: options) || RCTLinkingManager.application(app, open: url, options: options)' ;
16- contents = contents . replace (
17- replaceText2 ,
18- `if let authorizationFlowManagerDelegate = self.authorizationFlowManagerDelegate {
6+ const APP_AUTH_PROTOCOL = 'RNAppAuthAuthorizationFlowManager' ;
7+ const APP_AUTH_DELEGATE_PROPERTY =
8+ 'public weak var authorizationFlowManagerDelegate: RNAppAuthAuthorizationFlowManagerDelegate?' ;
9+ const APP_AUTH_DELEGATE_PROPERTY_PATTERN =
10+ / \b v a r \s + a u t h o r i z a t i o n F l o w M a n a g e r D e l e g a t e \s * : \s * R N A p p A u t h A u t h o r i z a t i o n F l o w M a n a g e r D e l e g a t e \? ? / ;
11+ const APP_AUTH_RESUME_BLOCK = `if let authorizationFlowManagerDelegate = self.authorizationFlowManagerDelegate {
1912 if authorizationFlowManagerDelegate.resumeExternalUserAgentFlow(with: url) {
20- return true
13+ return true
14+ }
15+ }` ;
16+
17+ export const applyExpo53AppDelegatePatch = ( contents : string ) : string => {
18+ contents = contents . replace (
19+ / ^ ( \s * (?: p u b l i c \s + ) ? c l a s s \s + A p p D e l e g a t e \s * : \s * E x p o A p p D e l e g a t e ) ( [ ^ { ] * ) ( \{ ) / m,
20+ ( match , declaration , conformances , openingBrace ) => {
21+ const trimmedConformances = conformances . trim ( ) ;
22+ if ( trimmedConformances . includes ( APP_AUTH_PROTOCOL ) ) {
23+ return match ;
2124 }
25+
26+ return `${ declaration } ${ trimmedConformances } , ${ APP_AUTH_PROTOCOL } ${ openingBrace } ` ;
2227 }
23- ${ replaceText2 } `
24- ) ;
28+ ) ;
2529
26- const replaceText3 = 'var reactNativeFactory: RCTReactNativeFactory?' ;
30+ if ( ! APP_AUTH_DELEGATE_PROPERTY_PATTERN . test ( contents ) ) {
31+ const reactNativeFactoryPattern =
32+ / ^ ( \s * ) (?: p u b l i c \s + ) ? v a r \s + r e a c t N a t i v e F a c t o r y \s * : \s * R C T R e a c t N a t i v e F a c t o r y \? \s * $ / m;
33+ const factoryMatch = contents . match ( reactNativeFactoryPattern ) ;
34+ if ( factoryMatch ) {
35+ const indent = factoryMatch [ 1 ] ;
2736 contents = contents . replace (
28- replaceText3 ,
29- `${ replaceText3 } \n\n public weak var authorizationFlowManagerDelegate: RNAppAuthAuthorizationFlowManagerDelegate? `
37+ reactNativeFactoryPattern ,
38+ match => `${ match } \n\n${ indent } ${ APP_AUTH_DELEGATE_PROPERTY } `
3039 ) ;
3140 }
41+ }
3242
33- config . modResults . contents = contents ;
43+ if ( ! contents . includes ( 'resumeExternalUserAgentFlow(with: url)' ) ) {
44+ contents = contents . replace (
45+ / ( (?: p u b l i c \s + ) ? o v e r r i d e \s + f u n c \s + a p p l i c a t i o n \s * \( [ \s \S ] * ?o p e n \s + u r l \s * : \s * U R L [ \s \S ] * ?\) \s * - > \s * B o o l \s * \{ ) / m,
46+ match => `${ match } \n ${ APP_AUTH_RESUME_BLOCK } \n`
47+ ) ;
48+ }
49+
50+ return contents ;
51+ } ;
52+
53+ const withAppDelegateSwift : ConfigPlugin = rootConfig => {
54+ return withAppDelegate ( rootConfig , config => {
55+ assertExpo53OrLater ( config , config . modRequest . projectRoot ) ;
56+ config . modResults . contents = applyExpo53AppDelegatePatch ( config . modResults . contents ) ;
3457 return config ;
3558 } ) ;
3659} ;
3760
38- export const withAppAuthAppDelegate : ConfigPlugin = rootConfig => {
39- if ( isExpo53OrLater ( rootConfig ) ) {
40- return withAppDelegateSwift ( rootConfig ) ;
41- }
42-
61+ export const withLegacyAppAuthAppDelegate : ConfigPlugin = rootConfig => {
4362 return withAppDelegate ( rootConfig , config => {
4463 let { contents } = config . modResults ;
4564
@@ -59,3 +78,11 @@ export const withAppAuthAppDelegate: ConfigPlugin = rootConfig => {
5978 return config ;
6079 } ) ;
6180} ;
81+
82+ export const withAppAuthAppDelegate : ConfigPlugin = rootConfig => {
83+ if ( isExpo53OrLater ( rootConfig ) ) {
84+ return withAppDelegateSwift ( rootConfig ) ;
85+ }
86+
87+ return withLegacyAppAuthAppDelegate ( rootConfig ) ;
88+ } ;
0 commit comments