@@ -22,6 +22,13 @@ var pkgUp = require('pkg-up')
2222var testExclude = require ( 'test-exclude' )
2323var yargs = require ( 'yargs' )
2424
25+ var ProcessInfo
26+ try {
27+ ProcessInfo = require ( './lib/process.covered.js' )
28+ } catch ( e ) {
29+ ProcessInfo = require ( './lib/process.js' )
30+ }
31+
2532/* istanbul ignore next */
2633if ( / i n d e x \. c o v e r e d \. j s $ / . test ( __filename ) ) {
2734 require ( './lib/self-coverage-helper' )
@@ -36,6 +43,7 @@ function NYC (opts) {
3643 this . _instrumenterLib = require ( config . instrumenter || './lib/instrumenters/istanbul' )
3744 this . _reportDir = config . reportDir
3845 this . _sourceMap = config . sourceMap
46+ this . _showProcessTree = config . showProcessTree
3947 this . cwd = config . cwd
4048
4149 this . reporter = arrify ( config . reporter || 'text' )
@@ -71,6 +79,9 @@ function NYC (opts) {
7179 this . hashCache = { }
7280 this . loadedMaps = null
7381 this . fakeRequire = null
82+
83+ this . processInfo = new ProcessInfo ( opts && opts . _processInfo )
84+ this . rootId = this . processInfo . root || this . generateUniqueID ( )
7485}
7586
7687NYC . prototype . _loadConfig = function ( opts ) {
@@ -327,6 +338,10 @@ NYC.prototype.clearCache = function () {
327338
328339NYC . prototype . createTempDirectory = function ( ) {
329340 mkdirp . sync ( this . tempDirectory ( ) )
341+
342+ if ( this . _showProcessTree ) {
343+ mkdirp . sync ( this . processInfoDirectory ( ) )
344+ }
330345}
331346
332347NYC . prototype . reset = function ( ) {
@@ -352,6 +367,12 @@ NYC.prototype.wrap = function (bin) {
352367 return this
353368}
354369
370+ NYC . prototype . generateUniqueID = function ( ) {
371+ return md5hex (
372+ process . hrtime ( ) . concat ( process . pid ) . map ( String )
373+ )
374+ }
375+
355376NYC . prototype . writeCoverageFile = function ( ) {
356377 var coverage = coverageFinder ( )
357378 if ( ! coverage ) return
@@ -366,15 +387,26 @@ NYC.prototype.writeCoverageFile = function () {
366387 coverage = this . sourceMapTransform ( coverage )
367388 }
368389
369- var id = md5hex (
370- process . hrtime ( ) . concat ( process . pid ) . map ( String )
371- )
390+ var id = this . generateUniqueID ( )
391+ var coverageFilename = path . resolve ( this . tempDirectory ( ) , id + '.json' )
372392
373393 fs . writeFileSync (
374- path . resolve ( this . tempDirectory ( ) , './' , id + '.json' ) ,
394+ coverageFilename ,
375395 JSON . stringify ( coverage ) ,
376396 'utf-8'
377397 )
398+
399+ if ( ! this . _showProcessTree ) {
400+ return
401+ }
402+
403+ this . processInfo . coverageFilename = coverageFilename
404+
405+ fs . writeFileSync (
406+ path . resolve ( this . processInfoDirectory ( ) , id + '.json' ) ,
407+ JSON . stringify ( this . processInfo ) ,
408+ 'utf-8'
409+ )
378410}
379411
380412NYC . prototype . sourceMapTransform = function ( obj ) {
@@ -413,6 +445,14 @@ NYC.prototype.report = function () {
413445 this . reporter . forEach ( function ( _reporter ) {
414446 tree . visit ( reports . create ( _reporter ) , context )
415447 } )
448+
449+ if ( this . _showProcessTree ) {
450+ this . showProcessTree ( )
451+ }
452+ }
453+
454+ NYC . prototype . showProcessTree = function ( ) {
455+ console . log ( this . _loadProcessInfoTree ( ) . render ( ) )
416456}
417457
418458NYC . prototype . checkCoverage = function ( thresholds ) {
@@ -429,6 +469,26 @@ NYC.prototype.checkCoverage = function (thresholds) {
429469 } )
430470}
431471
472+ NYC . prototype . _loadProcessInfoTree = function ( ) {
473+ return ProcessInfo . buildProcessTree ( this . _loadProcessInfos ( ) )
474+ }
475+
476+ NYC . prototype . _loadProcessInfos = function ( ) {
477+ var _this = this
478+ var files = fs . readdirSync ( this . processInfoDirectory ( ) )
479+
480+ return files . map ( function ( f ) {
481+ try {
482+ return new ProcessInfo ( JSON . parse ( fs . readFileSync (
483+ path . resolve ( _this . processInfoDirectory ( ) , f ) ,
484+ 'utf-8'
485+ ) ) )
486+ } catch ( e ) { // handle corrupt JSON output.
487+ return { }
488+ }
489+ } )
490+ }
491+
432492NYC . prototype . _loadReports = function ( ) {
433493 var _this = this
434494 var files = fs . readdirSync ( this . tempDirectory ( ) )
@@ -441,7 +501,7 @@ NYC.prototype._loadReports = function () {
441501 var report
442502 try {
443503 report = JSON . parse ( fs . readFileSync (
444- path . resolve ( _this . tempDirectory ( ) , './' , f ) ,
504+ path . resolve ( _this . tempDirectory ( ) , f ) ,
445505 'utf-8'
446506 ) )
447507 } catch ( e ) { // handle corrupt JSON output.
@@ -472,7 +532,11 @@ NYC.prototype._loadReports = function () {
472532}
473533
474534NYC . prototype . tempDirectory = function ( ) {
475- return path . resolve ( this . cwd , './' , this . _tempDirectory )
535+ return path . resolve ( this . cwd , this . _tempDirectory )
536+ }
537+
538+ NYC . prototype . processInfoDirectory = function ( ) {
539+ return path . resolve ( this . tempDirectory ( ) , 'processinfo' )
476540}
477541
478542module . exports = NYC
0 commit comments