From aada3e813d35be6e16156ba631cf398df55c9802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Thu, 22 Oct 2020 18:03:28 +0200 Subject: [PATCH] refactor: do not kill adb on UNIX-like systems (#1103) --- bin/templates/cordova/lib/build.js | 43 ++--------------------------- bin/templates/cordova/lib/device.js | 22 ++------------- spec/unit/device.spec.js | 25 ----------------- 3 files changed, 5 insertions(+), 85 deletions(-) diff --git a/bin/templates/cordova/lib/build.js b/bin/templates/cordova/lib/build.js index fcbd486f..71b93cc5 100644 --- a/bin/templates/cordova/lib/build.js +++ b/bin/templates/cordova/lib/build.js @@ -24,8 +24,6 @@ var nopt = require('nopt'); var Adb = require('./Adb'); var events = require('cordova-common').events; -const execa = require('execa'); -var CordovaError = require('cordova-common').CordovaError; var PackageType = require('./PackageType'); module.exports.parseBuildOptions = parseOpts; @@ -195,45 +193,8 @@ module.exports.run = function (options, optResolvedTarget) { * Returns "arm" or "x86". */ module.exports.detectArchitecture = function (target) { - function helper () { - return Adb.shell(target, 'cat /proc/cpuinfo').then(function (output) { - return /intel/i.exec(output) ? 'x86' : 'arm'; - }); - } - function timeout (ms, err) { - return new Promise((resolve, reject) => { - setTimeout(() => reject(err), ms); - }); - } - // It sometimes happens (at least on OS X), that this command will hang forever. - // To fix it, either unplug & replug device, or restart adb server. - return Promise.race([ - helper(), - timeout(5000, new CordovaError( - 'Device communication timed out. Try unplugging & replugging the device.' - )) - ]).catch(err => { - if (/timed out/.exec('' + err)) { - // adb kill-server doesn't seem to do the trick. - // Could probably find a x-platform version of killall, but I'm not actually - // sure that this scenario even happens on non-OSX machines. - events.emit('verbose', 'adb timed out while detecting device/emulator architecture. Killing adb and trying again.'); - return execa('killall', ['adb']).then(function () { - return helper().then(null, function () { - // The double kill is sadly often necessary, at least on mac. - events.emit('warn', 'adb timed out a second time while detecting device/emulator architecture. Killing adb and trying again.'); - return execa('killall', ['adb']).then(function () { - return helper().then(null, function () { - return Promise.reject(new CordovaError('adb timed out a third time while detecting device/emulator architecture. Try unplugging & replugging the device.')); - }); - }); - }); - }, function () { - // For non-killall OS's. - return Promise.reject(err); - }); - } - throw err; + return Adb.shell(target, 'cat /proc/cpuinfo').then(function (output) { + return /intel/i.exec(output) ? 'x86' : 'arm'; }); }; diff --git a/bin/templates/cordova/lib/device.js b/bin/templates/cordova/lib/device.js index 3bb95c90..dedaf717 100644 --- a/bin/templates/cordova/lib/device.js +++ b/bin/templates/cordova/lib/device.js @@ -17,7 +17,6 @@ under the License. */ -const execa = require('execa'); var build = require('./build'); var path = require('path'); var Adb = require('./Adb'); @@ -27,28 +26,13 @@ var events = require('cordova-common').events; /** * Returns a promise for the list of the device ID's found - * @param lookHarder When true, try restarting adb if no devices are found. */ -module.exports.list = function (lookHarder) { - return Adb.devices().then(function (list) { - if (list.length === 0 && lookHarder) { - // adb kill-server doesn't seem to do the trick. - // Could probably find a x-platform version of killall, but I'm not actually - // sure that this scenario even happens on non-OSX machines. - return execa('killall', ['adb']).then(function () { - events.emit('verbose', 'Restarting adb to see if more devices are detected.'); - return Adb.devices(); - }, function () { - // For non-killall OS's. - return list; - }); - } - return list; - }); +module.exports.list = function () { + return Adb.devices(); }; module.exports.resolveTarget = function (target) { - return this.list(true).then(function (device_list) { + return this.list().then(function (device_list) { if (!device_list || !device_list.length) { return Promise.reject(new CordovaError('Failed to deploy to device, no devices found.')); } diff --git a/spec/unit/device.spec.js b/spec/unit/device.spec.js index d5adcaa1..6340d86b 100644 --- a/spec/unit/device.spec.js +++ b/spec/unit/device.spec.js @@ -40,31 +40,6 @@ describe('device', () => { expect(list).toEqual(DEVICE_LIST); }); }); - - it('should kill adb and try to get devices again if none are found the first time, and `lookHarder` is set', () => { - const execaSpy = jasmine.createSpy('execa').and.returnValue(Promise.resolve()); - device.__set__('execa', execaSpy); - AdbSpy.devices.and.returnValues(Promise.resolve([]), Promise.resolve(DEVICE_LIST)); - - return device.list(true).then(list => { - expect(execaSpy).toHaveBeenCalledWith('killall', ['adb']); - expect(list).toBe(DEVICE_LIST); - expect(AdbSpy.devices).toHaveBeenCalledTimes(2); - }); - }); - - it('should return the empty list if killing adb fails', () => { - const emptyDevices = []; - const execaSpy = jasmine.createSpy('execa').and.returnValue(Promise.reject()); - device.__set__('execa', execaSpy); - AdbSpy.devices.and.returnValues(Promise.resolve(emptyDevices)); - - return device.list(true).then(list => { - expect(execaSpy).toHaveBeenCalledWith('killall', ['adb']); - expect(list).toBe(emptyDevices); - expect(AdbSpy.devices).toHaveBeenCalledTimes(1); - }); - }); }); describe('resolveTarget', () => {