BufferedConsole.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 _console() {
  14. const data = require('console');
  15. _console = function _console() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _util() {
  21. const data = require('util');
  22. _util = function _util() {
  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 _sourceMap() {
  35. const data = require('@jest/source-map');
  36. _sourceMap = function _sourceMap() {
  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 BufferedConsole extends _console().Console {
  58. constructor(getSourceMaps) {
  59. const buffer = [];
  60. super({
  61. write: message => {
  62. BufferedConsole.write(buffer, 'log', message, null, getSourceMaps());
  63. return true;
  64. }
  65. });
  66. _defineProperty(this, '_buffer', void 0);
  67. _defineProperty(this, '_counters', void 0);
  68. _defineProperty(this, '_timers', void 0);
  69. _defineProperty(this, '_groupDepth', void 0);
  70. _defineProperty(this, '_getSourceMaps', void 0);
  71. this._getSourceMaps = getSourceMaps;
  72. this._buffer = buffer;
  73. this._counters = {};
  74. this._timers = {};
  75. this._groupDepth = 0;
  76. }
  77. static write(buffer, type, message, level, sourceMaps) {
  78. const callsite = (0, _sourceMap().getCallsite)(
  79. level != null ? level : 2,
  80. sourceMaps
  81. );
  82. const origin = callsite.getFileName() + ':' + callsite.getLineNumber();
  83. buffer.push({
  84. message,
  85. origin,
  86. type
  87. });
  88. return buffer;
  89. }
  90. _log(type, message) {
  91. BufferedConsole.write(
  92. this._buffer,
  93. type,
  94. ' '.repeat(this._groupDepth) + message,
  95. 3,
  96. this._getSourceMaps()
  97. );
  98. }
  99. assert(value, message) {
  100. try {
  101. (0, _assert().default)(value, message);
  102. } catch (error) {
  103. this._log('assert', error.toString());
  104. }
  105. }
  106. count(label = 'default') {
  107. if (!this._counters[label]) {
  108. this._counters[label] = 0;
  109. }
  110. this._log(
  111. 'count',
  112. (0, _util().format)(`${label}: ${++this._counters[label]}`)
  113. );
  114. }
  115. countReset(label = 'default') {
  116. this._counters[label] = 0;
  117. }
  118. debug(firstArg, ...rest) {
  119. this._log('debug', (0, _util().format)(firstArg, ...rest));
  120. }
  121. dir(firstArg, ...rest) {
  122. this._log('dir', (0, _util().format)(firstArg, ...rest));
  123. }
  124. dirxml(firstArg, ...rest) {
  125. this._log('dirxml', (0, _util().format)(firstArg, ...rest));
  126. }
  127. error(firstArg, ...rest) {
  128. this._log('error', (0, _util().format)(firstArg, ...rest));
  129. }
  130. group(title, ...rest) {
  131. this._groupDepth++;
  132. if (title || rest.length > 0) {
  133. this._log(
  134. 'group',
  135. _chalk().default.bold((0, _util().format)(title, ...rest))
  136. );
  137. }
  138. }
  139. groupCollapsed(title, ...rest) {
  140. this._groupDepth++;
  141. if (title || rest.length > 0) {
  142. this._log(
  143. 'groupCollapsed',
  144. _chalk().default.bold((0, _util().format)(title, ...rest))
  145. );
  146. }
  147. }
  148. groupEnd() {
  149. if (this._groupDepth > 0) {
  150. this._groupDepth--;
  151. }
  152. }
  153. info(firstArg, ...rest) {
  154. this._log('info', (0, _util().format)(firstArg, ...rest));
  155. }
  156. log(firstArg, ...rest) {
  157. this._log('log', (0, _util().format)(firstArg, ...rest));
  158. }
  159. time(label = 'default') {
  160. if (this._timers[label]) {
  161. return;
  162. }
  163. this._timers[label] = new Date();
  164. }
  165. timeEnd(label = 'default') {
  166. const startTime = this._timers[label];
  167. if (startTime) {
  168. const endTime = new Date();
  169. const time = endTime.getTime() - startTime.getTime();
  170. this._log('time', (0, _util().format)(`${label}: ${time}ms`));
  171. delete this._timers[label];
  172. }
  173. }
  174. warn(firstArg, ...rest) {
  175. this._log('warn', (0, _util().format)(firstArg, ...rest));
  176. }
  177. getBuffer() {
  178. return this._buffer.length ? this._buffer : undefined;
  179. }
  180. }
  181. exports.default = BufferedConsole;