diff options
| author | Kumar Priyansh <[email protected]> | 2019-01-19 12:37:14 +0530 |
|---|---|---|
| committer | Kumar Priyansh <[email protected]> | 2019-01-19 12:37:14 +0530 |
| commit | dcdfc94cb39dfe2c39925a0145ffa45e2d061c30 (patch) | |
| tree | 4f6379d955555b298c0e7b83a67e264240ee5614 /cordova/lib/list-emulator-build-targets | |
| parent | 76f7b3678d3f1ff99c3935a774d420453b0c3cb9 (diff) | |
| download | WeatherApp-dcdfc94cb39dfe2c39925a0145ffa45e2d061c30.tar.xz WeatherApp-dcdfc94cb39dfe2c39925a0145ffa45e2d061c30.zip | |
Initial Upload via GIT
Diffstat (limited to 'cordova/lib/list-emulator-build-targets')
| -rwxr-xr-x | cordova/lib/list-emulator-build-targets | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/cordova/lib/list-emulator-build-targets b/cordova/lib/list-emulator-build-targets new file mode 100755 index 0000000..c0d566f --- /dev/null +++ b/cordova/lib/list-emulator-build-targets @@ -0,0 +1,107 @@ +#!/usr/bin/env node + +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + + +var Q = require('q'), + exec = require('child_process').exec; + +/** + * Returns a list of available simulator build targets of the form + * + * [ + * { name: <xcode-destination-name>, + * identifier: <simctl-identifier>, + * simIdentifier: <cordova emulate target> + * } + * ] + * + */ +function listEmulatorBuildTargets () { + return Q.nfcall(exec, 'xcrun simctl list --json') + .then(function(stdio) { + return JSON.parse(stdio[0]); + }) + .then(function(simInfo) { + var devices = simInfo.devices; + var deviceTypes = simInfo.devicetypes; + return deviceTypes.reduce(function (typeAcc, deviceType) { + if (!deviceType.name.match(/^[iPad|iPhone]/)) { + // ignore targets we don't support (like Apple Watch or Apple TV) + return typeAcc; + } + var availableDevices = Object.keys(devices).reduce(function (availAcc, deviceCategory) { + var availableDevicesInCategory = devices[deviceCategory]; + availableDevicesInCategory.forEach(function (device) { + if (device.name === deviceType.name.replace(/\-inch/g, ' inch') && + device.availability.toLowerCase().indexOf('unavailable') < 0) { + availAcc.push(device); + } + }); + return availAcc; + }, []); + // we only want device types that have at least one available device + // (regardless of OS); this filters things out like iPhone 4s, which + // is present in deviceTypes, but probably not available on the user's + // system. + if (availableDevices.length > 0) { + typeAcc.push(deviceType); + } + return typeAcc; + }, []); + }) + .then(function(filteredTargets) { + // the simIdentifier, or cordova emulate target name, is the very last part + // of identifier. + return filteredTargets.map(function (target) { + var identifierPieces = target.identifier.split("."); + target.simIdentifier = identifierPieces[identifierPieces.length-1]; + return target; + }); + }); +} + +exports.run = listEmulatorBuildTargets; + +/** + * Given a simIdentifier, return the matching target. + * + * @param {string} simIdentifier a target, like "iPhone-SE" + * @return {Object} the matching target, or undefined if no match + */ +exports.targetForSimIdentifier = function(simIdentifier) { + return listEmulatorBuildTargets() + .then(function(targets) { + return targets.reduce(function(acc, target) { + if (!acc && target.simIdentifier.toLowerCase() === simIdentifier.toLowerCase()) { + acc = target; + } + return acc; + }, undefined); + }); +} + +// Check if module is started as separate script. +// If so, then invoke main method and print out results. +if (!module.parent) { + listEmulatorBuildTargets().then(function (targets) { + console.log(JSON.stringify(targets, null, 2)); + }); +} |
