index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.spawn = exports.exec = void 0;
  4. var child_process_1 = require("child_process");
  5. var MAX_BUFFER = 10 * Math.pow(2, 20);
  6. function exec(command, stdout, stderr, stdin) {
  7. return new Promise(function (resolve, reject) {
  8. try {
  9. var stdoutbuf_1 = '';
  10. var stderrbuf_1 = '';
  11. // EXEC CHILD PROCESS
  12. var p = (0, child_process_1.exec)(command, { maxBuffer: MAX_BUFFER }, function (err, out) {
  13. if (err)
  14. return reject(err);
  15. if (stdoutbuf_1.length > 0 && typeof stdout === 'function')
  16. stdout(stdoutbuf_1);
  17. if (stderrbuf_1.length > 0 && typeof stderr === 'function')
  18. stderr(stderrbuf_1);
  19. resolve(out);
  20. });
  21. // PIPE STDIN (?)
  22. if (typeof stdin === 'string') {
  23. p.stdin.setDefaultEncoding('utf-8');
  24. if (!stdin.endsWith('\n'))
  25. stdin += '\n';
  26. p.stdin.write(stdin);
  27. p.stdin.end();
  28. }
  29. else if (typeof stdin !== 'undefined') {
  30. stdin.pipe(p.stdin);
  31. }
  32. // PIPE STDOUT
  33. if (typeof stdout === 'function') {
  34. p.stdout.on('data', function (chunk) {
  35. stdoutbuf_1 += chunk;
  36. var i = -1;
  37. while ((i = stdoutbuf_1.indexOf('\n')) >= 0) {
  38. var line = stdoutbuf_1.substring(0, i);
  39. stdoutbuf_1 = stdoutbuf_1.substring(i + 1);
  40. if (typeof stdout === 'function') {
  41. stdout(line);
  42. }
  43. }
  44. });
  45. }
  46. else if (typeof stdout !== 'undefined') {
  47. p.stdout.pipe(stdout);
  48. }
  49. // PIPE STDERR
  50. if (typeof stderr === 'function') {
  51. p.stderr.on('data', function (chunk) {
  52. stderrbuf_1 += chunk;
  53. var i = -1;
  54. while ((i = stderrbuf_1.indexOf('\n')) >= 0) {
  55. var line = stderrbuf_1.substring(0, i);
  56. stderrbuf_1 = stderrbuf_1.substring(i + 1);
  57. if (typeof stderr === 'function') {
  58. stderr(line);
  59. }
  60. }
  61. });
  62. }
  63. else if (typeof stderr !== 'undefined') {
  64. p.stderr.pipe(stderr);
  65. }
  66. }
  67. catch (err) {
  68. reject(err);
  69. }
  70. });
  71. }
  72. exports.exec = exec;
  73. function spawn(command, args, stdout, stderr, stdin) {
  74. return new Promise(function (resolve, reject) {
  75. try {
  76. var stdoutbuf_2 = '';
  77. var stderrbuf_2 = '';
  78. var p = (0, child_process_1.spawn)(command, args);
  79. // PIPE STDIN (?)
  80. if (typeof stdin === 'string') {
  81. p.stdin.setDefaultEncoding('utf-8');
  82. if (!stdin.endsWith('\n'))
  83. stdin += '\n';
  84. p.stdin.write(stdin);
  85. p.stdin.end();
  86. }
  87. else if (typeof stdin !== 'undefined') {
  88. stdin.pipe(p.stdin);
  89. }
  90. // PIPE STDOUT
  91. if (typeof stdout === 'function') {
  92. p.stdout.on('data', function (chunk) {
  93. stdoutbuf_2 += chunk;
  94. var i = -1;
  95. while ((i = stdoutbuf_2.indexOf('\n')) >= 0) {
  96. var line = stdoutbuf_2.substring(0, i);
  97. stdoutbuf_2 = stdoutbuf_2.substring(i + 1);
  98. if (typeof stdout === 'function') {
  99. stdout(line);
  100. }
  101. }
  102. });
  103. }
  104. else if (typeof stdout !== 'undefined') {
  105. p.stdout.pipe(stdout);
  106. }
  107. // PIPE STDERR
  108. if (typeof stderr === 'function') {
  109. p.stderr.on('data', function (chunk) {
  110. stderrbuf_2 += chunk;
  111. var i = -1;
  112. while ((i = stderrbuf_2.indexOf('\n')) >= 0) {
  113. var line = stderrbuf_2.substring(0, i);
  114. stderrbuf_2 = stderrbuf_2.substring(i + 1);
  115. if (typeof stderr === 'function') {
  116. stderr(line);
  117. }
  118. }
  119. });
  120. }
  121. else if (typeof stderr !== 'undefined') {
  122. p.stderr.pipe(stderr);
  123. }
  124. p.on('close', function (code, sig) {
  125. if (!code)
  126. resolve();
  127. else
  128. reject();
  129. });
  130. p.on('error', reject);
  131. p.on('exit', function (code, sig) {
  132. if (!code)
  133. resolve();
  134. else
  135. reject();
  136. });
  137. }
  138. catch (err) {
  139. reject(err);
  140. }
  141. });
  142. }
  143. exports.spawn = spawn;
  144. //# sourceMappingURL=index.js.map