d46631135fd45376055408ea9713a7b89a6b7dca.svn-base
9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
'use strict'
const uuid = require('uuid/v4')
const archy = require('archy')
const libCoverage = require('istanbul-lib-coverage')
const {basename, dirname, resolve} = require('path')
const fs = require('fs')
const {spawn, sync: spawnSync} = require('cross-spawn')
const rimraf = require('rimraf').sync
const mkdirp = require('mkdirp').sync
const _nodes = Symbol('nodes')
const _label = Symbol('label')
const _coverageMap = Symbol('coverageMap')
const _processInfoDirectory = Symbol('processInfoDirectory')
// shared symbol for testing
const _spawnArgs = Symbol.for('spawnArgs')
const nycConfig = process.env.NYC_CONFIG
// the enumerable fields
const defaults = {
parent: null,
pid: process.pid,
argv: process.argv,
execArgv: process.execArgv,
cwd: process.cwd(),
time: Date.now(),
ppid: process.ppid,
root: null,
coverageFilename: null,
externalId: process.env.NYC_PROCESSINFO_EXTERNAL_ID || '',
}
delete process.env.NYC_PROCESSINFO_EXTERNAL_ID
class ProcessInfo {
constructor (fields) {
fields = fields || {}
for (const key in fields) {
this[key] = fields[key]
}
for (const key in defaults) {
if (!(key in fields)) {
this[key] = defaults[key]
}
}
if (!this.uuid) {
this.uuid = uuid()
}
if (!this[_nodes]) {
this[_nodes] = []
}
if (!this[_processInfoDirectory]) {
this[_processInfoDirectory] = nycConfig
? resolve(JSON.parse(nycConfig).tempDir, 'processinfo')
: resolve(this.cwd, '.nyc_output', 'processinfo')
}
this[_label] = null
this[_coverageMap] = null
}
get nodes () {
return this[_nodes]
}
set nodes (n) {
this[_nodes] = n
}
set processInfoDirectory (d) {
this[_processInfoDirectory] = d
}
get processInfoDirectory () {
return this[_processInfoDirectory]
}
save () {
const f = this.processInfoDirectory + '/' + this.uuid + '.json'
fs.writeFileSync(f, JSON.stringify(this), 'utf-8')
}
getCoverageMap (nyc) {
if (this[_coverageMap]) {
return this[_coverageMap]
}
const childMaps = this.nodes.map(child => child.getCoverageMap(nyc))
this[_coverageMap] = mapMerger(nyc, [this.coverageFilename], childMaps)
return this[_coverageMap]
}
get label () {
if (this[_label]) {
return this[_label]
}
const covInfo = this[_coverageMap]
? '\n ' + this[_coverageMap].getCoverageSummary().lines.pct + ' % Lines'
: ''
return this[_label] = this.argv.join(' ') + covInfo
}
}
const mapMerger = (nyc, filenames, maps) => {
const map = libCoverage.createCoverageMap({})
nyc.eachReport(filenames, report => map.merge(report))
maps.forEach(otherMap => map.merge(otherMap))
return map
}
// Operations on the processinfo database as a whole,
// and the root of the tree rendering operation.
class ProcessDB {
constructor (dir) {
if (!dir && nycConfig) {
dir = resolve(JSON.parse(nycConfig).tempDir, 'processinfo')
}
if (!dir) {
throw new TypeError('must provide dir argument when outside of NYC')
}
mkdirp(dir)
Object.defineProperty(this, 'dir', { get: () => dir, enumerable: true })
this.nodes = []
this[_label] = null
this[_coverageMap] = null
}
get label () {
if (this[_label]) {
return this[_label]
}
const covInfo = this[_coverageMap]
? '\n ' + this[_coverageMap].getCoverageSummary().lines.pct + ' % Lines'
: ''
return this[_label] = 'nyc' + covInfo
}
getCoverageMap (nyc) {
if (this[_coverageMap]) {
return this[_coverageMap]
}
const childMaps = this.nodes.map(child => child.getCoverageMap(nyc))
this[_coverageMap] = mapMerger(nyc, [], childMaps)
return this[_coverageMap]
}
renderTree (nyc) {
this.buildProcessTree()
this.getCoverageMap(nyc)
return archy(this)
}
buildProcessTree () {
const infos = this.readProcessInfos(this.dir)
const index = this.readIndex()
for (const id in index.processes) {
const node = infos[id]
if (!node) {
throw new Error(`Invalid entry in processinfo index: ${id}`)
}
const idx = index.processes[id]
node.nodes = idx.children.map(id => infos[id])
.sort((a, b) => a.time - b.time)
if (!node.parent) {
this.nodes.push(node)
}
}
}
readProcessInfos () {
return fs.readdirSync(this.dir).filter(f => f !== 'index.json').map(f => {
let data
try {
data = JSON.parse(fs.readFileSync(resolve(this.dir, f), 'utf-8'))
} catch (e) { // handle corrupt JSON output.
return null
}
data.nodes = []
data = new ProcessInfo(data)
const file = basename(f, '.json')
return { file, data }
}).filter(Boolean).reduce((infos, {file, data}) => {
infos[file] = data
return infos
}, {})
}
writeIndex () {
const dir = this.dir
const pidToUid = new Map()
const infoByUid = new Map()
const eidToUid = new Map()
const infos = fs.readdirSync(dir).filter(f => f !== 'index.json').map(f => {
try {
const info = JSON.parse(fs.readFileSync(resolve(dir, f), 'utf-8'))
info.children = []
pidToUid.set(info.uuid, info.pid)
pidToUid.set(info.pid, info.uuid)
infoByUid.set(info.uuid, info)
if (info.externalId) {
eidToUid.set(info.externalId, info.uuid)
}
return info
} catch (er) {
return null
}
}).filter(Boolean)
// create all the parent-child links
infos.forEach(info => {
if (info.parent) {
const parentInfo = infoByUid.get(info.parent)
if (parentInfo.children.indexOf(info.uuid) === -1) {
parentInfo.children.push(info.uuid)
}
}
})
// figure out which files were touched by each process.
const files = infos.reduce((files, info) => {
info.files.forEach(f => {
files[f] = files[f] || []
if (files[f].indexOf(info.uuid) === -1) {
files[f].push(info.uuid)
}
})
return files
}, {})
// build the actual index!
const index = infos.reduce((index, info) => {
index.processes[info.uuid] = {}
index.processes[info.uuid].parent = info.parent
if (info.externalId) {
if (index.externalIds[info.externalId]) {
throw new Error(
`External ID ${info.externalId} used by multiple processes`)
}
index.processes[info.uuid].externalId = info.externalId
index.externalIds[info.externalId] = {
root: info.uuid,
children: info.children
}
}
index.processes[info.uuid].children = Array.from(info.children)
return index
}, { processes: {}, files: files, externalIds: {} })
// flatten the descendant sets of all the externalId procs
Object.keys(index.externalIds).forEach(eid => {
const { children } = index.externalIds[eid]
// push the next generation onto the list so we accumulate them all
for (let i = 0; i < children.length; i++) {
const nextGen = index.processes[children[i]].children
if (nextGen && nextGen.length) {
children.push(...nextGen.filter(uuid => children.indexOf(uuid) === -1))
}
}
})
const indexFile = resolve(dir, 'index.json')
fs.writeFileSync(indexFile, JSON.stringify(index))
return index
}
readIndex () {
try {
return JSON.parse(fs.readFileSync(this.dir + '/index.json', 'utf-8'))
} catch (e) {
return this.writeIndex()
}
}
// delete all coverage and processinfo for a given process
// Warning! Doing this makes the index out of date, so make sure
// to update it when you're done!
// Not multi-process safe, because it cannot be done atomically.
expunge (id) {
const index = this.readIndex()
const entry = index.externalIds[id]
if (!entry) {
return
}
rimraf(`${dirname(this.dir)}/${entry.root}.json`)
rimraf(`${this.dir}/${entry.root}.json`)
entry.children.forEach(c => {
rimraf(`${dirname(this.dir)}/${c}.json`)
rimraf(`${this.dir}/${c}.json`)
})
}
[_spawnArgs] (name, file, args, options) {
if (!Array.isArray(args)) {
options = args
args = []
}
if (!options) {
options = {}
}
if (!nycConfig) {
const nyc = options.nyc || 'nyc'
const nycArgs = options.nycArgs || []
args = [...nycArgs, file, ...args]
file = nyc
}
options.env = {
...(options.env || process.env),
NYC_PROCESSINFO_EXTERNAL_ID: name,
}
return [name, file, args, options]
}
// spawn an externally named process
spawn (...spawnArgs) {
const [name, file, args, options] = this[_spawnArgs](...spawnArgs)
this.expunge(name)
const proc = spawn(file, args, options)
if (options.regenerateIndex) {
proc.on('close', () => this.writeIndex())
}
return proc
}
spawnSync (...spawnArgs) {
const [name, file, args, options] = this[_spawnArgs](...spawnArgs)
this.expunge(name)
const proc = spawnSync(file, args, options)
if (options.regenerateIndex) {
this.writeIndex()
}
return proc
}
}
exports.ProcessDB = ProcessDB
exports.ProcessInfo = ProcessInfo