function listEmulatorBuildTargets()

in lib/listEmulatorBuildTargets.js [33:75]


function listEmulatorBuildTargets () {
    return execa('xcrun', ['simctl', 'list', '--json'])
        .then(({ stdout }) => JSON.parse(stdout))
        .then(function (simInfo) {
            const devices = simInfo.devices;
            const 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;
                }
                const availableDevices = Object.keys(devices).reduce(function (availAcc, deviceCategory) {
                    const availableDevicesInCategory = devices[deviceCategory];
                    availableDevicesInCategory.forEach(function (device) {
                        if (device.name === deviceType.name || device.name === deviceType.name.replace(/-inch/g, ' inch')) {
                        // Check new flag isAvailable (XCode 10.1+) or legacy string availability (XCode 10 and lower)
                            if (device.isAvailable || (device.availability && 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) {
                const identifierPieces = target.identifier.split('.');
                target.simIdentifier = identifierPieces[identifierPieces.length - 1];
                return target;
            });
        });
}