debug.js 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. /* tslint:disable no-invalid-this no-console */
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.debug = debug;
  5. let id = 0;
  6. function debug(name) {
  7. return (fn, context) => {
  8. if (process.env.NODE_ENV !== 'production') {
  9. return function (...args) {
  10. id++;
  11. const idStr = id.toString(36);
  12. const currentName = name ?? (this ? this.constructor?.name + '.' : '') + (String(context?.name) ?? fn.name ?? 'anonymous');
  13. console.log('%cRUN', 'background:white;color:blue', idStr, currentName, ...args);
  14. try {
  15. const res = fn.apply(this, args);
  16. if (res instanceof Promise) {
  17. res.then((res) => console.log('%cSUC', 'background:green;color:white', idStr, currentName, res), (err) => console.log('%cERR', 'background:red;color:white', idStr, currentName, err));
  18. return res;
  19. }
  20. console.log('%cSUC', 'background:green;color:white', idStr, currentName, res);
  21. return res;
  22. }
  23. catch (err) {
  24. console.log('%cERR', 'background:red;color:white', idStr, currentName, err);
  25. throw err;
  26. }
  27. };
  28. }
  29. return fn;
  30. };
  31. }