Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 10cf07e

Browse files
committed
chore(logger): move runtime logger into own file
1 parent 8836310 commit 10cf07e

File tree

4 files changed

+163
-157
lines changed

4 files changed

+163
-157
lines changed

src/dev-server/notification-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Ionic Dev Server: Server Side Logger
22
import { Logger } from '../logger/logger';
3-
import { hasDiagnostics, getDiagnosticsHtmlContent, generateRuntimeDiagnosticContent } from '../logger/logger-diagnostics';
3+
import { generateRuntimeDiagnosticContent } from '../logger/logger-runtime';
4+
import { hasDiagnostics, getDiagnosticsHtmlContent } from '../logger/logger-diagnostics';
45
import { on, EventType } from '../util/events';
56
import { Server as WebSocketServer } from 'ws';
67
import { ServeConfig } from './serve-config';

src/logger/logger-diagnostics.ts

Lines changed: 5 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { BuildContext, Diagnostic, PrintLine } from '../util/interfaces';
2+
import { escapeHtml, titleCase } from '../util/helpers';
3+
import { highlightError } from '../highlight/highlight';
4+
import { join } from 'path';
25
import { Logger } from './logger';
3-
import { join, resolve , normalize} from 'path';
4-
import { highlight, highlightError } from '../highlight/highlight';
56
import { readFileSync, unlinkSync, writeFileSync } from 'fs';
6-
import { splitLineBreaks, titleCase } from '../util/helpers';
77
import * as chalk from 'chalk';
88

99

@@ -205,7 +205,7 @@ export function getDiagnosticsHtmlContent(buildDir: string, includeDiagnosticsHt
205205
}
206206

207207

