forked from mklabs/tabtab
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tabtabDebug.js
43 lines (36 loc) · 907 Bytes
/
tabtabDebug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const fs = require('fs');
const util = require('util');
/**
* If TABTAB_DEBUG env is set, make it so that debug statements are also log to
* TABTAB_DEBUG file provided.
* @param {String} name
*/
const tabtabDebug = name => {
/* eslint-disable global-require */
// @ts-ignore
let debug = require('debug')(name);
if (process.env.TABTAB_DEBUG) {
const file = process.env.TABTAB_DEBUG;
const stream = fs.createWriteStream(file, {
flags: 'a+'
});
/**
* @param {...any} args
*/
const log = (...args) => {
args = args.map(arg => {
if (typeof arg === 'string') return arg;
return JSON.stringify(arg);
});
const str = `${util.format(...args)}\n`;
stream.write(str);
};
if (process.env.COMP_LINE) {
debug = log;
} else {
debug.log = log;
}
}
return debug;
};
module.exports = tabtabDebug;