refactor: do not kill adb on UNIX-like systems (#1103)

This commit is contained in:
Raphael von der Grün 2020-10-22 18:03:28 +02:00 committed by GitHub
parent 335b0f2575
commit aada3e813d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 85 deletions

View File

@ -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';
});
};

View File

@ -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.'));
}

View File

@ -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', () => {