From ae5acf7f84ae9dab4c73a82bf2fa4c4602ebb572 Mon Sep 17 00:00:00 2001 From: Audisho Sada Date: Wed, 10 Jun 2020 13:47:55 -0400 Subject: [PATCH] allows debug calls prior to instantiation deletes debug module caches and reloads debug --- index.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 488ec52..8c73634 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ var pino = require('pino') require('module').wrap = override -var debug = require('debug') +var debug = reload('debug') module.exports = pinoDebug @@ -102,3 +102,25 @@ function override (script) { return head + script + tail } + +function reload(module) { + unload(module); + return require(module); +} + +function unload(module, stack) { + var path = require.resolve(module); + + if (require.cache[path] && require.cache[path].children) { + stack = stack || []; + + require.cache[path].children.forEach(function (child) { + if (!stack.includes(child.id)) { + stack.push(path); + unload(child.id, stack); + } + }); + } + + delete require.cache[path]; +}