process.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const archy = require('archy')
  2. const libCoverage = require('istanbul-lib-coverage')
  3. function ProcessInfo (defaults) {
  4. defaults = defaults || {}
  5. this.pid = String(process.pid)
  6. this.argv = process.argv
  7. this.execArgv = process.execArgv
  8. this.cwd = process.cwd()
  9. this.time = Date.now()
  10. this.ppid = null
  11. this.root = null
  12. this.coverageFilename = null
  13. this.nodes = [] // list of children, filled by buildProcessTree()
  14. this._coverageMap = null
  15. for (var key in defaults) {
  16. this[key] = defaults[key]
  17. }
  18. }
  19. Object.defineProperty(ProcessInfo.prototype, 'label', {
  20. get: function () {
  21. if (this._label) {
  22. return this._label
  23. }
  24. var covInfo = ''
  25. if (this._coverageMap) {
  26. covInfo = '\n ' + this._coverageMap.getCoverageSummary().lines.pct + ' % Lines'
  27. }
  28. return this.argv.join(' ') + covInfo
  29. }
  30. })
  31. ProcessInfo.buildProcessTree = function (infos) {
  32. var treeRoot = new ProcessInfo({ _label: 'nyc' })
  33. var nodes = { }
  34. infos = infos.sort(function (a, b) {
  35. return a.time - b.time
  36. })
  37. infos.forEach(function (p) {
  38. nodes[p.root + ':' + p.pid] = p
  39. })
  40. infos.forEach(function (p) {
  41. if (!p.ppid) {
  42. return
  43. }
  44. var parent = nodes[p.root + ':' + p.ppid]
  45. if (!parent) {
  46. parent = treeRoot
  47. }
  48. parent.nodes.push(p)
  49. })
  50. return treeRoot
  51. }
  52. ProcessInfo.prototype.getCoverageMap = function (merger) {
  53. if (this._coverageMap) {
  54. return this._coverageMap
  55. }
  56. var childMaps = this.nodes.map(function (child) {
  57. return child.getCoverageMap(merger)
  58. })
  59. this._coverageMap = merger([this.coverageFilename], childMaps)
  60. return this._coverageMap
  61. }
  62. ProcessInfo.prototype.render = function (nyc) {
  63. this.getCoverageMap(function (filenames, maps) {
  64. var map = libCoverage.createCoverageMap({})
  65. nyc.eachReport(filenames, function (report) {
  66. map.merge(report)
  67. })
  68. maps.forEach(function (otherMap) {
  69. map.merge(otherMap)
  70. })
  71. return map
  72. })
  73. return archy(this)
  74. }
  75. module.exports = ProcessInfo