utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const child_process = require("child_process");
  4. const fs = require("fs");
  5. const http = require("http");
  6. const path = require("path");
  7. const config_1 = require("./config");
  8. function spawnFactory(sync) {
  9. return (cmd, args, stdio, opts) => {
  10. if ((config_1.Config.osType() === 'Windows_NT') && (cmd.slice(-4) !== '.exe')) {
  11. if (fs.existsSync(cmd + '.exe')) {
  12. cmd += '.exe';
  13. }
  14. else {
  15. args = ['/c'].concat([cmd], args);
  16. cmd = 'cmd';
  17. }
  18. }
  19. if (stdio) {
  20. opts = opts || {};
  21. opts.stdio = stdio;
  22. }
  23. if (sync) {
  24. return child_process.spawnSync(cmd, args, opts);
  25. }
  26. else {
  27. return child_process.spawn(cmd, args, opts);
  28. }
  29. };
  30. }
  31. exports.spawn = spawnFactory(false);
  32. exports.spawnSync = spawnFactory(true);
  33. function request(method, port, path, timeout, data) {
  34. let headers = {};
  35. let hasContent = data && ((method == 'POST') || (method == 'PUT'));
  36. if (hasContent) {
  37. data = data ? JSON.stringify(data) : '';
  38. headers['Content-Length'] = data.length;
  39. headers['Content-Type'] = 'application/json;charset=UTF-8';
  40. }
  41. return new Promise((resolve, reject) => {
  42. let unexpectedEnd = () => {
  43. reject({ code: 'UNKNOWN', message: 'Request ended unexpectedly' });
  44. };
  45. let req = http.request({ port: parseInt(port), method: method, path: path, headers: headers }, (res) => {
  46. req.removeListener('end', unexpectedEnd);
  47. if (res.statusCode !== 200) {
  48. reject({ code: res.statusCode, message: res.statusMessage });
  49. }
  50. else {
  51. let buffer = [];
  52. res.on('data', buffer.push.bind(buffer));
  53. res.on('end', () => {
  54. resolve(buffer.join('').replace(/\0/g, ''));
  55. });
  56. }
  57. });
  58. if (timeout) {
  59. req.setTimeout(timeout, () => {
  60. reject({ code: 'TIMEOUT', message: 'Request timed out' });
  61. });
  62. }
  63. req.on('error', reject);
  64. req.on('end', unexpectedEnd);
  65. if (hasContent) {
  66. req.write(data);
  67. }
  68. req.end();
  69. });
  70. }
  71. exports.request = request;
  72. function adb(sdkPath, port, command, timeout, args) {
  73. return new Promise((resolve, reject) => {
  74. let child = exports.spawn(path.resolve(sdkPath, 'platform-tools', 'adb'), ['-s', 'emulator-' + port, command].concat(args || []), 'pipe');
  75. let done = false;
  76. let buffer = [];
  77. child.stdout.on('data', buffer.push.bind(buffer));
  78. child.on('error', (err) => {
  79. if (!done) {
  80. done = true;
  81. reject(err);
  82. }
  83. });
  84. child.on('exit', (code, signal) => {
  85. if (!done) {
  86. done = true;
  87. if (code === 0) {
  88. resolve(buffer.join(''));
  89. }
  90. else {
  91. reject({
  92. code: code,
  93. message: 'abd command "' + command + '" ' +
  94. (signal ? 'received signal ' + signal : 'returned with a non-zero exit code') +
  95. 'for emulator-' + port
  96. });
  97. }
  98. }
  99. });
  100. if (timeout) {
  101. setTimeout(() => {
  102. if (!done) {
  103. done = true;
  104. child.kill();
  105. reject({
  106. code: 'TIMEOUT',
  107. message: 'adb command "' + command + '" timed out for emulator-' + port
  108. });
  109. }
  110. }, timeout);
  111. }
  112. });
  113. }
  114. exports.adb = adb;
  115. //# sourceMappingURL=utils.js.map