Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

listEmulatorBuildTargets.js 4.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. const { superspawn: { spawn } } = require('cordova-common');
  18. /**
  19. * Returns a list of available simulator build targets of the form
  20. *
  21. * [
  22. * { name: <xcode-destination-name>,
  23. * identifier: <simctl-identifier>,
  24. * simIdentifier: <cordova emulate target>
  25. * }
  26. * ]
  27. *
  28. */
  29. function listEmulatorBuildTargets () {
  30. return spawn('xcrun', ['simctl', 'list', '--json'])
  31. .then(output => JSON.parse(output))
  32. .then(function (simInfo) {
  33. var devices = simInfo.devices;
  34. var deviceTypes = simInfo.devicetypes;
  35. return deviceTypes.reduce(function (typeAcc, deviceType) {
  36. if (!deviceType.name.match(/^[iPad|iPhone]/)) {
  37. // ignore targets we don't support (like Apple Watch or Apple TV)
  38. return typeAcc;
  39. }
  40. var availableDevices = Object.keys(devices).reduce(function (availAcc, deviceCategory) {
  41. var availableDevicesInCategory = devices[deviceCategory];
  42. availableDevicesInCategory.forEach(function (device) {
  43. if (device.name === deviceType.name || device.name === deviceType.name.replace(/-inch/g, ' inch')) {
  44. // Check new flag isAvailable (XCode 10.1+) or legacy string availability (XCode 10 and lower)
  45. if (device.isAvailable || (device.availability && device.availability.toLowerCase().indexOf('unavailable') < 0)) {
  46. availAcc.push(device);
  47. }
  48. }
  49. });
  50. return availAcc;
  51. }, []);
  52. // we only want device types that have at least one available device
  53. // (regardless of OS); this filters things out like iPhone 4s, which
  54. // is present in deviceTypes, but probably not available on the user's
  55. // system.
  56. if (availableDevices.length > 0) {
  57. typeAcc.push(deviceType);
  58. }
  59. return typeAcc;
  60. }, []);
  61. })
  62. .then(function (filteredTargets) {
  63. // the simIdentifier, or cordova emulate target name, is the very last part
  64. // of identifier.
  65. return filteredTargets.map(function (target) {
  66. var identifierPieces = target.identifier.split('.');
  67. target.simIdentifier = identifierPieces[identifierPieces.length - 1];
  68. return target;
  69. });
  70. });
  71. }
  72. exports.run = listEmulatorBuildTargets;
  73. /**
  74. * Given a simIdentifier, return the matching target.
  75. *
  76. * @param {string} simIdentifier a target, like "iPhone-SE"
  77. * @return {Object} the matching target, or undefined if no match
  78. */
  79. exports.targetForSimIdentifier = function (simIdentifier) {
  80. return listEmulatorBuildTargets()
  81. .then(function (targets) {
  82. return targets.reduce(function (acc, target) {
  83. if (!acc && target.simIdentifier.toLowerCase() === simIdentifier.toLowerCase()) {
  84. acc = target;
  85. }
  86. return acc;
  87. }, undefined);
  88. });
  89. };