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

Commit

Permalink
chore(logger): move runtime logger into own file
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Nov 12, 2016
1 parent 8836310 commit 10cf07e
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 157 deletions.
3 changes: 2 additions & 1 deletion src/dev-server/notification-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Ionic Dev Server: Server Side Logger
import { Logger } from '../logger/logger';
import { hasDiagnostics, getDiagnosticsHtmlContent, generateRuntimeDiagnosticContent } from '../logger/logger-diagnostics';
import { generateRuntimeDiagnosticContent } from '../logger/logger-runtime';
import { hasDiagnostics, getDiagnosticsHtmlContent } from '../logger/logger-diagnostics';
import { on, EventType } from '../util/events';
import { Server as WebSocketServer } from 'ws';
import { ServeConfig } from './serve-config';
Expand Down
161 changes: 5 additions & 156 deletions src/logger/logger-diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BuildContext, Diagnostic, PrintLine } from '../util/interfaces';
import { escapeHtml, titleCase } from '../util/helpers';
import { highlightError } from '../highlight/highlight';
import { join } from 'path';
import { Logger } from './logger';
import { join, resolve , normalize} from 'path';
import { highlight, highlightError } from '../highlight/highlight';
import { readFileSync, unlinkSync, writeFileSync } from 'fs';
import { splitLineBreaks, titleCase } from '../util/helpers';
import * as chalk from 'chalk';


Expand Down Expand Up @@ -205,7 +205,7 @@ export function getDiagnosticsHtmlContent(buildDir: string, includeDiagnosticsHt
}


