|
| 1 | +import {ConfigPlugin, withDangerousMod} from '@expo/config-plugins'; |
| 2 | +import {mergeContents} from '@expo/config-plugins/build/utils/generateCode'; |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +type Props = { |
| 8 | + iosPermissions?: Array< |
| 9 | + | 'AppTrackingTransparency' |
| 10 | + | 'Bluetooth' |
| 11 | + | 'Calendars' |
| 12 | + | 'CalendarsWriteOnly' |
| 13 | + | 'Camera' |
| 14 | + | 'Contacts' |
| 15 | + | 'FaceID' |
| 16 | + | 'LocationAccuracy' |
| 17 | + | 'LocationAlways' |
| 18 | + | 'LocationWhileInUse' |
| 19 | + | 'MediaLibrary' |
| 20 | + | 'Microphone' |
| 21 | + | 'Motion' |
| 22 | + | 'Notifications' |
| 23 | + | 'PhotoLibrary' |
| 24 | + | 'PhotoLibraryAddOnly' |
| 25 | + | 'Reminders' |
| 26 | + | 'Siri' |
| 27 | + | 'SpeechRecognition' |
| 28 | + | 'StoreKit' |
| 29 | + >; |
| 30 | +}; |
| 31 | + |
| 32 | +const plugin: ConfigPlugin<Props> = (config, {iosPermissions}) => { |
| 33 | + if (iosPermissions == null || iosPermissions.length === 0) { |
| 34 | + return config; |
| 35 | + } |
| 36 | + |
| 37 | + return withDangerousMod(config, [ |
| 38 | + 'ios', |
| 39 | + async (config) => { |
| 40 | + const file = path.join(config.modRequest.platformProjectRoot, 'Podfile'); |
| 41 | + let contents = await fs.promises.readFile(file, 'utf8'); |
| 42 | + |
| 43 | + contents = mergeContents({ |
| 44 | + tag: 'node-require-function', |
| 45 | + src: contents, |
| 46 | + newSrc: `def node_require(script)\n\t# Resolve script with node to allow for hoisting\n\trequire Pod::Executable.execute_command('node', ['-p',\n\t\t"require.resolve(\n\t\t\t'#{script}',\n\t\t\t {paths: [process.argv[1]]},\n\t\t)", __dir__]).strip\nend\n\nnode_require('react-native-permissions/scripts/setup.rb')`, |
| 47 | + anchor: /scripts\/react_native_pods/, |
| 48 | + offset: 1, |
| 49 | + comment: '#', |
| 50 | + }).contents; |
| 51 | + |
| 52 | + contents = mergeContents({ |
| 53 | + tag: 'permissions-array', |
| 54 | + src: contents, |
| 55 | + newSrc: `setup_permissions([${iosPermissions |
| 56 | + .map((permission) => `'${permission}'`) |
| 57 | + .join(',')}])`, |
| 58 | + anchor: /prepare_react_native_project!/, |
| 59 | + offset: 1, |
| 60 | + comment: '#', |
| 61 | + }).contents; |
| 62 | + |
| 63 | + await fs.promises.writeFile(file, contents, 'utf-8'); |
| 64 | + return config; |
| 65 | + }, |
| 66 | + ]); |
| 67 | +}; |
| 68 | + |
| 69 | +export default plugin; |
0 commit comments