@@ -10,6 +10,7 @@ const glob = require('glob')
10
10
const Hash = require ( './lib/hash' )
11
11
const libCoverage = require ( 'istanbul-lib-coverage' )
12
12
const libHook = require ( 'istanbul-lib-hook' )
13
+ const { ProcessInfo, ProcessDB } = require ( 'istanbul-lib-processinfo' )
13
14
const libReport = require ( 'istanbul-lib-report' )
14
15
const mkdirp = require ( 'make-dir' )
15
16
const Module = require ( 'module' )
@@ -24,8 +25,6 @@ const util = require('util')
24
25
25
26
const debugLog = util . debuglog ( 'nyc' )
26
27
27
- const ProcessInfo = require ( './lib/process.js' )
28
-
29
28
/* istanbul ignore next */
30
29
if ( / s e l f - c o v e r a g e / . test ( __dirname ) ) {
31
30
require ( '../self-coverage-helper' )
@@ -87,7 +86,9 @@ class NYC {
87
86
this . hookRunInThisContext = config . hookRunInThisContext
88
87
this . fakeRequire = null
89
88
90
- this . processInfo = new ProcessInfo ( config && config . _processInfo )
89
+ this . processInfo = new ProcessInfo ( Object . assign ( { } , config . _processInfo , {
90
+ directory : path . resolve ( this . tempDirectory ( ) , 'processinfo' )
91
+ } ) )
91
92
92
93
this . hashCache = { }
93
94
}
@@ -308,7 +309,7 @@ class NYC {
308
309
mkdirp . sync ( this . tempDirectory ( ) )
309
310
if ( this . cache ) mkdirp . sync ( this . cacheDirectory )
310
311
311
- mkdirp . sync ( this . processInfoDirectory ( ) )
312
+ mkdirp . sync ( this . processInfo . directory )
312
313
}
313
314
314
315
reset ( ) {
@@ -364,12 +365,7 @@ class NYC {
364
365
365
366
this . processInfo . coverageFilename = coverageFilename
366
367
this . processInfo . files = Object . keys ( coverage )
367
-
368
- fs . writeFileSync (
369
- path . resolve ( this . processInfoDirectory ( ) , id + '.json' ) ,
370
- JSON . stringify ( this . processInfo ) ,
371
- 'utf-8'
372
- )
368
+ this . processInfo . save ( )
373
369
}
374
370
375
371
getCoverageMapFromAllCoverageFiles ( baseDirectory ) {
@@ -412,84 +408,14 @@ class NYC {
412
408
}
413
409
}
414
410
415
- // XXX(@isaacs) Index generation should move to istanbul-lib-processinfo
416
411
writeProcessIndex ( ) {
417
- const dir = this . processInfoDirectory ( )
418
- const pidToUid = new Map ( )
419
- const infoByUid = new Map ( )
420
- const eidToUid = new Map ( )
421
- const infos = fs . readdirSync ( dir ) . filter ( f => f !== 'index.json' ) . map ( f => {
422
- try {
423
- const info = JSON . parse ( fs . readFileSync ( path . resolve ( dir , f ) , 'utf-8' ) )
424
- info . children = [ ]
425
- pidToUid . set ( info . uuid , info . pid )
426
- pidToUid . set ( info . pid , info . uuid )
427
- infoByUid . set ( info . uuid , info )
428
- if ( info . externalId ) {
429
- eidToUid . set ( info . externalId , info . uuid )
430
- }
431
- return info
432
- } catch ( er ) {
433
- return null
434
- }
435
- } ) . filter ( Boolean )
436
-
437
- // create all the parent-child links and write back the updated info
438
- infos . forEach ( info => {
439
- if ( info . parent ) {
440
- const parentInfo = infoByUid . get ( info . parent )
441
- if ( parentInfo && ! parentInfo . children . includes ( info . uuid ) ) {
442
- parentInfo . children . push ( info . uuid )
443
- }
444
- }
445
- } )
446
-
447
- // figure out which files were touched by each process.
448
- const files = infos . reduce ( ( files , info ) => {
449
- info . files . forEach ( f => {
450
- files [ f ] = files [ f ] || [ ]
451
- files [ f ] . push ( info . uuid )
452
- } )
453
- return files
454
- } , { } )
455
-
456
- // build the actual index!
457
- const index = infos . reduce ( ( index , info ) => {
458
- index . processes [ info . uuid ] = { }
459
- index . processes [ info . uuid ] . parent = info . parent
460
- if ( info . externalId ) {
461
- if ( index . externalIds [ info . externalId ] ) {
462
- throw new Error ( `External ID ${ info . externalId } used by multiple processes` )
463
- }
464
- index . processes [ info . uuid ] . externalId = info . externalId
465
- index . externalIds [ info . externalId ] = {
466
- root : info . uuid ,
467
- children : info . children
468
- }
469
- }
470
- index . processes [ info . uuid ] . children = Array . from ( info . children )
471
- return index
472
- } , { processes : { } , files : files , externalIds : { } } )
473
-
474
- // flatten the descendant sets of all the externalId procs
475
- Object . keys ( index . externalIds ) . forEach ( eid => {
476
- const { children } = index . externalIds [ eid ]
477
- // push the next generation onto the list so we accumulate them all
478
- for ( let i = 0 ; i < children . length ; i ++ ) {
479
- const nextGen = index . processes [ children [ i ] ] . children
480
- if ( nextGen && nextGen . length ) {
481
- children . push ( ...nextGen . filter ( uuid => children . indexOf ( uuid ) === - 1 ) )
482
- }
483
- }
484
- } )
485
-
486
- fs . writeFileSync ( path . resolve ( dir , 'index.json' ) , JSON . stringify ( index ) )
412
+ const db = new ProcessDB ( this . processInfo . directory )
413
+ db . writeIndex ( )
487
414
}
488
415
489
416
showProcessTree ( ) {
490
- var processTree = ProcessInfo . buildProcessTree ( this . _loadProcessInfos ( ) )
491
-
492
- console . log ( processTree . render ( this ) )
417
+ const db = new ProcessDB ( this . processInfo . directory )
418
+ console . log ( db . renderTree ( this ) )
493
419
}
494
420
495
421
checkCoverage ( thresholds , perFile ) {
@@ -521,28 +447,6 @@ class NYC {
521
447
} )
522
448
}
523
449
524
- _loadProcessInfos ( ) {
525
- return fs . readdirSync ( this . processInfoDirectory ( ) ) . map ( f => {
526
- let data
527
- try {
528
- data = JSON . parse ( fs . readFileSync (
529
- path . resolve ( this . processInfoDirectory ( ) , f ) ,
530
- 'utf-8'
531
- ) )
532
- } catch ( e ) { // handle corrupt JSON output.
533
- return null
534
- }
535
- if ( f !== 'index.json' ) {
536
- data . nodes = [ ]
537
- data = new ProcessInfo ( data )
538
- }
539
- return { file : path . basename ( f , '.json' ) , data : data }
540
- } ) . filter ( Boolean ) . reduce ( ( infos , info ) => {
541
- infos [ info . file ] = info . data
542
- return infos
543
- } , { } )
544
- }
545
-
546
450
eachReport ( filenames , iterator , baseDirectory ) {
547
451
baseDirectory = baseDirectory || this . tempDirectory ( )
548
452
@@ -588,10 +492,6 @@ class NYC {
588
492
reportDirectory ( ) {
589
493
return path . resolve ( this . cwd , this . _reportDir )
590
494
}
591
-
592
- processInfoDirectory ( ) {
593
- return path . resolve ( this . tempDirectory ( ) , 'processinfo' )
594
- }
595
495
}
596
496
597
497
module . exports = NYC
0 commit comments