208-
function generateDiagnosticHtml(d: Diagnostic) {
208+
export function generateDiagnosticHtml(d: Diagnostic) {
209209
const c: string[] = [];
210210

211211
c.push(`<div class="ion-diagnostic">`);
@@ -227,7 +227,7 @@ function generateDiagnosticHtml(d: Diagnostic) {
227227
}
228228

229229

230-
function generateCodeBlock(d: Diagnostic) {
230+
export function generateCodeBlock(d: Diagnostic) {
231231
const c: string[] = [];
232232

233233
c.push(`<div class="ion-diagnostic-file">`);
@@ -260,147 +260,6 @@ function generateCodeBlock(d: Diagnostic) {
260260
}
261261

262262

263-
export function generateRuntimeDiagnosticContent(rootDir: string, buildDir: string, runtimeErrorMessage: string, runtimeErrorStack: string) {
264-
let c: string[] = [];
265-
266-
c.push(`<div class="ion-diagnostic">`);
267-
c.push(`<div class="ion-diagnostic-masthead">`);
268-
c.push(`<div class="ion-diagnostic-header">Runtime Error</div>`);
269-
270-
if (runtimeErrorMessage) {
271-
runtimeErrorMessage = runtimeErrorMessage.replace(/inline template:\d+:\d+/g, '');
272-
runtimeErrorMessage = runtimeErrorMessage.replace('inline template', '');
273-
274-
c.push(`<div class="ion-diagnostic-message">${escapeHtml(runtimeErrorMessage)}</div>`);
275-
}
276-
c.push(`</div>`); // .ion-diagnostic-masthead
277-
278-
const diagnosticsHtmlCache = generateRuntimeStackDiagnostics(rootDir, runtimeErrorStack);
279-
diagnosticsHtmlCache.forEach(d => {
280-
c.push(generateCodeBlock(d));
281-
});
282-
283-
if (runtimeErrorStack) {
284-
c.push(`<div class="ion-diagnostic-stack-header">Stack</div>`);
285-
c.push(`<div class="ion-diagnostic-stack">${escapeHtml(runtimeErrorStack)}</div>`);
286-
}
287-
288-
c.push(`</div>`); // .ion-diagnostic
289-
290-
return getDiagnosticsHtmlContent(buildDir, c.join('\n'));
291-
}
292-
293-
294-
export function generateRuntimeStackDiagnostics(rootDir: string, stack: string) {
295-
const diagnostics: Diagnostic[] = [];
296-
297-
if (stack) {
298-
splitLineBreaks(stack).forEach(stackLine => {
299-
try {
300-
const match = WEBPACK_FILE_REGEX.exec(stackLine);
301-
if (!match) return;
302-
303-
const fileSplit = match[1].split('?');
304-
if (fileSplit.length !== 2) return;
305-
306-
const linesSplit = fileSplit[1].split(':');
307-
if (linesSplit.length !== 3) return;
308-
309-
const fileName = fileSplit[0];
310-
if (fileName.indexOf('~') > -1) return;
311-
312-
const errorLineNumber = parseInt(linesSplit[1], 10);
313-
const errorCharNumber = parseInt(linesSplit[2], 10);
314-
315-
const d: Diagnostic = {
316-
level: 'error',
317-
language: 'typescript',
318-
type: 'runtime',
319-
header: '',
320-
code: 'runtime',
321-
messageText: '',
322-
absFileName: resolve(rootDir, fileName),
323-
relFileName: normalize(fileName),
324-
lines: []
325-
};
326-
327-
const sourceText = readFileSync(d.absFileName, 'utf8');
328-
const srcLines = splitLineBreaks(sourceText);
329-
if (!srcLines.length || errorLineNumber >= srcLines.length) return;
330-
331-
let htmlLines = srcLines;
332-
333-
try {
334-
htmlLines = splitLineBreaks(highlight(d.language, sourceText, true).value);
335-
} catch (e) {}
336-
337-
const errorLine: PrintLine = {
338-
lineIndex: errorLineNumber - 1,
339-
lineNumber: errorLineNumber,
340-
text: srcLines[errorLineNumber - 1],
341-
html: htmlLines[errorLineNumber - 1],
342-
errorCharStart: errorCharNumber + 1,
343-
errorLength: 1
344-
};
345-
346-
if (errorLine.html.indexOf('class="hljs') === -1) {
347-
try {
348-
errorLine.html = highlight(d.language, errorLine.text, true).value;
349-
} catch (e) {}
350-
}
351-
352-
d.lines.push(errorLine);
353-
354-
if (errorLine.lineIndex > 0) {
355-
const previousLine: PrintLine = {
356-
lineIndex: errorLine.lineIndex - 1,
357-
lineNumber: errorLine.lineNumber - 1,
358-
text: srcLines[errorLine.lineIndex - 1],
359-
html: htmlLines[errorLine.lineIndex - 1],
360-
errorCharStart: -1,
361-
errorLength: -1
362-
};
363-
364-
if (previousLine.html.indexOf('class="hljs') === -1) {
365-
try {
366-
previousLine.html = highlight(d.language, previousLine.text, true).value;
367-
} catch (e) {}
368-
}
369-
370-
d.lines.unshift(previousLine);
371-
}
372-
373-
if (errorLine.lineIndex < srcLines.length) {
374-
const nextLine: PrintLine = {
375-
lineIndex: errorLine.lineIndex + 1,
376-
lineNumber: errorLine.lineNumber + 1,
377-
text: srcLines[errorLine.lineIndex + 1],
378-
html: htmlLines[errorLine.lineIndex + 1],
379-
errorCharStart: -1,
380-
errorLength: -1
381-
};
382-
383-
if (nextLine.html.indexOf('class="hljs') === -1) {
384-
try {
385-
nextLine.html = highlight(d.language, nextLine.text, true).value;
386-
} catch (e) {}
387-
}
388-
389-
d.lines.push(nextLine);
390-
}
391-
392-
diagnostics.push(d);
393-
394-
} catch (e) {}
395-
});
396-
}
397-
398-
return diagnostics;
399-
};
400-
401-
const WEBPACK_FILE_REGEX = /\(webpack:\/\/\/(.*?)\)/;
402-
403-
404263
function jsConsoleSyntaxHighlight(text: string) {
405264
if (text.trim().startsWith('//')) {
406265
return chalk.dim(text);
@@ -444,16 +303,6 @@ function cssConsoleSyntaxHighlight(text: string, errorCharStart: number) {
444303
}
445304

446305

447-
function escapeHtml(unsafe: string) {
448-
return unsafe
449-
.replace(/&/g, '&amp;')
450-
.replace(/</g, '&lt;')
451-
.replace(/>/g, '&gt;')
452-
.replace(/"/g, '&quot;')
453-
.replace(/'/g, '&#039;');
454-
}
455-
456-
457306
function prepareLines(orgLines: PrintLine[], code: 'text'|'html') {
458307
const lines: PrintLine[] = JSON.parse(JSON.stringify(orgLines));
459308

src/logger/logger-runtime.ts

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

src/util/helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,12 @@ export function changeExtension(filePath: string, newExtension: string) {
145145
const newFileName = extensionlessfileName + newExtension;
146146
return join(dir, newFileName);
147147
}
148+
149+
export function escapeHtml(unsafe: string) {
150+
return unsafe
151+
.replace(/&/g, '&amp;')
152+
.replace(/</g, '&lt;')
153+
.replace(/>/g, '&gt;')
154+
.replace(/"/g, '&quot;')
155+
.replace(/'/g, '&#039;');
156+
}

0 commit comments

Comments
 (0)