Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

list-emulator-build-targets 4.1KB

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