adb.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.runAndroidLoggingProcess = runAndroidLoggingProcess;
  6. exports.getAdbPath = getAdbPath;
  7. exports.spawnLogcatProcess = spawnLogcatProcess;
  8. exports.getApplicationPid = getApplicationPid;
  9. var _child_process = require("child_process");
  10. var _path = _interopRequireDefault(require("path"));
  11. var _errors = require("../errors");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function runAndroidLoggingProcess(adbPath) {
  14. const execPath = getAdbPath(adbPath);
  15. return spawnLogcatProcess(execPath);
  16. }
  17. function getAdbPath(customPath) {
  18. if (customPath) {
  19. return _path.default.resolve(customPath);
  20. }
  21. return process.env.ANDROID_HOME ? `${process.env.ANDROID_HOME}/platform-tools/adb` : 'adb';
  22. }
  23. function spawnLogcatProcess(adbPath) {
  24. try {
  25. (0, _child_process.execFileSync)(adbPath, ['logcat', '-c']);
  26. } catch (error) {
  27. throw new _errors.CodeError(_errors.ERR_ANDROID_CANNOT_CLEAN_LOGCAT_BUFFER, error.message);
  28. }
  29. try {
  30. return (0, _child_process.spawn)(adbPath, ['logcat', '-v', 'time', 'process', 'tag'], {
  31. stdio: 'pipe'
  32. });
  33. } catch (error) {
  34. throw new _errors.CodeError(_errors.ERR_ANDROID_CANNOT_START_LOGCAT, error.message);
  35. }
  36. }
  37. function getApplicationPid(applicationId, adbPath) {
  38. let output;
  39. try {
  40. output = (0, _child_process.execFileSync)(getAdbPath(adbPath), ['shell', 'pidof', '-s', applicationId]);
  41. } catch (error) {
  42. throw new _errors.CodeError(_errors.ERR_ANDROID_CANNOT_GET_APP_PID, error.message);
  43. }
  44. const pid = output ? parseInt(output.toString(), 10) : NaN;
  45. if (isNaN(pid)) {
  46. throw new _errors.CodeError(_errors.ERR_ANDROID_UNPROCESSABLE_PID);
  47. }
  48. return pid;
  49. }
  50. //# sourceMappingURL=adb.js.map