|
| 1 | +const { IOSConfig, withDangerousMod } = require('@expo/config-plugins'); |
| 2 | +const codeModIOs = require('@expo/config-plugins/build/ios/codeMod'); |
| 3 | +const { |
| 4 | + createGeneratedHeaderComment, |
| 5 | + removeContents, |
| 6 | +} = require('@expo/config-plugins/build/utils/generateCode'); |
| 7 | +const fs = require('fs'); |
| 8 | +const { insertProtocolDeclaration } = require('./utils/insert-protocol-declaration'); |
| 9 | + |
| 10 | +const withAppAuthAppDelegateHeader = rootConfig => |
| 11 | + withDangerousMod(rootConfig, [ |
| 12 | + 'ios', |
| 13 | + config => { |
| 14 | + // find the AppDelegate.h file in the project |
| 15 | + const headerFilePath = IOSConfig.Paths.getAppDelegateObjcHeaderFilePath( |
| 16 | + config.modRequest.projectRoot |
| 17 | + ); |
| 18 | + |
| 19 | + // BEWARE: we update the AppDelegate.h file *outside* of the standard Expo config procedure ! |
| 20 | + let contents = fs.readFileSync(headerFilePath, 'utf-8'); |
| 21 | + |
| 22 | + // add a new import (unless it already exists) |
| 23 | + contents = codeModIOs.addObjcImports(contents, ['"RNAppAuthAuthorizationFlowManager.h"']); |
| 24 | + |
| 25 | + // adds a new protocol to the AppDelegate interface (unless it already exists) |
| 26 | + contents = insertProtocolDeclaration({ |
| 27 | + source: contents, |
| 28 | + interfaceName: 'AppDelegate', |
| 29 | + protocolName: 'RNAppAuthAuthorizationFlowManager', |
| 30 | + baseClassName: 'EXAppDelegateWrapper', |
| 31 | + }); |
| 32 | + |
| 33 | + // add a new property to the AppDelegate interface (unless it already exists) |
| 34 | + contents = removeContents({ |
| 35 | + src: contents, |
| 36 | + tag: 'react-native-app-auth', |
| 37 | + }).contents; |
| 38 | + contents = codeModIOs.insertContentsInsideObjcInterfaceBlock( |
| 39 | + contents, |
| 40 | + '@interface AppDelegate', |
| 41 | + ` |
| 42 | +${createGeneratedHeaderComment(contents, 'react-native-app-auth', '//')} |
| 43 | +@property(nonatomic, weak) id<RNAppAuthAuthorizationFlowManagerDelegate> authorizationFlowManagerDelegate; |
| 44 | +// @generated end react-native-app-auth`, |
| 45 | + { position: 'head' } |
| 46 | + ); |
| 47 | + |
| 48 | + // and finally we write the file back to the disk |
| 49 | + fs.writeFileSync(headerFilePath, contents, 'utf-8'); |
| 50 | + |
| 51 | + return config; |
| 52 | + }, |
| 53 | + ]); |
| 54 | + |
| 55 | +module.exports = { |
| 56 | + withAppAuthAppDelegateHeader, |
| 57 | +}; |
0 commit comments