index.js 5.5 KB

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