twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

node.js (4475B)


      1 /**
      2  * Module dependencies.
      3  */
      4 
      5 const tty = require('tty');
      6 const util = require('util');
      7 
      8 /**
      9  * This is the Node.js implementation of `debug()`.
     10  */
     11 
     12 exports.init = init;
     13 exports.log = log;
     14 exports.formatArgs = formatArgs;
     15 exports.save = save;
     16 exports.load = load;
     17 exports.useColors = useColors;
     18 
     19 /**
     20  * Colors.
     21  */
     22 
     23 exports.colors = [6, 2, 3, 4, 5, 1];
     24 
     25 try {
     26 	// Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
     27 	// eslint-disable-next-line import/no-extraneous-dependencies
     28 	const supportsColor = require('supports-color');
     29 
     30 	if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
     31 		exports.colors = [
     32 			20,
     33 			21,
     34 			26,
     35 			27,
     36 			32,
     37 			33,
     38 			38,
     39 			39,
     40 			40,
     41 			41,
     42 			42,
     43 			43,
     44 			44,
     45 			45,
     46 			56,
     47 			57,
     48 			62,
     49 			63,
     50 			68,
     51 			69,
     52 			74,
     53 			75,
     54 			76,
     55 			77,
     56 			78,
     57 			79,
     58 			80,
     59 			81,
     60 			92,
     61 			93,
     62 			98,
     63 			99,
     64 			112,
     65 			113,
     66 			128,
     67 			129,
     68 			134,
     69 			135,
     70 			148,
     71 			149,
     72 			160,
     73 			161,
     74 			162,
     75 			163,
     76 			164,
     77 			165,
     78 			166,
     79 			167,
     80 			168,
     81 			169,
     82 			170,
     83 			171,
     84 			172,
     85 			173,
     86 			178,
     87 			179,
     88 			184,
     89 			185,
     90 			196,
     91 			197,
     92 			198,
     93 			199,
     94 			200,
     95 			201,
     96 			202,
     97 			203,
     98 			204,
     99 			205,
    100 			206,
    101 			207,
    102 			208,
    103 			209,
    104 			214,
    105 			215,
    106 			220,
    107 			221
    108 		];
    109 	}
    110 } catch (error) {
    111 	// Swallow - we only care if `supports-color` is available; it doesn't have to be.
    112 }
    113 
    114 /**
    115  * Build up the default `inspectOpts` object from the environment variables.
    116  *
    117  *   $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
    118  */
    119 
    120 exports.inspectOpts = Object.keys(process.env).filter(key => {
    121 	return /^debug_/i.test(key);
    122 }).reduce((obj, key) => {
    123 	// Camel-case
    124 	const prop = key
    125 		.substring(6)
    126 		.toLowerCase()
    127 		.replace(/_([a-z])/g, (_, k) => {
    128 			return k.toUpperCase();
    129 		});
    130 
    131 	// Coerce string value into JS value
    132 	let val = process.env[key];
    133 	if (/^(yes|on|true|enabled)$/i.test(val)) {
    134 		val = true;
    135 	} else if (/^(no|off|false|disabled)$/i.test(val)) {
    136 		val = false;
    137 	} else if (val === 'null') {
    138 		val = null;
    139 	} else {
    140 		val = Number(val);
    141 	}
    142 
    143 	obj[prop] = val;
    144 	return obj;
    145 }, {});
    146 
    147 /**
    148  * Is stdout a TTY? Colored output is enabled when `true`.
    149  */
    150 
    151 function useColors() {
    152 	return 'colors' in exports.inspectOpts ?
    153 		Boolean(exports.inspectOpts.colors) :
    154 		tty.isatty(process.stderr.fd);
    155 }
    156 
    157 /**
    158  * Adds ANSI color escape codes if enabled.
    159  *
    160  * @api public
    161  */
    162 
    163 function formatArgs(args) {
    164 	const {namespace: name, useColors} = this;
    165 
    166 	if (useColors) {
    167 		const c = this.color;
    168 		const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
    169 		const prefix = `  ${colorCode};1m${name} \u001B[0m`;
    170 
    171 		args[0] = prefix + args[0].split('\n').join('\n' + prefix);
    172 		args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m');
    173 	} else {
    174 		args[0] = getDate() + name + ' ' + args[0];
    175 	}
    176 }
    177 
    178 function getDate() {
    179 	if (exports.inspectOpts.hideDate) {
    180 		return '';
    181 	}
    182 	return new Date().toISOString() + ' ';
    183 }
    184 
    185 /**
    186  * Invokes `util.format()` with the specified arguments and writes to stderr.
    187  */
    188 
    189 function log(...args) {
    190 	return process.stderr.write(util.format(...args) + '\n');
    191 }
    192 
    193 /**
    194  * Save `namespaces`.
    195  *
    196  * @param {String} namespaces
    197  * @api private
    198  */
    199 function save(namespaces) {
    200 	if (namespaces) {
    201 		process.env.DEBUG = namespaces;
    202 	} else {
    203 		// If you set a process.env field to null or undefined, it gets cast to the
    204 		// string 'null' or 'undefined'. Just delete instead.
    205 		delete process.env.DEBUG;
    206 	}
    207 }
    208 
    209 /**
    210  * Load `namespaces`.
    211  *
    212  * @return {String} returns the previously persisted debug modes
    213  * @api private
    214  */
    215 
    216 function load() {
    217 	return process.env.DEBUG;
    218 }
    219 
    220 /**
    221  * Init logic for `debug` instances.
    222  *
    223  * Create a new `inspectOpts` object in case `useColors` is set
    224  * differently for a particular `debug` instance.
    225  */
    226 
    227 function init(debug) {
    228 	debug.inspectOpts = {};
    229 
    230 	const keys = Object.keys(exports.inspectOpts);
    231 	for (let i = 0; i < keys.length; i++) {
    232 		debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
    233 	}
    234 }
    235 
    236 module.exports = require('./common')(exports);
    237 
    238 const {formatters} = module.exports;
    239 
    240 /**
    241  * Map %o to `util.inspect()`, all on a single line.
    242  */
    243 
    244 formatters.o = function (v) {
    245 	this.inspectOpts.colors = this.useColors;
    246 	return util.inspect(v, this.inspectOpts)
    247 		.replace(/\s*\n\s*/g, ' ');
    248 };
    249 
    250 /**
    251  * Map %O to `util.inspect()`, allowing multiple lines if needed.
    252  */
    253 
    254 formatters.O = function (v) {
    255 	this.inspectOpts.colors = this.useColors;
    256 	return util.inspect(v, this.inspectOpts);
    257 };