Skip to content

Commit

Permalink
Update debug.js
Browse files Browse the repository at this point in the history
  • Loading branch information
arpu committed Apr 5, 2024
1 parent 2d90e1e commit 07e6fd0
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions src/utils/debug.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var debugLib = require('debug');
var debug = require('debug');

var settings = {
colors: {
Expand All @@ -10,18 +10,46 @@ var settings = {
};

/**
* Monkeypatches `debug` so we can colorize error/warning messages.
* Overwrite `debug` so we can colorize error/warning messages and remove Time Diff
*
* (See issue: https://github.com/visionmedia/debug/issues/137)
* (See issue: https://github.com/debug-js/debug/issues/582#issuecomment-1755718739)
*/
var debug = function (namespace) {
var d = debugLib(namespace);
debug.formatArgs = formatArgs;

d.color = getDebugNamespaceColor(namespace);
function formatArgs(args) {
args[0] =
(this.useColors ? '%c' : '') +
this.namespace +
(this.useColors ? ' %c' : ' ') +
args[0] +
(this.useColors ? '%c ' : ' ');

return d;
};
Object.assign(debug, debugLib);
if (!this.useColors) {
return;
}
this.color = getDebugNamespaceColor(this.namespace);
const c = 'color: ' + this.color;

Check failure on line 31 in src/utils/debug.js

View workflow job for this annotation

GitHub Actions / Test Cases (20.x, latest)

Parsing error: The keyword 'const' is reserved
args.splice(1, 0, c, 'color: inherit');

// The final "%c" is somewhat tricky, because there could be other
// arguments passed either before or after the %c, so we need to
// figure out the correct index to insert the CSS into
let index = 0;
let lastC = 0;
args[0].replace(/%[a-zA-Z%]/g, (match) => {
if (match === '%%') {
return;
}
index++;
if (match === '%c') {
// We only are interested in the *last* %c
// (the user may have provided their own)
lastC = index;
}
});

args.splice(lastC, 0, c);
}

/**
* Returns the type of the namespace (e.g., `error`, `warn`).
Expand Down

0 comments on commit 07e6fd0

Please sign in to comment.