2016-03-08 20:07:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-04-06 16:20:39 +00:00
|
|
|
*
|
2018-05-11 19:43:49 +00:00
|
|
|
* @format
|
2018-06-20 07:35:29 +00:00
|
|
|
* @flow strict
|
2016-03-08 20:07:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
const child_process = require('child_process');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the output of the 'adb devices' command
|
|
|
|
*/
|
|
|
|
function parseDevicesResult(result: string): Array<string> {
|
|
|
|
if (!result) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2016-03-08 20:07:59 +00:00
|
|
|
const devices = [];
|
|
|
|
const lines = result.trim().split(/\r?\n/);
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2017-10-10 00:37:08 +00:00
|
|
|
for (let i = 0; i < lines.length; i++) {
|
2018-05-11 19:43:49 +00:00
|
|
|
let words = lines[i].split(/[ ,\t]+/).filter(w => w !== '');
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2016-03-08 20:07:59 +00:00
|
|
|
if (words[1] === 'device') {
|
|
|
|
devices.push(words[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the commands needed to get a list of devices from ADB
|
|
|
|
*/
|
|
|
|
function getDevices(): Array<string> {
|
|
|
|
try {
|
|
|
|
const devicesResult = child_process.execSync('adb devices');
|
|
|
|
return parseDevicesResult(devicesResult.toString());
|
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
parseDevicesResult: parseDevicesResult,
|
2018-05-11 19:43:49 +00:00
|
|
|
getDevices: getDevices,
|
2017-10-10 00:37:08 +00:00
|
|
|
};
|