1
1
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' ;
2
5
import { Logger } from './logger' ;
3
- import { join , resolve , normalize } from 'path' ;
4
- import { highlight , highlightError } from '../highlight/highlight' ;
5
6
import { readFileSync , unlinkSync , writeFileSync } from 'fs' ;
6
- import { splitLineBreaks , titleCase } from '../util/helpers' ;
7
7
import * as chalk from 'chalk' ;
8
8
9
9
@@ -205,7 +205,7 @@ export function getDiagnosticsHtmlContent(buildDir: string, includeDiagnosticsHt
205
205
}
206
206
207
207
208
- function generateDiagnosticHtml ( d : Diagnostic ) {
208
+ export function generateDiagnosticHtml ( d : Diagnostic ) {
209
209
const c : string [ ] = [ ] ;
210
210
211
211
c . push ( `<div class="ion-diagnostic">` ) ;
@@ -227,7 +227,7 @@ function generateDiagnosticHtml(d: Diagnostic) {
227
227
}
228
228
229
229
230
- function generateCodeBlock ( d : Diagnostic ) {
230
+ export function generateCodeBlock ( d : Diagnostic ) {
231
231
const c : string [ ] = [ ] ;
232
232
233
233
c . push ( `<div class="ion-diagnostic-file">` ) ;
@@ -260,147 +260,6 @@ function generateCodeBlock(d: Diagnostic) {
260
260
}
261
261
262
262
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 ( / i n l i n e t e m p l a t e : \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 = / \( w e b p a c k : \/ \/ \/ ( .* ?) \) / ;
402
-
403
-
404
263
function jsConsoleSyntaxHighlight ( text : string ) {
405
264
if ( text . trim ( ) . startsWith ( '//' ) ) {
406
265
return chalk . dim ( text ) ;
@@ -444,16 +303,6 @@ function cssConsoleSyntaxHighlight(text: string, errorCharStart: number) {
444
303
}
445
304
446
305
447
- function escapeHtml ( unsafe : string ) {
448
- return unsafe
449
- . replace ( / & / g, '&' )
450
- . replace ( / < / g, '<' )
451
- . replace ( / > / g, '>' )
452
- . replace ( / " / g, '"' )
453
- . replace ( / ' / g, ''' ) ;
454
- }
455
-
456
-
457
306
function prepareLines ( orgLines : PrintLine [ ] , code : 'text' | 'html' ) {
458
307
const lines : PrintLine [ ] = JSON . parse ( JSON . stringify ( orgLines ) ) ;
459
308
0 commit comments