diff --git a/bin/templates/cordova/lib/Adb.js b/bin/templates/cordova/lib/Adb.js index a78a6ab4..93262698 100644 --- a/bin/templates/cordova/lib/Adb.js +++ b/bin/templates/cordova/lib/Adb.js @@ -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} 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 = {} } = {}) { diff --git a/bin/templates/cordova/lib/device.js b/bin/templates/cordova/lib/device.js index dedaf717..ede9151d 100644 --- a/bin/templates/cordova/lib/device.js +++ b/bin/templates/cordova/lib/device.js @@ -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) { diff --git a/bin/templates/cordova/lib/emulator.js b/bin/templates/cordova/lib/emulator.js index e7b24919..3ae02883 100644 --- a/bin/templates/cordova/lib/emulator.js +++ b/bin/templates/cordova/lib/emulator.js @@ -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-')); }; /* diff --git a/spec/unit/Adb.spec.js b/spec/unit/Adb.spec.js index ac681e48..aafe9778 100644 --- a/spec/unit/Adb.spec.js +++ b/spec/unit/Adb.spec.js @@ -20,13 +20,15 @@ const CordovaError = require('cordova-common').CordovaError; const rewire = require('rewire'); -describe('Adb', () => { - const adbOutput = `List of devices attached +const adbOutput = `List of devices attached emulator-5554\tdevice -123a76565509e124\tdevice`; - const [, emulatorLine, deviceLine] = adbOutput.split('\n'); - const emulatorId = emulatorLine.split('\t')[0]; - const deviceId = deviceLine.split('\t')[0]; +emulator-5556\toffline +123a76565509e124\tdevice +123a76565509e123\tbootloader +`; + +describe('Adb', () => { + const deviceId = '123a76565509e124'; const alreadyExistsError = 'adb: failed to install app.apk: Failure[INSTALL_FAILED_ALREADY_EXISTS]'; const certificateError = 'adb: failed to install app.apk: Failure[INSTALL_PARSE_FAILED_NO_CERTIFICATES]'; @@ -41,40 +43,15 @@ emulator-5554\tdevice Adb.__set__('execa', execaSpy); }); - describe('isDevice', () => { - it('should return true for a real device', () => { - const isDevice = Adb.__get__('isDevice'); - - expect(isDevice(deviceLine)).toBeTruthy(); - expect(isDevice(emulatorLine)).toBeFalsy(); - }); - }); - - describe('isEmulator', () => { - it('should return true for an emulator', () => { - const isEmulator = Adb.__get__('isEmulator'); - - expect(isEmulator(emulatorLine)).toBeTruthy(); - expect(isEmulator(deviceLine)).toBeFalsy(); - }); - }); - describe('devices', () => { - beforeEach(() => { - execaSpy.and.returnValue(Promise.resolve({ stdout: adbOutput })); - }); + it('should return the IDs of all fully booted devices & emulators', () => { + execaSpy.and.resolveTo({ stdout: adbOutput }); - it('should return only devices if no options are specified', () => { return Adb.devices().then(devices => { - expect(devices.length).toBe(1); - expect(devices[0]).toBe(deviceId); - }); - }); - - it('should return only emulators if opts.emulators is true', () => { - return Adb.devices({ emulators: true }).then(devices => { - expect(devices.length).toBe(1); - expect(devices[0]).toBe(emulatorId); + expect(devices).toEqual([ + 'emulator-5554', + '123a76565509e124' + ]); }); }); }); diff --git a/spec/unit/device.spec.js b/spec/unit/device.spec.js index 6340d86b..5eb166cd 100644 --- a/spec/unit/device.spec.js +++ b/spec/unit/device.spec.js @@ -33,11 +33,11 @@ describe('device', () => { }); describe('list', () => { - it('should return the list from adb devices', () => { - AdbSpy.devices.and.returnValue(Promise.resolve(DEVICE_LIST)); + it('should return a list of all connected devices', () => { + AdbSpy.devices.and.resolveTo(['emulator-5556', '123a76565509e124']); return device.list().then(list => { - expect(list).toEqual(DEVICE_LIST); + expect(list).toEqual(['123a76565509e124']); }); }); }); diff --git a/spec/unit/emulator.spec.js b/spec/unit/emulator.spec.js index 70ae5c2f..943525ab 100644 --- a/spec/unit/emulator.spec.js +++ b/spec/unit/emulator.spec.js @@ -205,13 +205,13 @@ describe('emulator', () => { }); describe('list_started', () => { - it('should call adb devices with the emulators flag', () => { + it('should return a list of all online emulators', () => { const AdbSpy = jasmine.createSpyObj('Adb', ['devices']); - AdbSpy.devices.and.returnValue(Promise.resolve()); + AdbSpy.devices.and.resolveTo(['emulator-5556', '123a76565509e124']); emu.__set__('Adb', AdbSpy); - return emu.list_started().then(() => { - expect(AdbSpy.devices).toHaveBeenCalledWith({ emulators: true }); + return emu.list_started().then(emus => { + expect(emus).toEqual(['emulator-5556']); }); }); });