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
2114 }
22- }
23- ${ replaceText2 } `
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+ if ( conformances . includes ( APP_AUTH_PROTOCOL ) ) {
22+ return match ;
23+ }
24+
25+ const trailingWhitespace = conformances . match ( / \s * $ / ) ?. [ 0 ] ?? '' ;
26+ const existingConformances = conformances . slice (
27+ 0 ,
28+ conformances . length - trailingWhitespace . length
2429 ) ;
2530
26- const replaceText3 = 'var reactNativeFactory: RCTReactNativeFactory?' ;
31+ return `${ declaration } ${ existingConformances } , ${ APP_AUTH_PROTOCOL } ${ trailingWhitespace } ${ openingBrace } ` ;
32+ }
33+ ) ;
34+
35+ if ( ! APP_AUTH_DELEGATE_PROPERTY_PATTERN . test ( contents ) ) {
36+ const reactNativeFactoryPattern =
37+ / ^ ( \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;
38+ const factoryMatch = contents . match ( reactNativeFactoryPattern ) ;
39+ if ( factoryMatch ) {
40+ const indent = factoryMatch [ 1 ] ;
2741 contents = contents . replace (
28- replaceText3 ,
29- `${ replaceText3 } \n\n public weak var authorizationFlowManagerDelegate: RNAppAuthAuthorizationFlowManagerDelegate? `
42+ reactNativeFactoryPattern ,
43+ match => `${ match } \n\n${ indent } ${ APP_AUTH_DELEGATE_PROPERTY } `
3044 ) ;
3145 }
46+ }
3247
33- config . modResults . contents = contents ;
48+ if ( ! contents . includes ( 'resumeExternalUserAgentFlow(with: url)' ) ) {
49+ contents = contents . replace (
50+ / ( (?: 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,
51+ match => `${ match } \n ${ APP_AUTH_RESUME_BLOCK } \n`
52+ ) ;
53+ }
54+
55+ return contents ;
56+ } ;
57+
58+ const withAppDelegateSwift : ConfigPlugin = rootConfig => {
59+ return withAppDelegate ( rootConfig , config => {
60+ assertExpo53OrLater ( config , config . modRequest . projectRoot ) ;
61+ config . modResults . contents = applyExpo53AppDelegatePatch ( config . modResults . contents ) ;
3462 return config ;
3563 } ) ;
3664} ;
3765
38- export const withAppAuthAppDelegate : ConfigPlugin = rootConfig => {
39- if ( isExpo53OrLater ( rootConfig ) ) {
40- return withAppDelegateSwift ( rootConfig ) ;
41- }
42-
66+ export const withLegacyAppAuthAppDelegate : ConfigPlugin = rootConfig => {
4367 return withAppDelegate ( rootConfig , config => {
4468 let { contents } = config . modResults ;
4569
@@ -59,3 +83,11 @@ export const withAppAuthAppDelegate: ConfigPlugin = rootConfig => {
5983 return config ;
6084 } ) ;
6185} ;
86+
87+ export const withAppAuthAppDelegate : ConfigPlugin = rootConfig => {
88+ if ( isExpo53OrLater ( rootConfig ) ) {
89+ return withAppDelegateSwift ( rootConfig ) ;
90+ }
91+
92+ return withLegacyAppAuthAppDelegate ( rootConfig ) ;
93+ } ;
0 commit comments