-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
347 lines (292 loc) · 8.63 KB
/
index.js
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
'use strict'
const { v4: uuidv4 } = require('uuid');
const archy = require('archy')
const libCoverage = require('istanbul-lib-coverage')
const {dirname, resolve} = require('path')
const {promisify} = require('util')
/* Shallow clone so we can promisify in-place */
const fs = { ...require('fs') }
const {spawn} = require('cross-spawn')
const rimraf = promisify(require('rimraf'))
const pMap = require('p-map')
const _nodes = Symbol('nodes')
const _label = Symbol('label')
const _coverageMap = Symbol('coverageMap')
const _processInfoDirectory = Symbol('processInfo.directory')
// shared symbol for testing
const _spawnArgs = Symbol.for('spawnArgs')
;['writeFile', 'readFile', 'readdir'].forEach(fn => {
fs[fn] = promisify(fs[fn])
})
// 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,
coverageFilename: null,
externalId: '',
[_nodes]: [],
[_label]: null,
[_coverageMap]: null
})
/* istanbul ignore next */
const fromEntries = Object.fromEntries || (
entries => entries.reduce((obj, [name, value]) => {
obj[name] = value
return obj
}, {})
)
class ProcessInfo {
constructor (fields = {}) {
Object.assign(this, defaults(), fields)
if (!this.uuid) {
this.uuid = uuidv4()
}
}
get nodes () {
return this[_nodes]
}
set nodes (n) {
this[_nodes] = n
}
set directory (d) {
this[_processInfoDirectory] = resolve(d)
}
get directory () {
return this[_processInfoDirectory]
}
saveSync () {
const f = resolve(this.directory, this.uuid + '.json')
fs.writeFileSync(f, JSON.stringify(this), 'utf-8')
}
async save () {
const f = resolve(this.directory, this.uuid + '.json')
await fs.writeFile(f, JSON.stringify(this), 'utf-8')
}
async getCoverageMap (nyc) {
if (this[_coverageMap]) {
return this[_coverageMap]
}
const childMaps = await Promise.all(this.nodes.map(child => child.getCoverageMap(nyc)))
this[_coverageMap] = await 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 = async (nyc, filename, maps) => {
const map = libCoverage.createCoverageMap({})
if (filename) {
map.merge(await nyc.coverageFileLoad(filename))
}
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 (directory) {
if (!directory) {
const nycConfig = process.env.NYC_CONFIG;
if (nycConfig) {
directory = resolve(JSON.parse(nycConfig).tempDir, 'processinfo')
}
if (!directory) {
throw new TypeError('must provide directory argument when outside of NYC')
}
}
Object.defineProperty(this, 'directory', { get: () => directory, 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
}
async getCoverageMap (nyc) {
if (this[_coverageMap]) {
return this[_coverageMap]
}
const childMaps = await Promise.all(this.nodes.map(child => child.getCoverageMap(nyc)))
this[_coverageMap] = await mapMerger(nyc, undefined, childMaps)
return this[_coverageMap]
}
async renderTree (nyc) {
await this.buildProcessTree()
await this.getCoverageMap(nyc)
return archy(this)
}
async buildProcessTree () {
const infos = await this.readProcessInfos(this.directory)
const index = await 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)
}
}
}
async _readJSON (file) {
if (Array.isArray(file)) {
const result = await pMap(
file,
f => this._readJSON(f),
{ concurrency: 8 }
)
return result.filter(Boolean)
}
try {
return JSON.parse(await fs.readFile(resolve(this.directory, file), 'utf-8'))
} catch (error) {
}
}
async readProcessInfos () {
const files = await fs.readdir(this.directory)
const fileData = await this._readJSON(files.filter(f => f !== 'index.json'))
return fromEntries(fileData.map(info => [
info.uuid,
new ProcessInfo(info)
]))
}
_createIndex (infos) {
const infoMap = fromEntries(infos.map(info => [
info.uuid,
Object.assign(info, {children: []})
]))
// create all the parent-child links
infos.forEach(info => {
if (info.parent) {
const parentInfo = infoMap[info.parent]
if (parentInfo && !parentInfo.children.includes(info.uuid)) {
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].includes(info.uuid)) {
files[f].push(info.uuid)
}
})
return files
}, {})
const processes = fromEntries(infos.map(info => [
info.uuid,
{
parent: info.parent,
...(info.externalId ? { externalId: info.externalId } : {}),
children: Array.from(info.children)
}
]))
const eidList = new Set()
const externalIds = fromEntries(infos.filter(info => info.externalId).map(info => {
if (eidList.has(info.externalId)) {
throw new Error(
`External ID ${info.externalId} used by multiple processes`)
}
eidList.add(info.externalId)
const children = Array.from(info.children)
// flatten the descendant sets of all the externalId procs
// push the next generation onto the list so we accumulate them all
for (let i = 0; i < children.length; i++) {
children.push(...processes[children[i]].children.filter(uuid => !children.includes(uuid)))
}
return [
info.externalId,
{
root: info.uuid,
children
}
]
}))
return { processes, files, externalIds }
}
async writeIndex () {
const {directory} = this
const files = await fs.readdir(directory)
const infos = await this._readJSON(files.filter(f => f !== 'index.json'))
const index = this._createIndex(infos)
const indexFile = resolve(directory, 'index.json')
await fs.writeFile(indexFile, JSON.stringify(index))
return index
}
async readIndex () {
return await this._readJSON('index.json') || await 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.
async expunge (id) {
const index = await this.readIndex()
const entry = index.externalIds[id]
if (!entry) {
return
}
await pMap(
[].concat(
`${dirname(this.directory)}/${entry.root}.json`,
`${this.directory}/${entry.root}.json`,
...entry.children.map(c => [
`${dirname(this.directory)}/${c}.json`,
`${this.directory}/${c}.json`
])
),
f => rimraf(f),
{ concurrency: 8 }
)
}
[_spawnArgs] (name, file, args, options) {
if (!Array.isArray(args)) {
options = args
args = []
}
if (!options) {
options = {}
}
if (!process.env.NYC_CONFIG) {
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
async spawn (...spawnArgs) {
const [name, file, args, options] = this[_spawnArgs](...spawnArgs)
await this.expunge(name)
return spawn(file, args, options)
}
}
exports.ProcessDB = ProcessDB
exports.ProcessInfo = ProcessInfo