feat(Adb): list devices _and_ emulators in one go (#1125)

This commit is contained in:
Raphael von der Grün
2020-11-19 21:30:56 +01:00
committed by GitHub
parent 0e8234abfd
commit aa679ea1d6
6 changed files with 42 additions and 70 deletions
+15 -21
View File
@@ -24,33 +24,27 @@ var CordovaError = require('cordova-common').CordovaError;
var Adb = {};
function isDevice (line) {
return line.match(/\w+\tdevice/) && !line.match(/emulator/);
}
function isEmulator (line) {
return line.match(/device/) && line.match(/emulator/);
}
/**
* Lists available/connected devices and emulators
*
* @param {Object} opts Various options
* @param {Boolean} opts.emulators Specifies whether this method returns
* emulators only
*
* @return {Promise<String[]>} list of available/connected
* devices/emulators
*/
Adb.devices = function (opts) {
return execa('adb', ['devices'], { cwd: os.tmpdir() }).then(({ stdout: output }) => {
return output.split('\n').filter(function (line) {
// Filter out either real devices or emulators, depending on options
return (line && opts && opts.emulators) ? isEmulator(line) : isDevice(line);
}).map(function (line) {
return line.replace(/\tdevice/, '').replace('\r', '');
});
});
Adb.devices = async function () {
const { stdout } = await execa('adb', ['devices'], { cwd: os.tmpdir() });
// Split into lines & drop first one (header)
const rawDeviceLines = stdout.trim().split(/\r?\n/).slice(1);
return rawDeviceLines
.map(line => line.split('\t'))
// We are only interested in fully booted devices & emulators. These
// have a state of `device`. For a list of all the other possible states
// see https://github.com/aosp-mirror/platform_system_core/blob/2abdb1eb5b83c8f39874644af576c869815f5c5b/adb/transport.cpp#L1129
.filter(([, state]) => state === 'device')
.map(([id]) => id);
};
Adb.install = function (target, packagePath, { replace = false, execOptions = {} } = {}) {
+3 -2
View File
@@ -27,8 +27,9 @@ var events = require('cordova-common').events;
/**
* Returns a promise for the list of the device ID's found
*/
module.exports.list = function () {
return Adb.devices();
module.exports.list = async () => {
return (await Adb.devices())
.filter(id => !id.startsWith('emulator-'));
};
module.exports.resolveTarget = function (target) {
+3 -3
View File
@@ -214,9 +214,9 @@ module.exports.best_image = function () {
});
};
// Returns a promise.
module.exports.list_started = function () {
return Adb.devices({ emulators: true });
exports.list_started = async () => {
return (await Adb.devices())
.filter(id => id.startsWith('emulator-'));
};
/*