2023-09-26 03:39:17 -06:00
import { NativeModules , Platform } from 'react-native'
import { CameraRuntimeError } from './CameraError'
2023-03-13 07:21:08 -06:00
2023-09-26 03:39:17 -06:00
const supportedPlatforms = [ 'ios' , 'android' , 'macos' ]
2023-03-13 07:21:08 -06:00
// NativeModules automatically resolves 'CameraView' to 'CameraViewModule'
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2023-09-26 03:39:17 -06:00
export const CameraModule = NativeModules . CameraView
2023-03-13 07:21:08 -06:00
if ( CameraModule == null ) {
if ( ! supportedPlatforms . includes ( Platform . OS ) ) {
throw new CameraRuntimeError (
'system/camera-module-not-found' ,
` Failed to initialize VisionCamera: VisionCamera currently does not work on ${ Platform . OS } . ` ,
2023-09-26 03:39:17 -06:00
)
2023-03-13 07:21:08 -06:00
}
2023-09-26 03:39:17 -06:00
let message = 'Failed to initialize VisionCamera: The native Camera Module (`NativeModules.CameraView`) could not be found.'
message += '\n* Make sure react-native-vision-camera is correctly autolinked (run `npx react-native config` to verify)'
if ( Platform . OS === 'ios' || Platform . OS === 'macos' ) message += '\n* Make sure you ran `pod install` in the ios/ directory.'
2023-03-13 07:21:08 -06:00
2023-09-26 03:39:17 -06:00
if ( Platform . OS === 'android' ) message += '\n* Make sure gradle is synced.'
2023-03-13 07:21:08 -06:00
// check if Expo
2023-03-15 07:34:15 -06:00
// @ts-expect-error expo global JSI modules are not typed
2023-03-13 07:21:08 -06:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2023-09-26 03:39:17 -06:00
const ExpoConstants = global . expo ? . module s?.ExponentConstants
2023-03-13 07:21:08 -06:00
if ( ExpoConstants != null ) {
if ( ExpoConstants . appOwnership === 'expo' ) {
// We're running Expo Go
throw new CameraRuntimeError (
'system/camera-module-not-found' ,
` react-native-vision-camera is not supported in Expo Go! Use EAS/expo prebuild instead ( \` expo run: ${ Platform . OS } \` ). For more info, see https://docs.expo.dev/workflow/prebuild/. ` ,
2023-09-26 03:39:17 -06:00
)
2023-03-13 07:21:08 -06:00
} else {
// We're running Expo bare / standalone
2023-09-26 03:39:17 -06:00
message += '\n* Make sure you ran `expo prebuild`.'
2023-03-13 07:21:08 -06:00
}
}
2023-09-26 03:39:17 -06:00
message += '\n* Make sure you rebuilt the app.'
throw new CameraRuntimeError ( 'system/camera-module-not-found' , message )
2023-03-13 07:21:08 -06:00
}