rubick
Version:
tools for mtc
114 lines (100 loc) • 3.4 kB
JavaScript
/**
* Created by hongzhiyuan on 2017/4/14.
*/
let cmdUtil = require('./cmd');
// let childProcess = require('child_process');
//
// let exec = (cmd, option) => {
// let defaultOption = {
// encoding: 'utf8',
// timeout: 30000,
// maxBuffer: 256 * 1024 * 1024,
// killSignal: 'SIGTERM',
// cwd: null,
// env: null
// };
// if (option) {
// let extend = require('extend');
// defaultOption = extend(true, defaultOption, option);
// }
//
// return new Promise((resolve, reject) => {
// childProcess.exec(cmd, defaultOption, (err, stdout, stderr) => {
// if (err) {
// reject(err, stdout, stderr);
// } else {
// resolve(stdout, stderr);
// }
// });
// });
// };
class AdbKit {
static listDevices() {
return cmdUtil.exec('adb devices').then((stdout) => {
let result = [];
let devices = stdout.split('\n').slice(1);
for (let device of devices) {
if (device) {
device = device.split('\t');
if (device.length >= 2) {
result.push({
id: device[0],
type: device[1]
});
}
}
}
return result;
});
}
static push(serial, localfile, devicefile) {
return cmdUtil.exec(`adb -s ${serial} push ${localfile} ${devicefile}`);
}
static pull(serial, devicefile, localfile) {
return cmdUtil.exec(`adb -s ${serial} pull ${devicefile} ${localfile}`);
}
static shell(serial, cmd, option) {
return cmdUtil.exec(`adb -s ${serial} shell ${cmd}`, option);
}
static install(serial, apk) {
return cmdUtil.exec(`adb -s ${serial} install -r ${apk}`);
}
static getProperties(serial) {
return this.shell(serial, 'getprop').then((stdout) => {
let result = {};
let properties = stdout.split('\n');
for (let propertie of properties) {
if (propertie) {
propertie = propertie.split(/^\[(.+)\]:\s\[(.+)\]\s*$/).filter(Boolean);
if (propertie.length >= 2) {
result[propertie[0]] = propertie[1];
}
}
}
return result;
});
}
static getPackages(serial) {
return this.shell(serial, 'pm list packages').then((stdout) => {
let result = [];
let packages = stdout.split('\n');
for (let packageName of packages) {
if (packageName && packageName.startsWith('package:')) {
packageName = packageName.substr(8).trim();
result.push(packageName);
}
}
return result;
});
}
static screencap(serial) {
let adbkit = require('adbkit');
let client = adbkit.createClient();
return client.screencap(serial);
}
static reboot(serial) {
return cmdUtil.exec(`adb -s ${serial} reboot`);
}
}
module.exports = AdbKit;