Skip to content

Commit 7943413

Browse files
authored
feat: Use istanbul-lib-processinfo (#1145)
1 parent 00c3b34 commit 7943413

File tree

5 files changed

+58
-223
lines changed

5 files changed

+58
-223
lines changed

index.js

Lines changed: 10 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const glob = require('glob')
1010
const Hash = require('./lib/hash')
1111
const libCoverage = require('istanbul-lib-coverage')
1212
const libHook = require('istanbul-lib-hook')
13+
const { ProcessInfo, ProcessDB } = require('istanbul-lib-processinfo')
1314
const libReport = require('istanbul-lib-report')
1415
const mkdirp = require('make-dir')
1516
const Module = require('module')
@@ -24,8 +25,6 @@ const util = require('util')
2425

2526
const debugLog = util.debuglog('nyc')
2627

27-
const ProcessInfo = require('./lib/process.js')
28-
2928
/* istanbul ignore next */
3029
if (/self-coverage/.test(__dirname)) {
3130
require('../self-coverage-helper')
@@ -87,7 +86,9 @@ class NYC {
8786
this.hookRunInThisContext = config.hookRunInThisContext
8887
this.fakeRequire = null
8988

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+
}))
9192

9293
this.hashCache = {}
9394
}
@@ -308,7 +309,7 @@ class NYC {
308309
mkdirp.sync(this.tempDirectory())
309310
if (this.cache) mkdirp.sync(this.cacheDirectory)
310311

311-
mkdirp.sync(this.processInfoDirectory())
312+
mkdirp.sync(this.processInfo.directory)
312313
}
313314

314315
reset () {
@@ -364,12 +365,7 @@ class NYC {
364365

365366
this.processInfo.coverageFilename = coverageFilename
366367
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()
373369
}
374370

375371
getCoverageMapFromAllCoverageFiles (baseDirectory) {
@@ -412,84 +408,14 @@ class NYC {
412408
}
413409
}
414410

415-
// XXX(@isaacs) Index generation should move to istanbul-lib-processinfo
416411
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()
487414
}
488415

489416
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))
493419
}
494420

495421
checkCoverage (thresholds, perFile) {
@@ -521,28 +447,6 @@ class NYC {
521447
})
522448
}
523449

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-
546450
eachReport (filenames, iterator, baseDirectory) {
547451
baseDirectory = baseDirectory || this.tempDirectory()
548452

@@ -588,10 +492,6 @@ class NYC {
588492
reportDirectory () {
589493
return path.resolve(this.cwd, this._reportDir)
590494
}
591-
592-
processInfoDirectory () {
593-
return path.resolve(this.tempDirectory(), 'processinfo')
594-
}
595495
}
596496

597497
module.exports = NYC

lib/process.js

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)