Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8.  
  9. /**
  10. * Takes in a parsed simulator list and a desired name, and returns an object with the matching simulator.
  11. *
  12. * If the simulatorName argument is null, we'll go into default mode and return the currently booted simulator, or if
  13. * none is booted, it will be the first in the list.
  14. *
  15. * @param Object simulators a parsed list from `xcrun simctl list --json devices` command
  16. * @param String|null simulatorName the string with the name of desired simulator. If null, it will use the currently
  17. * booted simulator, or if none are booted, the first in the list.
  18. * @returns {Object} {udid, name, version}
  19. */
  20. function findMatchingSimulator(simulators, simulatorName) {
  21. if (!simulators.devices) {
  22. return null;
  23. }
  24. const devices = simulators.devices;
  25. var match;
  26. for (let version in devices) {
  27. // Making sure the version of the simulator is an iOS or tvOS (Removes Apple Watch, etc)
  28. if (!version.startsWith('iOS') && !version.startsWith('tvOS')) {
  29. continue;
  30. }
  31. for (let i in devices[version]) {
  32. let simulator = devices[version][i];
  33. // Skipping non-available simulator
  34. if (simulator.availability !== '(available)') {
  35. continue;
  36. }
  37. let booted = simulator.state === 'Booted';
  38. if (booted && simulatorName === null) {
  39. return {
  40. udid: simulator.udid,
  41. name: simulator.name,
  42. booted,
  43. version
  44. };
  45. }
  46. if (simulator.name === simulatorName && !match) {
  47. match = {
  48. udid: simulator.udid,
  49. name: simulator.name,
  50. booted,
  51. version
  52. };
  53. }
  54. // Keeps track of the first available simulator for use if we can't find one above.
  55. if (simulatorName === null && !match) {
  56. match = {
  57. udid: simulator.udid,
  58. name: simulator.name,
  59. booted,
  60. version
  61. };
  62. }
  63. }
  64. }
  65. if (match) {
  66. return match;
  67. }
  68. return null;
  69. }
  70.  
  71. module.exports = findMatchingSimulator;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement