CustomConsole.js 4.2 KB

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