function generateDiagnosticHtml(d: Diagnostic) {
export function generateDiagnosticHtml(d: Diagnostic) {
const c: string[] = [];

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


function generateCodeBlock(d: Diagnostic) {
export function generateCodeBlock(d: Diagnostic) {
const c: string[] = [];

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


export function generateRuntimeDiagnosticContent(rootDir: string, buildDir: string, runtimeErrorMessage: string, runtimeErrorStack: string) {
let c: string[] = [];

c.push(`<div class="ion-diagnostic">`);
c.push(`<div class="ion-diagnostic-masthead">`);
c.push(`<div class="ion-diagnostic-header">Runtime Error</div>`);

if (runtimeErrorMessage) {
runtimeErrorMessage = runtimeErrorMessage.replace(/inline template:\d+:\d+/g, '');
runtimeErrorMessage = runtimeErrorMessage.replace('inline template', '');

c.push(`<div class="ion-diagnostic-message">${escapeHtml(runtimeErrorMessage)}</div>`);
}
c.push(`</div>`); // .ion-diagnostic-masthead

const diagnosticsHtmlCache = generateRuntimeStackDiagnostics(rootDir, runtimeErrorStack);
diagnosticsHtmlCache.forEach(d => {
c.push(generateCodeBlock(d));
});

if (runtimeErrorStack) {
c.push(`<div class="ion-diagnostic-stack-header">Stack</div>`);
c.push(`<div class="ion-diagnostic-stack">${escapeHtml(runtimeErrorStack)}</div>`);
}

c.push(`</div>`); // .ion-diagnostic

return getDiagnosticsHtmlContent(buildDir, c.join('\n'));
}


export function generateRuntimeStackDiagnostics(rootDir: string, stack: string) {
const diagnostics: Diagnostic[] = [];

if (stack) {
splitLineBreaks(stack).forEach(stackLine => {
try {
const match = WEBPACK_FILE_REGEX.exec(stackLine);
if (!match) return;

const fileSplit = match[1].split('?');
if (fileSplit.length !== 2) return;

const linesSplit = fileSplit[1].split(':');
if (linesSplit.length !== 3) return;

const fileName = fileSplit[0];
if (fileName.indexOf('~') > -1) return;

const errorLineNumber = parseInt(linesSplit[1], 10);
const errorCharNumber = parseInt(linesSplit[2], 10);

const d: Diagnostic = {
level: 'error',
language: 'typescript',
type: 'runtime',
header: '',
code: 'runtime',
messageText: '',
absFileName: resolve(rootDir, fileName),
relFileName: normalize(fileName),
lines: []
};

const sourceText = readFileSync(d.absFileName, 'utf8');
const srcLines = splitLineBreaks(sourceText);
if (!srcLines.length || errorLineNumber >= srcLines.length) return;

let htmlLines = srcLines;

try {
htmlLines = splitLineBreaks(highlight(d.language, sourceText, true).value);
} catch (e) {}

const errorLine: PrintLine = {
lineIndex: errorLineNumber - 1,
lineNumber: errorLineNumber,
text: srcLines[errorLineNumber - 1],
html: htmlLines[errorLineNumber - 1],
errorCharStart: errorCharNumber + 1,
errorLength: 1
};

if (errorLine.html.indexOf('class="hljs') === -1) {
try {
errorLine.html = highlight(d.language, errorLine.text, true).value;
} catch (e) {}
}

d.lines.push(errorLine);

if (errorLine.lineIndex > 0) {
const previousLine: PrintLine = {
lineIndex: errorLine.lineIndex - 1,
lineNumber: errorLine.lineNumber - 1,
text: srcLines[errorLine.lineIndex - 1],
html: htmlLines[errorLine.lineIndex - 1],
errorCharStart: -1,
errorLength: -1
};

if (previousLine.html.indexOf('class="hljs') === -1) {
try {
previousLine.html = highlight(d.language, previousLine.text, true).value;
} catch (e) {}
}

d.lines.unshift(previousLine);
}

if (errorLine.lineIndex < srcLines.length) {
const nextLine: PrintLine = {
lineIndex: errorLine.lineIndex + 1,
lineNumber: errorLine.lineNumber + 1,
text: srcLines[errorLine.lineIndex + 1],
html: htmlLines[errorLine.lineIndex + 1],
errorCharStart: -1,
errorLength: -1
};

if (nextLine.html.indexOf('class="hljs') === -1) {
try {
nextLine.html = highlight(d.language, nextLine.text, true).value;
} catch (e) {}
}

d.lines.push(nextLine);
}

diagnostics.push(d);

} catch (e) {}
});
}

return diagnostics;
};

const WEBPACK_FILE_REGEX = /\(webpack:\/\/\/(.*?)\)/;


function jsConsoleSyntaxHighlight(text: string) {
if (text.trim().startsWith('//')) {
return chalk.dim(text);
Expand Down Expand Up @@ -444,16 +303,6 @@ function cssConsoleSyntaxHighlight(text: string, errorCharStart: number) {
}


function escapeHtml(unsafe: string) {
return unsafe
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}


function prepareLines(orgLines: PrintLine[], code: 'text'|'html') {
const lines: PrintLine[] = JSON.parse(JSON.stringify(orgLines));

Expand Down
147 changes: 147 additions & 0 deletions src/logger/logger-runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { Diagnostic, PrintLine } from '../util/interfaces';
import { escapeHtml, splitLineBreaks } from '../util/helpers';
import { generateCodeBlock, getDiagnosticsHtmlContent } from './logger-diagnostics';
import { highlight } from '../highlight/highlight';
import { readFileSync } from 'fs';
import { resolve , normalize} from 'path';


export function generateRuntimeDiagnosticContent(rootDir: string, buildDir: string, runtimeErrorMessage: string, runtimeErrorStack: string) {
let c: string[] = [];

c.push(`<div class="ion-diagnostic">`);
c.push(`<div class="ion-diagnostic-masthead">`);
c.push(`<div class="ion-diagnostic-header">Runtime Error</div>`);

if (runtimeErrorMessage) {
runtimeErrorMessage = runtimeErrorMessage.replace(/inline template:\d+:\d+/g, '');
runtimeErrorMessage = runtimeErrorMessage.replace('inline template', '');

c.push(`<div class="ion-diagnostic-message">${escapeHtml(runtimeErrorMessage)}</div>`);
}
c.push(`</div>`); // .ion-diagnostic-masthead

const diagnosticsHtmlCache = generateRuntimeStackDiagnostics(rootDir, runtimeErrorStack);
diagnosticsHtmlCache.forEach(d => {
c.push(generateCodeBlock(d));
});

if (runtimeErrorStack) {
c.push(`<div class="ion-diagnostic-stack-header">Stack</div>`);
c.push(`<div class="ion-diagnostic-stack">${escapeHtml(runtimeErrorStack)}</div>`);
}

c.push(`</div>`); // .ion-diagnostic

return getDiagnosticsHtmlContent(buildDir, c.join('\n'));
}


export function generateRuntimeStackDiagnostics(rootDir: string, stack: string) {
const diagnostics: Diagnostic[] = [];

if (stack) {
splitLineBreaks(stack).forEach(stackLine => {
try {
const match = WEBPACK_FILE_REGEX.exec(stackLine);
if (!match) return;

const fileSplit = match[1].split('?');
if (fileSplit.length !== 2) return;

const linesSplit = fileSplit[1].split(':');
if (linesSplit.length !== 3) return;

const fileName = fileSplit[0];
if (fileName.indexOf('~') > -1) return;

const errorLineNumber = parseInt(linesSplit[1], 10);
const errorCharNumber = parseInt(linesSplit[2], 10);

const d: Diagnostic = {
level: 'error',
language: 'typescript',
type: 'runtime',
header: '',
code: 'runtime',
messageText: '',
absFileName: resolve(rootDir, fileName),
relFileName: normalize(fileName),
lines: []
};

const sourceText = readFileSync(d.absFileName, 'utf8');
const srcLines = splitLineBreaks(sourceText);
if (!srcLines.length || errorLineNumber >= srcLines.length) return;

let htmlLines = srcLines;

try {
htmlLines = splitLineBreaks(highlight(d.language, sourceText, true).value);
} catch (e) {}

const errorLine: PrintLine = {
lineIndex: errorLineNumber - 1,
lineNumber: errorLineNumber,
text: srcLines[errorLineNumber - 1],
html: htmlLines[errorLineNumber - 1],
errorCharStart: errorCharNumber + 1,
errorLength: 1
};

if (errorLine.html.indexOf('class="hljs') === -1) {
try {
errorLine.html = highlight(d.language, errorLine.text, true).value;
} catch (e) {}
}

d.lines.push(errorLine);

if (errorLine.lineIndex > 0) {
const previousLine: PrintLine = {
lineIndex: errorLine.lineIndex - 1,
lineNumber: errorLine.lineNumber - 1,
text: srcLines[errorLine.lineIndex - 1],
html: htmlLines[errorLine.lineIndex - 1],
errorCharStart: -1,
errorLength: -1
};

if (previousLine.html.indexOf('class="hljs') === -1) {
try {
previousLine.html = highlight(d.language, previousLine.text, true).value;
} catch (e) {}
}

d.lines.unshift(previousLine);
}

if (errorLine.lineIndex < srcLines.length) {
const nextLine: PrintLine = {
lineIndex: errorLine.lineIndex + 1,
lineNumber: errorLine.lineNumber + 1,
text: srcLines[errorLine.lineIndex + 1],
html: htmlLines[errorLine.lineIndex + 1],
errorCharStart: -1,
errorLength: -1
};

if (nextLine.html.indexOf('class="hljs') === -1) {
try {
nextLine.html = highlight(d.language, nextLine.text, true).value;
} catch (e) {}
}

d.lines.push(nextLine);
}

diagnostics.push(d);

} catch (e) {}
});
}

return diagnostics;
};

const WEBPACK_FILE_REGEX = /\(webpack:\/\/\/(.*?)\)/;
9 changes: 9 additions & 0 deletions src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,12 @@ export function changeExtension(filePath: string, newExtension: string) {
const newFileName = extensionlessfileName + newExtension;
return join(dir, newFileName);
}

export function escapeHtml(unsafe: string) {
return unsafe
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}

0 comments on commit 10cf07e

Please sign in to comment.