import { ExpoConfig } from '@expo/config-types' ; import { createInfoPlistPlugin } from '../plugins/ios-plugins' ; import { InfoPlist , URLScheme } from './IosConfig.types' ; export const withScheme = createInfoPlistPlugin ( setScheme , 'withScheme' ) ; export function getScheme ( config ) { if ( Array . isArray ( config . scheme ) ) { const validate = ( value ) : value is string => { return typeof value === 'string' ; } ; return config . scheme . filter < string > ( validate ) ; } else if ( typeof config . scheme === 'string' ) { return [ config . scheme ] ; } return [ ] ; } export function setScheme ( config , infoPlist ) { const scheme = [ ... getScheme ( config ) , ... getScheme ( config . ios ?? { } ) , ] ; if ( config . ios ?. bundleIdentifier ) { scheme . push ( config . ios . bundleIdentifier ) ; } if ( scheme . length === 0 ) { return infoPlist ; } return { ... infoPlist , CFBundleURLTypes : [ { CFBundleURLSchemes : scheme } ] } ; } export function appendScheme ( scheme , infoPlist ) { if ( ! scheme ) { return infoPlist ; } const existingSchemes = infoPlist . CFBundleURLTypes ; return { ... infoPlist , CFBundleURLTypes : [ ... ( existingSchemes ?? [ ] ) , { CFBundleURLSchemes : [ scheme ] } , ] } ; } export function removeScheme ( scheme , infoPlist ) { if ( ! scheme ) { return infoPlist ; } if ( ! infoPlist . CFBundleURLTypes ) { return infoPlist ; } infoPlist . CFBundleURLTypes = infoPlist . CFBundleURLTypes . map ( bundleUrlType => { const index = bundleUrlType . CFBundleURLSchemes . indexOf ( scheme ) ; if ( index > - 1 ) { bundleUrlType . CFBundleURLSchemes . splice ( index , 1 ) ; if ( bundleUrlType . CFBundleURLSchemes . length === 0 ) { return undefined ; } } return bundleUrlType ; } ) . filter ( Boolean ) as URLScheme [ ] ; return infoPlist ; } export function hasScheme ( scheme , infoPlist ) { const existingSchemes = infoPlist . CFBundleURLTypes ; if ( ! Array . isArray ( existingSchemes ) ) return false ; return existingSchemes . some ( ( { CFBundleURLSchemes : schemes } ) => Array . isArray ( schemes ) ? schemes . includes ( scheme ) : false ) ; } export function getSchemesFromPlist ( infoPlist ) { if ( Array . isArray ( infoPlist . CFBundleURLTypes ) ) { return infoPlist . CFBundleURLTypes . reduce < string [ ] > ( ( schemes , { CFBundleURLSchemes } ) => { if ( Array . isArray ( CFBundleURLSchemes ) ) { return [ ... schemes , ... CFBundleURLSchemes ] ; } return schemes ; } , [ ] ) ; } return [ ] ; }