node.js 4.58 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
/**
 * Module dependencies.
 */

5
6
const tty = require('tty');
const util = require('util');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
7
8
9
10
11
12
13
14
15
16
17

/**
 * This is the Node.js implementation of `debug()`.
 */

exports.init = init;
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
18
19
20
21
exports.destroy = util.deprecate(
	() => {},
	'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'
);
Rosanny Sihombing's avatar
Rosanny Sihombing committed
22
23
24
25
26
27
28

/**
 * Colors.
 */

exports.colors = [6, 2, 3, 4, 5, 1];

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
try {
	// Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
	// eslint-disable-next-line import/no-extraneous-dependencies
	const supportsColor = require('supports-color');

	if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
		exports.colors = [
			20,
			21,
			26,
			27,
			32,
			33,
			38,
			39,
			40,
			41,
			42,
			43,
			44,
			45,
			56,
			57,
			62,
			63,
			68,
			69,
			74,
			75,
			76,
			77,
			78,
			79,
			80,
			81,
			92,
			93,
			98,
			99,
			112,
			113,
			128,
			129,
			134,
			135,
			148,
			149,
			160,
			161,
			162,
			163,
			164,
			165,
			166,
			167,
			168,
			169,
			170,
			171,
			172,
			173,
			178,
			179,
			184,
			185,
			196,
			197,
			198,
			199,
			200,
			201,
			202,
			203,
			204,
			205,
			206,
			207,
			208,
			209,
			214,
			215,
			220,
			221
		];
	}
} catch (error) {
	// Swallow - we only care if `supports-color` is available; it doesn't have to be.
}

Rosanny Sihombing's avatar
Rosanny Sihombing committed
118
119
120
121
122
123
/**
 * Build up the default `inspectOpts` object from the environment variables.
 *
 *   $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
 */

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
exports.inspectOpts = Object.keys(process.env).filter(key => {
	return /^debug_/i.test(key);
}).reduce((obj, key) => {
	// Camel-case
	const prop = key
		.substring(6)
		.toLowerCase()
		.replace(/_([a-z])/g, (_, k) => {
			return k.toUpperCase();
		});

	// Coerce string value into JS value
	let val = process.env[key];
	if (/^(yes|on|true|enabled)$/i.test(val)) {
		val = true;
	} else if (/^(no|off|false|disabled)$/i.test(val)) {
		val = false;
	} else if (val === 'null') {
		val = null;
	} else {
		val = Number(val);
	}

	obj[prop] = val;
	return obj;
Rosanny Sihombing's avatar
Rosanny Sihombing committed
149
150
151
152
153
154
155
}, {});

/**
 * Is stdout a TTY? Colored output is enabled when `true`.
 */

function useColors() {
156
157
158
	return 'colors' in exports.inspectOpts ?
		Boolean(exports.inspectOpts.colors) :
		tty.isatty(process.stderr.fd);
Rosanny Sihombing's avatar
Rosanny Sihombing committed
159
160
161
162
163
164
165
166
167
}

/**
 * Adds ANSI color escape codes if enabled.
 *
 * @api public
 */

function formatArgs(args) {
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
	const {namespace: name, useColors} = this;

	if (useColors) {
		const c = this.color;
		const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
		const prefix = `  ${colorCode};1m${name} \u001B[0m`;

		args[0] = prefix + args[0].split('\n').join('\n' + prefix);
		args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m');
	} else {
		args[0] = getDate() + name + ' ' + args[0];
	}
}

function getDate() {
	if (exports.inspectOpts.hideDate) {
		return '';
	}
	return new Date().toISOString() + ' ';
Rosanny Sihombing's avatar
Rosanny Sihombing committed
187
188
189
}

/**
190
 * Invokes `util.format()` with the specified arguments and writes to stderr.
Rosanny Sihombing's avatar
Rosanny Sihombing committed
191
192
 */

193
194
function log(...args) {
	return process.stderr.write(util.format(...args) + '\n');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
195
196
197
198
199
200
201
202
203
}

/**
 * Save `namespaces`.
 *
 * @param {String} namespaces
 * @api private
 */
function save(namespaces) {
204
205
206
207
208
209
210
	if (namespaces) {
		process.env.DEBUG = namespaces;
	} else {
		// If you set a process.env field to null or undefined, it gets cast to the
		// string 'null' or 'undefined'. Just delete instead.
		delete process.env.DEBUG;
	}
Rosanny Sihombing's avatar
Rosanny Sihombing committed
211
212
213
214
215
216
217
218
219
220
}

/**
 * Load `namespaces`.
 *
 * @return {String} returns the previously persisted debug modes
 * @api private
 */

function load() {
221
	return process.env.DEBUG;
Rosanny Sihombing's avatar
Rosanny Sihombing committed
222
223
}

Rosanny Sihombing's avatar
Rosanny Sihombing committed
224
/**
Rosanny Sihombing's avatar
Rosanny Sihombing committed
225
226
227
228
 * Init logic for `debug` instances.
 *
 * Create a new `inspectOpts` object in case `useColors` is set
 * differently for a particular `debug` instance.
Rosanny Sihombing's avatar
Rosanny Sihombing committed
229
230
 */

231
232
function init(debug) {
	debug.inspectOpts = {};
Rosanny Sihombing's avatar
Rosanny Sihombing committed
233

234
235
236
237
	const keys = Object.keys(exports.inspectOpts);
	for (let i = 0; i < keys.length; i++) {
		debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
	}
Rosanny Sihombing's avatar
Rosanny Sihombing committed
238
}
Rosanny Sihombing's avatar
Rosanny Sihombing committed
239

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
module.exports = require('./common')(exports);

const {formatters} = module.exports;

/**
 * Map %o to `util.inspect()`, all on a single line.
 */

formatters.o = function (v) {
	this.inspectOpts.colors = this.useColors;
	return util.inspect(v, this.inspectOpts)
		.split('\n')
		.map(str => str.trim())
		.join(' ');
};

Rosanny Sihombing's avatar
Rosanny Sihombing committed
256
/**
257
 * Map %O to `util.inspect()`, allowing multiple lines if needed.
Rosanny Sihombing's avatar
Rosanny Sihombing committed
258
259
 */

260
261
262
263
formatters.O = function (v) {
	this.inspectOpts.colors = this.useColors;
	return util.inspect(v, this.inspectOpts);
};