Skip to content

Instantly share code, notes, and snippets.

@roccomuso
Created October 8, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roccomuso/e300fb2daa0b7152d026e6b7b5391812 to your computer and use it in GitHub Desktop.
Save roccomuso/e300fb2daa0b7152d026e6b7b5391812 to your computer and use it in GitHub Desktop.
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