|
| 1 | +const { AndroidConfig, withDangerousMod } = require('@expo/config-plugins'); |
| 2 | +const { |
| 3 | + createGeneratedHeaderComment, |
| 4 | + removeContents, |
| 5 | +} = require('@expo/config-plugins/build/utils/generateCode'); |
| 6 | +const codeModAndroid = require('@expo/config-plugins/build/android/codeMod'); |
| 7 | +const fs = require('fs'); |
| 8 | + |
| 9 | +const withAppAuthAppBuildGradle = (rootConfig, options) => |
| 10 | + withDangerousMod(rootConfig, [ |
| 11 | + 'android', |
| 12 | + config => { |
| 13 | + // detauls to app scheme |
| 14 | + const authScheme = options?.authScheme ?? config.scheme ?? ''; |
| 15 | + |
| 16 | + // find the app/build.gradle file and checks its format |
| 17 | + const appBuildGradlePath = AndroidConfig.Paths.getAppBuildGradleFilePath( |
| 18 | + config.modRequest.projectRoot |
| 19 | + ); |
| 20 | + |
| 21 | + // BEWARE: we update the app/build.gradle file *outside* of the standard Expo config procedure ! |
| 22 | + let contents = fs.readFileSync(appBuildGradlePath, 'utf-8'); |
| 23 | + |
| 24 | + if (contents.includes('manifestPlaceholders')) { |
| 25 | + throw new Error( |
| 26 | + 'app/build.gradle already contains manifestPlaceholders, cannot update automatically !' |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + // let's add the manifestPlaceholders section ! |
| 31 | + contents = removeContents({ |
| 32 | + src: contents, |
| 33 | + tag: 'react-native-app-auth', |
| 34 | + }).contents; |
| 35 | + contents = codeModAndroid.appendContentsInsideDeclarationBlock( |
| 36 | + contents, |
| 37 | + 'defaultConfig', |
| 38 | + ` |
| 39 | + ${createGeneratedHeaderComment(contents, 'react-native-app-auth', '//')} |
| 40 | + manifestPlaceholders = [ |
| 41 | + 'appAuthRedirectScheme': '${authScheme}', |
| 42 | + ] |
| 43 | + // @generated end react-native-app-auth |
| 44 | +` |
| 45 | + ); |
| 46 | + |
| 47 | + // and finally we write the file back to the disk |
| 48 | + fs.writeFileSync(appBuildGradlePath, contents, 'utf-8'); |
| 49 | + |
| 50 | + return config; |
| 51 | + }, |
| 52 | + ]); |
| 53 | + |
| 54 | +module.exports = { withAppAuthAppBuildGradle }; |
0 commit comments