Created
October 8, 2016 14:09
-
-
Save roccomuso/e300fb2daa0b7152d026e6b7b5391812 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var config = require('config'); | |
var chalk = require('chalk'); | |
/* | |
* | |
* DEBUG wrapper to enhance the debug module features. Include this wrapper into your projects to enable debug from a config file and make the files use the global namespace and their name. | |
* Use it along with the 'config' module. The provides at least this configuration: | |
* {namespace: "projectName", debug: true} | |
* | |
*/ | |
function noop() {} | |
var firstCall = false; | |
var PROJECT_NAME = config.namespace; // debug environment | |
module.exports = function(customName) { | |
if (!module.parent) throw Error('module.parent not defined'); | |
if (process.env.DEBUG === undefined && config.debug === true) | |
process.env.DEBUG = PROJECT_NAME + ':*'; | |
if (process.env.DEBUG && !(process.env.DEBUG).match(new RegExp(config.namespace)) && !firstCall) | |
console.log(chalk.bgBlue.bold('DEBUG not enabled on the '+config.namespace+' namespace!')); | |
if (!firstCall) { | |
console.log(chalk.bgCyan.bold('Debug:', (process.env.DEBUG ? true : false))); | |
firstCall = true; | |
} | |
if (process.env.DEBUG){ | |
var sector = PROJECT_NAME+':'+ (customName ? customName : path.basename(module.parent.filename)); | |
return require('debug')(sector); | |
}else | |
return noop; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment