Skip to content

Commit

Permalink
util: allow wildcards in NODE_DEBUG variable
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Dec 11, 2017
1 parent 7bb2cc4 commit bf5f3ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,25 @@ function format(f) {
var debugs = {};
var debugEnviron;

const regExpSpecialChars = /[|\\{}()[\]^$+?.]/g;

function stringToRegExp(string) {
// Escape special chars except wildcard.
string = string.replace(regExpSpecialChars, '\\$&');
// Transform wildcard to "match anything"
string = string.replace(/\*/g, '.*');
return new RegExp(`^${string}$`);
}

function debuglog(set) {
if (debugEnviron === undefined) {
debugEnviron = new Set(
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
debugEnviron = Array.from(new Set(
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase())));
debugEnviron = debugEnviron.map(stringToRegExp);
}
set = set.toUpperCase();
if (!debugs[set]) {
if (debugEnviron.has(set)) {
if (debugEnviron.some((name) => name.test(set))) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
Expand Down
9 changes: 9 additions & 0 deletions test/sequential/test-util-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ function parent() {
test('f$oo', true, 'f$oo');
test('f$oo', false, 'f.oo');
test('no-bar-at-all', false, 'bar');

test('test-abc', true, 'test-abc');
test('test-a', false, 'test-abc');
test('test-*', true, 'test-abc');
test('test-*c', true, 'test-abc');
test('test-*abc', true, 'test-abc');
test('abc-test', true, 'abc-test');
test('a*-test', true, 'abc-test');
test('*-test', true, 'abc-test');
}

function test(environ, shouldWrite, section) {
Expand Down

0 comments on commit bf5f3ac

Please sign in to comment.