UNPKG

6.61 kBSource Map (JSON)View Raw
1{"version":3,"names":["findMatchingSimulator","simulators","findOptions","devices","simulatorVersion","simulatorName","simulator","parsedSimulatorName","match","undefined","fallbackMatch","versionDescriptor","device","version","test","replace","includes","endsWith","i","availability","isAvailable","booted","state","lastBootedAt","simulatorDescriptor","udid","name"],"sources":["../../src/tools/findMatchingSimulator.ts"],"sourcesContent":["/**\n * Copyright (c) Facebook, Inc. and its affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\nimport {Device} from '../types';\n\n/**\n * Takes in a parsed simulator list and a desired name, and returns an object with the matching simulator. The desired\n * name can optionally include the iOS version in between parenthesis after the device name. Ex: \"iPhone 6 (9.2)\" in\n * which case it'll attempt to find a simulator with the exact version specified.\n *\n * If the simulatorString argument is null, we'll go into default mode and return the currently booted simulator,\n * the last booted simulator or\n * if none is booted, it will be the first in the list.\n *\n * @param simulators a parsed list from `xcrun simctl list --json devices` command\n * @param findOptions null or an object containing:\n * ```\n * {\n * simulator: name of desired simulator\n * udid: udid of desired simulator\n * }\n * ```\n * If null, it will use the currently booted simulator, or if none are booted, the first in the list.\n */\nfunction findMatchingSimulator(\n simulators: {devices: {[index: string]: Array<Device>}},\n findOptions?: null | {simulator?: string; udid?: string},\n) {\n if (!simulators.devices) {\n return null;\n }\n const devices = simulators.devices;\n let simulatorVersion;\n let simulatorName = null;\n\n if (findOptions && findOptions.simulator) {\n const parsedSimulatorName = findOptions.simulator.match(\n /(.*)? (?:\\((\\d+\\.\\d+)?\\))$/,\n );\n if (parsedSimulatorName && parsedSimulatorName[2] !== undefined) {\n simulatorVersion = parsedSimulatorName[2];\n simulatorName = parsedSimulatorName[1];\n } else {\n simulatorName = findOptions.simulator;\n }\n }\n\n let match;\n let fallbackMatch;\n\n for (const versionDescriptor in devices) {\n const device = devices[versionDescriptor];\n let version = versionDescriptor;\n\n if (/^com\\.apple\\.CoreSimulator\\.SimRuntime\\./g.test(version)) {\n // Transform \"com.apple.CoreSimulator.SimRuntime.iOS-12-2\" into \"iOS 12.2\"\n version = version.replace(\n /^com\\.apple\\.CoreSimulator\\.SimRuntime\\.([^-]+)-([^-]+)-([^-]+)$/g,\n '$1 $2.$3',\n );\n }\n\n // Making sure the version of the simulator is an iOS or tvOS (Removes Apple Watch, etc)\n if (!version.includes('iOS') && !version.includes('tvOS')) {\n continue;\n }\n if (simulatorVersion && !version.endsWith(simulatorVersion)) {\n continue;\n }\n for (const i in device) {\n const simulator = device[i];\n // Skipping non-available simulator\n if (\n simulator.availability !== '(available)' &&\n // @ts-ignore verify isAvailable parameter\n simulator.isAvailable !== 'YES' &&\n simulator.isAvailable !== true\n ) {\n continue;\n }\n const booted = simulator.state === 'Booted';\n const lastBootedAt = simulator.lastBootedAt;\n const simulatorDescriptor = {\n udid: simulator.udid,\n name: simulator.name,\n booted,\n version,\n };\n if (findOptions && findOptions.udid) {\n if (simulator.udid === findOptions.udid) {\n return simulatorDescriptor;\n }\n } else {\n if (booted && simulatorName === null) {\n return simulatorDescriptor;\n }\n if (simulator.name === simulatorName && !match) {\n match = simulatorDescriptor;\n }\n // If no match found, use first available simulator that was booted before\n if (!!lastBootedAt && !match) {\n fallbackMatch = simulatorDescriptor;\n }\n // Keeps track of the first available simulator for use if we can't find one above.\n if (simulatorName === null && !match) {\n match = simulatorDescriptor;\n }\n }\n }\n }\n\n return match ?? fallbackMatch ?? null;\n}\n\nexport default findMatchingSimulator;\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,qBAAqB,CAC5BC,UAAuD,EACvDC,WAAwD,EACxD;EACA,IAAI,CAACD,UAAU,CAACE,OAAO,EAAE;IACvB,OAAO,IAAI;EACb;EACA,MAAMA,OAAO,GAAGF,UAAU,CAACE,OAAO;EAClC,IAAIC,gBAAgB;E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