forked from ionic-team/ionic-app-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger-diagnostics.js
More file actions
295 lines (295 loc) Β· 11 KB
/
logger-diagnostics.js
File metadata and controls
295 lines (295 loc) Β· 11 KB
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var helpers_1 = require("../util/helpers");
var highlight_1 = require("../highlight/highlight");
var path_1 = require("path");
var logger_1 = require("./logger");
var fs_1 = require("fs");
var chalk = require("chalk");
function printDiagnostics(context, diagnosticsType, diagnostics, consoleLogDiagnostics, writeHtmlDiagnostics) {
if (diagnostics && diagnostics.length) {
if (consoleLogDiagnostics) {
diagnostics.forEach(consoleLogDiagnostic);
}
if (writeHtmlDiagnostics) {
var content = diagnostics.map(generateDiagnosticHtml);
var fileName = getDiagnosticsFileName(context.buildDir, diagnosticsType);
fs_1.writeFileSync(fileName, content.join('\n'), { encoding: 'utf8' });
}
}
}
exports.printDiagnostics = printDiagnostics;
function consoleLogDiagnostic(d) {
if (d.level === 'warn') {
logger_1.Logger.warn(d.header);
}
else {
logger_1.Logger.error(d.header);
}
logger_1.Logger.wordWrap([d.messageText]).forEach(function (m) {
console.log(m);
});
console.log('');
if (d.lines && d.lines.length) {
var lines = prepareLines(d.lines, 'text');
lines.forEach(function (l) {
if (!isMeaningfulLine(l.text)) {
return;
}
var msg = "L" + l.lineNumber + ": ";
while (msg.length < logger_1.Logger.INDENT.length) {
msg = ' ' + msg;
}
var text = l.text;
if (l.errorCharStart > -1) {
text = consoleHighlightError(text, l.errorCharStart, l.errorLength);
}
msg = chalk.dim(msg);
if (d.language === 'javascript') {
msg += jsConsoleSyntaxHighlight(text);
}
else if (d.language === 'scss') {
msg += cssConsoleSyntaxHighlight(text, l.errorCharStart);
}
else {
msg += text;
}
console.log(msg);
});
console.log('');
}
}
function consoleHighlightError(errorLine, errorCharStart, errorLength) {
var rightSideChars = errorLine.length - errorCharStart + errorLength - 1;
while (errorLine.length + logger_1.Logger.INDENT.length > logger_1.Logger.MAX_LEN) {
if (errorCharStart > (errorLine.length - errorCharStart + errorLength) && errorCharStart > 5) {
// larger on left side
errorLine = errorLine.substr(1);
errorCharStart--;
}
else if (rightSideChars > 1) {
// larger on right side
errorLine = errorLine.substr(0, errorLine.length - 1);
rightSideChars--;
}
else {
break;
}
}
var lineChars = [];
var lineLength = Math.max(errorLine.length, errorCharStart + errorLength);
for (var i = 0; i < lineLength; i++) {
var chr = errorLine.charAt(i);
if (i >= errorCharStart && i < errorCharStart + errorLength) {
chr = chalk.bgRed(chr === '' ? ' ' : chr);
}
lineChars.push(chr);
}
return lineChars.join('');
}
var diagnosticsHtmlCache = {};
function clearDiagnosticsCache() {
diagnosticsHtmlCache = {};
}
exports.clearDiagnosticsCache = clearDiagnosticsCache;
function clearDiagnostics(context, type) {
try {
delete diagnosticsHtmlCache[type];
fs_1.unlinkSync(getDiagnosticsFileName(context.buildDir, type));
}
catch (e) { }
}
exports.clearDiagnostics = clearDiagnostics;
function hasDiagnostics(buildDir) {
loadBuildDiagnosticsHtml(buildDir);
var keys = Object.keys(diagnosticsHtmlCache);
for (var i = 0; i < keys.length; i++) {
if (typeof diagnosticsHtmlCache[keys[i]] === 'string') {
return true;
}
}
return false;
}
exports.hasDiagnostics = hasDiagnostics;
function loadBuildDiagnosticsHtml(buildDir) {
try {
if (diagnosticsHtmlCache[exports.DiagnosticsType.TypeScript] === undefined) {
diagnosticsHtmlCache[exports.DiagnosticsType.TypeScript] = fs_1.readFileSync(getDiagnosticsFileName(buildDir, exports.DiagnosticsType.TypeScript), 'utf8');
}
}
catch (e) {
diagnosticsHtmlCache[exports.DiagnosticsType.TypeScript] = false;
}
try {
if (diagnosticsHtmlCache[exports.DiagnosticsType.Sass] === undefined) {
diagnosticsHtmlCache[exports.DiagnosticsType.Sass] = fs_1.readFileSync(getDiagnosticsFileName(buildDir, exports.DiagnosticsType.Sass), 'utf8');
}
}
catch (e) {
diagnosticsHtmlCache[exports.DiagnosticsType.Sass] = false;
}
}
function injectDiagnosticsHtml(buildDir, content) {
if (!hasDiagnostics(buildDir)) {
return content;
}
var contentStr = content.toString();
var c = [];
c.push("<div id=\"ion-diagnostics\">");
// diagnostics content
c.push(getDiagnosticsHtmlContent(buildDir));
c.push("</div>"); // #ion-diagnostics
var match = contentStr.match(/<body>(?![\s\S]*<body>)/i);
if (match) {
contentStr = contentStr.replace(match[0], match[0] + '\n' + c.join('\n'));
}
else {
contentStr = c.join('\n') + contentStr;
}
return contentStr;
}
exports.injectDiagnosticsHtml = injectDiagnosticsHtml;
function getDiagnosticsHtmlContent(buildDir, includeDiagnosticsHtml) {
var c = [];
// diagnostics header
c.push("\n <div class=\"ion-diagnostics-header\">\n <div class=\"ion-diagnostics-header-content\">\n <div class=\"ion-diagnostics-header-inner\">Error</div>\n <div class=\"ion-diagnostics-buttons\">\n <button id=\"ion-diagnostic-close\">Close</button>\n </div>\n </div>\n </div>\n ");
c.push("<div class=\"ion-diagnostics-content\">");
if (includeDiagnosticsHtml) {
c.push(includeDiagnosticsHtml);
}
loadBuildDiagnosticsHtml(buildDir);
var keys = Object.keys(diagnosticsHtmlCache);
for (var i = 0; i < keys.length; i++) {
if (typeof diagnosticsHtmlCache[keys[i]] === 'string') {
c.push(diagnosticsHtmlCache[keys[i]]);
}
}
c.push("</div>");
return c.join('\n');
}
exports.getDiagnosticsHtmlContent = getDiagnosticsHtmlContent;
function generateDiagnosticHtml(d) {
var c = [];
c.push("<div class=\"ion-diagnostic\">");
c.push("<div class=\"ion-diagnostic-masthead\" title=\"" + helpers_1.escapeHtml(d.type) + " error: " + helpers_1.escapeHtml(d.code) + "\">");
var title = helpers_1.titleCase(d.type) + " " + helpers_1.titleCase(d.level);
c.push("<div class=\"ion-diagnostic-title\">" + helpers_1.escapeHtml(title) + "</div>");
c.push("<div class=\"ion-diagnostic-message\" data-error-code=\"" + helpers_1.escapeHtml(d.type) + "-" + helpers_1.escapeHtml(d.code) + "\">" + helpers_1.escapeHtml(d.messageText) + "</div>");
c.push("</div>"); // .ion-diagnostic-masthead
c.push(generateCodeBlock(d));
c.push("</div>"); // .ion-diagnostic
return c.join('\n');
}
exports.generateDiagnosticHtml = generateDiagnosticHtml;
function generateCodeBlock(d) {
var c = [];
c.push("<div class=\"ion-diagnostic-file\">");
c.push("<div class=\"ion-diagnostic-file-header\" title=\"" + helpers_1.escapeHtml(d.absFileName) + "\">" + helpers_1.escapeHtml(d.relFileName) + "</div>");
if (d.lines && d.lines.length) {
c.push("<div class=\"ion-diagnostic-blob\">");
c.push("<table class=\"ion-diagnostic-table\">");
prepareLines(d.lines, 'html').forEach(function (l) {
c.push("<tr" + ((l.errorCharStart > -1) ? ' class="ion-diagnostic-error-line"' : '') + ">");
c.push("<td class=\"ion-diagnostic-blob-num\" data-line-number=\"" + l.lineNumber + "\"></td>");
c.push("<td class=\"ion-diagnostic-blob-code\">" + highlight_1.highlightError(l.html, l.errorCharStart, l.errorLength) + "</td>");
c.push("</tr>");
});
c.push("</table>");
c.push("</div>"); // .ion-diagnostic-blob
}
c.push("</div>"); // .ion-diagnostic-file
return c.join('\n');
}
exports.generateCodeBlock = generateCodeBlock;
function jsConsoleSyntaxHighlight(text) {
if (text.trim().startsWith('//')) {
return chalk.dim(text);
}
var words = text.split(' ').map(function (word) {
if (JS_KEYWORDS.indexOf(word) > -1) {
return chalk.cyan(word);
}
return word;
});
return words.join(' ');
}
function cssConsoleSyntaxHighlight(text, errorCharStart) {
var cssProp = true;
var safeChars = 'abcdefghijklmnopqrstuvwxyz-_';
var notProp = '.#,:}@$[]/*';
var chars = [];
for (var i = 0; i < text.length; i++) {
var c = text.charAt(i);
if (c === ';' || c === '{') {
cssProp = true;
}
else if (notProp.indexOf(c) > -1) {
cssProp = false;
}
if (cssProp && safeChars.indexOf(c.toLowerCase()) > -1) {
chars.push(chalk.cyan(c));
continue;
}
chars.push(c);
}
return chars.join('');
}
function prepareLines(orgLines, code) {
var lines = JSON.parse(JSON.stringify(orgLines));
for (var i = 0; i < 100; i++) {
if (!eachLineHasLeadingWhitespace(lines, code)) {
return lines;
}
for (var i_1 = 0; i_1 < lines.length; i_1++) {
lines[i_1][code] = lines[i_1][code].substr(1);
lines[i_1].errorCharStart--;
if (!lines[i_1][code].length) {
return lines;
}
}
}
return lines;
}
function eachLineHasLeadingWhitespace(lines, code) {
if (!lines.length) {
return false;
}
for (var i = 0; i < lines.length; i++) {
if (!lines[i][code] || lines[i][code].length < 1) {
return false;
}
var firstChar = lines[i][code].charAt(0);
if (firstChar !== ' ' && firstChar !== '\t') {
return false;
}
}
return true;
}
var JS_KEYWORDS = [
'abstract', 'any', 'as', 'break', 'boolean', 'case', 'catch', 'class',
'console', 'const', 'continue', 'debugger', 'declare', 'default', 'delete',
'do', 'else', 'enum', 'export', 'extends', 'false', 'finally', 'for', 'from',
'function', 'get', 'if', 'import', 'in', 'implements', 'Infinity',
'instanceof', 'let', 'module', 'namespace', 'NaN', 'new', 'number', 'null',
'public', 'private', 'protected', 'require', 'return', 'static', 'set',
'string', 'super', 'switch', 'this', 'throw', 'try', 'true', 'type',
'typeof', 'undefined', 'var', 'void', 'with', 'while', 'yield',
];
function getDiagnosticsFileName(buildDir, type) {
return path_1.join(buildDir, ".ion-diagnostic-" + type + ".html");
}
function isMeaningfulLine(line) {
if (line) {
line = line.trim();
if (line.length) {
return (MEH_LINES.indexOf(line) < 0);
}
}
return false;
}
var MEH_LINES = [';', ':', '{', '}', '(', ')', '/**', '/*', '*/', '*', '({', '})'];
exports.DiagnosticsType = {
TypeScript: 'typescript',
Sass: 'sass',
TsLint: 'tslint'
};