aboutsummaryrefslogtreecommitdiff
path: root/cordova/lib/list-emulator-build-targets
blob: c0d566fb5d8dd62afe8c74e68d0caf347e5d7b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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));
    });
}