forked from gebrkn/copytables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-logs.js
87 lines (71 loc) · 2.04 KB
/
convert-logs.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
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
/// In dev, rewrite console.logs to something more readable when logged to stdout
/// In production, get rid of them
function __dbg() {
var nl = '\n',
buf = [nl];
function type(x) {
try {
return Object.prototype.toString.call(x).replace('[object ', '').replace(']', '');
} catch (e) {
return '';
}
}
function props(x, depth) {
var r = Array.isArray(x) ? [] : {};
try {
Object.keys(x).forEach(function(k) {
r[k] = inspect(x[k], depth + 1);
});
return r;
} catch(e) {
return 'error';
}
}
function inspect(x, depth) {
if (depth > 5)
return '...';
if (typeof x !== 'object' || x === null)
return x;
var t = type(x),
p = props(x, depth);
if(t === 'Object' || t === 'Array')
return p;
var r = {};
r[t] = p;
return r;
}
buf.push(location ? location.href : '??');
[].forEach.call(arguments, function (arg) {
var t = inspect(arg, 0);
t = JSON.stringify(t, 0, 4);
buf = buf.concat((t || '').split(nl));
});
return buf.map(function (x) {
return x + nl;
});
}
function handleLogging(content, path, isDev) {
var dbg = String(__dbg);
var out = [];
if (isDev) {
out.push(dbg.replace(/\s+/g, ' '));
}
content.split('\n').forEach(function (line, lnum) {
var ref = path + ':' + (lnum + 1);
var m = line.match(/(.*?)console\.log\((.*)\)(.*)/);
if (m) {
if (isDev) {
out.push(`${m[1]}__dbg("${ref}",${m[2]}).forEach(console.log.bind(console))${m[3]}`);
}
} else {
if (isDev && line.match(/function/)) {
out.push(` // ${ref}`);
}
out.push(line);
}
});
return out.join('\n');
}
module.exports = function (content) {
return handleLogging(content, this.resourcePath, this.query == '?dev')
}