-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (94 loc) · 3.56 KB
/
index.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
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
118
119
120
121
122
123
const logSymbols = require('log-symbols');
const path = require('path');
module.exports = function(results) {
results = results || [];
let show_all = process.env.OVERVIEW_FORMATTER_SHOW_ALL || false;
let nested = {};
let final = '';
let root = path.resolve('.');
results.forEach((res) => {
if (!show_all && (!res.messages || res.messages.length <= 0)) return;
let relativePath = path.relative(root, res.filePath);
let splitpath = relativePath.split('/');
let current = nested;
splitpath.forEach(function(subpath) {
if (current.hasOwnProperty(subpath)) {
current = current[subpath];
} else {
current = current[subpath] = {};
}
});
current['error_count'] = res.errorCount;
let tmp_errors = res.messages.filter(m => m.severity===2);
current['errors'] = tmp_errors.reduce((acc, cur) => {
let exists = acc && acc.hasOwnProperty(cur.ruleId);
if (exists) acc[cur.ruleId].push(`${cur.line}:${cur.column}`);
else acc[cur.ruleId] = [`${cur.line}:${cur.column}`];
return acc;
}, {});
current['warning_count'] = res.warningCount;
let tmp_warnings = res.messages.filter(m => m.severity===1);
current['warnings'] = tmp_warnings.reduce((acc, cur) => {
let exists = acc && acc.hasOwnProperty(cur.ruleId);
if (exists) acc[cur.ruleId].push(`${cur.line}:${cur.column}`);
else acc[cur.ruleId] = [`${cur.line}:${cur.column}`];
return acc;
}, {});
});
let output = '';
const isObjFile = obj => (obj && (obj.hasOwnProperty('errors') || obj.hasOwnProperty('warning')));
function theLoop(obj, key, indent, last_key=true) {
let isFile = isObjFile(obj);
let indent_string = indent.slice(0, indent.length-1).map(ind => dim(ind==1 ? ' │ ' : ' ')).join('');
output += indent_string;
if (key) {
output += dim(last_key ? ' └ ' : ' ├ ');
output += isFile ? lightgrey(`${key}`) : ul(`${key}`);
} else output += ul('Project');
output += '\n';
if (isFile) {
// print errors
let last_indent = dim(last_key ? ' ' : ' │ ');
if (obj.error_count > 0) {
let error_string = Object.keys(obj.errors).map(k => `${k}(${obj.errors[k].join(',')})`).join(', ');
output += `${indent_string}${last_indent} ${logSymbols.error} `;
output += red(`${obj.error_count} errors: [${error_string}]`);
output += '\n';
}
if (obj.warning_count > 0) {
let warning_string = Object.keys(obj.warnings).map(k => `${k}(${obj.warnings[k].join(',')})`).join(', ');
output += `${indent_string}${last_indent} ${logSymbols.warning} `;
output += yellow(` ${obj.warning_count} warnings: [${warning_string}]`);
output += '\n';
}
} else {
// object is folder
let child_keys = Object.keys(obj).sort((ka,kb) => {
// sort keys: files first, alphabetic
let oa = obj[ka], ob = obj[kb];
if (isObjFile(oa) === isObjFile(ob)) return (ka < kb) ? -1 : (ka > kb ? 1 : 0);
else if (isObjFile(oa)) return -1;
else return 1;
});
if (child_keys) child_keys.forEach((k,i,a) => theLoop(obj[k],k, [...indent, (i < a.length-1) ? 1 : 0], i == a.length-1));
}
}
theLoop(nested, null, []);
final = output;
return final;
};
function ul(text) {
return `\x1b[4m${text}\x1b[0m`;
}
function dim(text) {
return `\x1b[2m${text}\x1b[22m`;
}
function lightgrey(text) {
return `\x1b[37m${text}\x1b[0m`;
}
function red(text) {
return `\x1b[31m${text}\x1b[0m`;
}
function yellow(text) {
return `\x1b[33m${text}\x1b[0m`;
}