/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */ import { Device } from '../../types' ; param simulators param simulatorString function findMatchingSimulator ( simulators , findOptions ? ) { if ( ! simulators . devices ) { return null ; } const devices = simulators . devices ; let simulatorVersion ; let simulatorName = null ; if ( findOptions && findOptions . simulator ) { const parsedSimulatorName = findOptions . simulator . match ( /(.*)? (?:\((\d+\.\d+)?\))$/ ) ; if ( parsedSimulatorName && parsedSimulatorName [ 2 ] !== undefined ) { simulatorVersion = parsedSimulatorName [ 2 ] ; simulatorName = parsedSimulatorName [ 1 ] ; } else { simulatorName = findOptions . simulator ; } } let match ; for ( const versionDescriptor in devices ) { const device = devices [ versionDescriptor ] ; let version = versionDescriptor ; if ( /^com\.apple\.CoreSimulator\.SimRuntime\./g . test ( version ) ) { version = version . replace ( /^com\.apple\.CoreSimulator\.SimRuntime\.([^-]+)-([^-]+)-([^-]+)$/g , '$1 $2.$3' ) ; } if ( ! version . includes ( 'iOS' ) && ! version . includes ( 'tvOS' ) ) { continue ; } if ( simulatorVersion && ! version . endsWith ( simulatorVersion ) ) { continue ; } for ( const i in device ) { const simulator = device [ i ] ; if ( simulator . availability !== '(available)' && simulator . isAvailable !== 'YES' && simulator . isAvailable !== true ) { continue ; } const booted = simulator . state === 'Booted' ; const simulatorDescriptor = { udid : simulator . udid , name : simulator . name , booted , version , } ; if ( findOptions && findOptions . udid ) { if ( simulator . udid === findOptions . udid ) { return simulatorDescriptor ; } } else { if ( booted && simulatorName === null ) { return simulatorDescriptor ; } if ( simulator . name === simulatorName && ! match ) { match = simulatorDescriptor ; } if ( simulatorName === null && ! match ) { match = simulatorDescriptor ; } } } } if ( match ) { return match ; } return null ; } export default findMatchingSimulator ;