CustomConsole.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _assert() {
  7. const data = _interopRequireDefault(require('assert'));
  8. _assert = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _console() {
  14. const data = require('console');
  15. _console = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _util() {
  21. const data = require('util');
  22. _util = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _chalk() {
  28. const data = _interopRequireDefault(require('chalk'));
  29. _chalk = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _jestUtil() {
  35. const data = require('jest-util');
  36. _jestUtil = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _interopRequireDefault(obj) {
  42. return obj && obj.__esModule ? obj : {default: obj};
  43. }
  44. function _defineProperty(obj, key, value) {
  45. if (key in obj) {
  46. Object.defineProperty(obj, key, {
  47. value: value,
  48. enumerable: true,
  49. configurable: true,
  50. writable: true
  51. });
  52. } else {
  53. obj[key] = value;
  54. }
  55. return obj;
  56. }
  57. class CustomConsole extends _console().Console {
  58. constructor(stdout, stderr, formatBuffer = (_type, message) => message) {
  59. super(stdout, stderr);
  60. _defineProperty(this, '_stdout', void 0);
  61. _defineProperty(this, '_stderr', void 0);
  62. _defineProperty(this, '_formatBuffer', void 0);
  63. _defineProperty(this, '_counters', {});
  64. _defineProperty(this, '_timers', {});
  65. _defineProperty(this, '_groupDepth', 0);
  66. _defineProperty(this, 'Console', _console().Console);
  67. this._stdout = stdout;
  68. this._stderr = stderr;
  69. this._formatBuffer = formatBuffer;
  70. }
  71. _log(type, message) {
  72. (0, _jestUtil().clearLine)(this._stdout);
  73. super.log(
  74. this._formatBuffer(type, ' '.repeat(this._groupDepth) + message)
  75. );
  76. }
  77. _logError(type, message) {
  78. (0, _jestUtil().clearLine)(this._stderr);
  79. super.error(
  80. this._formatBuffer(type, ' '.repeat(this._groupDepth) + message)
  81. );
  82. }
  83. assert(value, message) {
  84. try {
  85. (0, _assert().default)(value, message);
  86. } catch (error) {
  87. this._logError('assert', error.toString());
  88. }
  89. }
  90. count(label = 'default') {
  91. if (!this._counters[label]) {
  92. this._counters[label] = 0;
  93. }
  94. this._log(
  95. 'count',
  96. (0, _util().format)(`${label}: ${++this._counters[label]}`)
  97. );
  98. }
  99. countReset(label = 'default') {
  100. this._counters[label] = 0;
  101. }
  102. debug(firstArg, ...args) {
  103. this._log('debug', (0, _util().format)(firstArg, ...args));
  104. }
  105. dir(firstArg, ...args) {
  106. this._log('dir', (0, _util().format)(firstArg, ...args));
  107. }
  108. dirxml(firstArg, ...args) {
  109. this._log('dirxml', (0, _util().format)(firstArg, ...args));
  110. }
  111. error(firstArg, ...args) {
  112. this._logError('error', (0, _util().format)(firstArg, ...args));
  113. }
  114. group(title, ...args) {
  115. this._groupDepth++;
  116. if (title || args.length > 0) {
  117. this._log(
  118. 'group',
  119. _chalk().default.bold((0, _util().format)(title, ...args))
  120. );
  121. }
  122. }
  123. groupCollapsed(title, ...args) {
  124. this._groupDepth++;
  125. if (title || args.length > 0) {
  126. this._log(
  127. 'groupCollapsed',
  128. _chalk().default.bold((0, _util().format)(title, ...args))
  129. );
  130. }
  131. }
  132. groupEnd() {
  133. if (this._groupDepth > 0) {
  134. this._groupDepth--;
  135. }
  136. }
  137. info(firstArg, ...args) {
  138. this._log('info', (0, _util().format)(firstArg, ...args));
  139. }
  140. log(firstArg, ...args) {
  141. this._log('log', (0, _util().format)(firstArg, ...args));
  142. }
  143. time(label = 'default') {
  144. if (this._timers[label]) {
  145. return;
  146. }
  147. this._timers[label] = new Date();
  148. }
  149. timeEnd(label = 'default') {
  150. const startTime = this._timers[label];
  151. if (startTime) {
  152. const endTime = new Date().getTime();
  153. const time = endTime - startTime.getTime();
  154. this._log(
  155. 'time',
  156. (0, _util().format)(`${label}: ${(0, _jestUtil().formatTime)(time)}`)
  157. );
  158. delete this._timers[label];
  159. }
  160. }
  161. timeLog(label = 'default', ...data) {
  162. const startTime = this._timers[label];
  163. if (startTime) {
  164. const endTime = new Date();
  165. const time = endTime.getTime() - startTime.getTime();
  166. this._log(
  167. 'time',
  168. (0, _util().format)(
  169. `${label}: ${(0, _jestUtil().formatTime)(time)}`,
  170. ...data
  171. )
  172. );
  173. }
  174. }
  175. warn(firstArg, ...args) {
  176. this._logError('warn', (0, _util().format)(firstArg, ...args));
  177. }
  178. getBuffer() {
  179. return undefined;
  180. }
  181. }
  182. exports.default = CustomConsole;