Skip to content

Commit

Permalink
refactor: do not kill adb on UNIX-like systems
Browse files Browse the repository at this point in the history
  • Loading branch information
raphinesse committed Oct 20, 2020
1 parent 335b0f2 commit d621b6d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 85 deletions.
43 changes: 2 additions & 41 deletions bin/templates/cordova/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
});
};

Expand Down
22 changes: 3 additions & 19 deletions bin/templates/cordova/lib/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
under the License.
*/

const execa = require('execa');
var build = require('./build');
var path = require('path');
var Adb = require('./Adb');
Expand All @@ -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.'));
}
Expand Down
25 changes: 0 additions & 25 deletions spec/unit/device.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit d621b6d

Please sign in to comment.