UNPKG

5.87 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var helpers_1 = require("../util/helpers");
4var logger_diagnostics_1 = require("./logger-diagnostics");
5var highlight_1 = require("../highlight/highlight");
6var fs_1 = require("fs");
7var path_1 = require("path");
8function generateRuntimeDiagnosticContent(rootDir, buildDir, runtimeErrorMessage, runtimeErrorStack) {
9 var c = [];
10 c.push("<div class=\"ion-diagnostic\">");
11 c.push("<div class=\"ion-diagnostic-masthead\">");
12 c.push("<div class=\"ion-diagnostic-header\">Runtime Error</div>");
13 if (runtimeErrorMessage) {
14 runtimeErrorMessage = runtimeErrorMessage.replace(/inline template:\d+:\d+/g, '');
15 runtimeErrorMessage = runtimeErrorMessage.replace('inline template', '');
16 c.push("<div class=\"ion-diagnostic-message\">" + helpers_1.escapeHtml(runtimeErrorMessage) + "</div>");
17 }
18 c.push("</div>"); // .ion-diagnostic-masthead
19 var diagnosticsHtmlCache = generateRuntimeStackDiagnostics(rootDir, runtimeErrorStack);
20 diagnosticsHtmlCache.forEach(function (d) {
21 c.push(logger_diagnostics_1.generateCodeBlock(d));
22 });
23 if (runtimeErrorStack) {
24 c.push("<div class=\"ion-diagnostic-stack-header\">Stack</div>");
25 c.push("<div class=\"ion-diagnostic-stack\">" + helpers_1.escapeHtml(runtimeErrorStack) + "</div>");
26 }
27 c.push("</div>"); // .ion-diagnostic
28 return logger_diagnostics_1.getDiagnosticsHtmlContent(buildDir, c.join('\n'));
29}
30exports.generateRuntimeDiagnosticContent = generateRuntimeDiagnosticContent;
31function generateRuntimeStackDiagnostics(rootDir, stack) {
32 var diagnostics = [];
33 if (stack) {
34 helpers_1.splitLineBreaks(stack).forEach(function (stackLine) {
35 try {
36 var match = WEBPACK_FILE_REGEX.exec(stackLine);
37 if (!match)
38 return;
39 var fileSplit = match[1].split('?');
40 if (fileSplit.length !== 2)
41 return;
42 var linesSplit = fileSplit[1].split(':');
43 if (linesSplit.length !== 3)
44 return;
45 var fileName = fileSplit[0];
46 if (fileName.indexOf('~') > -1)
47 return;
48 var errorLineNumber = parseInt(linesSplit[1], 10);
49 var errorCharNumber = parseInt(linesSplit[2], 10);
50 var d = {
51 level: 'error',
52 language: 'typescript',
53 type: 'runtime',
54 header: '',
55 code: 'runtime',
56 messageText: '',
57 absFileName: path_1.resolve(rootDir, fileName),
58 relFileName: path_1.normalize(fileName),
59 lines: []
60 };
61 var sourceText = fs_1.readFileSync(d.absFileName, 'utf8');
62 var srcLines = helpers_1.splitLineBreaks(sourceText);
63 if (!srcLines.length || errorLineNumber >= srcLines.length)
64 return;
65 var htmlLines = srcLines;
66 try {
67 htmlLines = helpers_1.splitLineBreaks(highlight_1.highlight(d.language, sourceText, true).value);
68 }
69 catch (e) { }
70 var errorLine = {
71 lineIndex: errorLineNumber - 1,
72 lineNumber: errorLineNumber,
73 text: srcLines[errorLineNumber - 1],
74 html: htmlLines[errorLineNumber - 1],
75 errorCharStart: errorCharNumber + 1,
76 errorLength: 1
77 };
78 if (errorLine.html.indexOf('class="hljs') === -1) {
79 try {
80 errorLine.html = highlight_1.highlight(d.language, errorLine.text, true).value;
81 }
82 catch (e) { }
83 }
84 d.lines.push(errorLine);
85 if (errorLine.lineIndex > 0) {
86 var previousLine = {
87 lineIndex: errorLine.lineIndex - 1,
88 lineNumber: errorLine.lineNumber - 1,
89 text: srcLines[errorLine.lineIndex - 1],
90 html: htmlLines[errorLine.lineIndex - 1],
91 errorCharStart: -1,
92 errorLength: -1
93 };
94 if (previousLine.html.indexOf('class="hljs') === -1) {
95 try {
96 previousLine.html = highlight_1.highlight(d.language, previousLine.text, true).value;
97 }
98 catch (e) { }
99 }
100 d.lines.unshift(previousLine);
101 }
102 if (errorLine.lineIndex < srcLines.length) {
103 var nextLine = {
104 lineIndex: errorLine.lineIndex + 1,
105 lineNumber: errorLine.lineNumber + 1,
106 text: srcLines[errorLine.lineIndex + 1],
107 html: htmlLines[errorLine.lineIndex + 1],
108 errorCharStart: -1,
109 errorLength: -1
110 };
111 if (nextLine.html.indexOf('class="hljs') === -1) {
112 try {
113 nextLine.html = highlight_1.highlight(d.language, nextLine.text, true).value;
114 }
115 catch (e) { }
116 }
117 d.lines.push(nextLine);
118 }
119 diagnostics.push(d);
120 }
121 catch (e) { }
122 });
123 }
124 return diagnostics;
125}
126exports.generateRuntimeStackDiagnostics = generateRuntimeStackDiagnostics;
127var WEBPACK_FILE_REGEX = /\(webpack:\/\/\/(.*?)\)/;