| 1 |
# istanbul-lib-processinfo |
| 2 |
|
| 3 |
A utility for managing the `processinfo` folder that NYC uses. |
| 4 |
|
| 5 |
This is intended to be used along with [NYC](https://npm.im/nyc), but can also |
| 6 |
be used by other tools that wish to consume NYC's processinfo data. |
| 7 |
|
| 8 |
## API |
| 9 |
|
| 10 |
### class ProcessInfo |
| 11 |
|
| 12 |
A representation of information about a single process. |
| 13 |
|
| 14 |
#### constructor(fields) |
| 15 |
|
| 16 |
Pass in fields that will be printed to the processinfo file. Several defaults |
| 17 |
will be provided if not specified. |
| 18 |
|
| 19 |
#### async processInfo.save() |
| 20 |
|
| 21 |
Write this process info to disk. This works by passing the ProcessInfo object |
| 22 |
to JSON.stringify, and writing to `${this.directory}/${this.uuid}.json`. |
| 23 |
|
| 24 |
#### processInfo.saveSync() |
| 25 |
|
| 26 |
The synchronous version of `.save()`. |
| 27 |
|
| 28 |
#### async processInfo.getCoverageMap(nyc) |
| 29 |
|
| 30 |
Get a merged coverage map of the current process, as well as any child |
| 31 |
processes. This should only be called during tree rendering, as it depends on |
| 32 |
child nodes being present in the `nodes` array. |
| 33 |
|
| 34 |
The `nyc` instance is required to load the report information and apply |
| 35 |
sourcemaps properly. |
| 36 |
|
| 37 |
### processInfo.label |
| 38 |
|
| 39 |
A read-only string for when archy prints the process tree. |
| 40 |
|
| 41 |
### processInfo.nodes |
| 42 |
|
| 43 |
A list of the child nodes used during tree rendering. |
| 44 |
|
| 45 |
### processInfo.directory |
| 46 |
|
| 47 |
If a process will be saved, it must have a `directory` included |
| 48 |
in the list of fields. This property is not saved to the processinfo file. |
| 49 |
|
| 50 |
## class ProcessDB |
| 51 |
|
| 52 |
A utility for interacting with the collection of ProcessInfo files in the |
| 53 |
processinfo folder. |
| 54 |
|
| 55 |
### constructor(directory) |
| 56 |
|
| 57 |
Supply the directory where processinfo files are found. This should be the |
| 58 |
full path, something like `${cwd}/.nyc_output/processinfo`. |
| 59 |
|
| 60 |
### processDB.directory |
| 61 |
|
| 62 |
A read-only property showing the directory where this object is working. |
| 63 |
|
| 64 |
### processDB.nodes |
| 65 |
|
| 66 |
A list of child ProcessInfo nodes used in tree printing. |
| 67 |
|
| 68 |
### processDB.label |
| 69 |
|
| 70 |
The string `'nyc'`, used as the default root node in the archy tree rendering. |
| 71 |
|
| 72 |
### async processDB.writeIndex() |
| 73 |
|
| 74 |
Create the `index.json` file in the processinfo folder, which is required for |
| 75 |
tree generation and expunging. |
| 76 |
|
| 77 |
WARNING: Index writing is non-atomic, and should not be performed by multiple |
| 78 |
processes. |
| 79 |
|
| 80 |
### async processDB.readIndex() |
| 81 |
|
| 82 |
Read and return the contents of the `index.json` file. If the `index.json` is |
| 83 |
not present or not valid, then it will attempt to generate one. |
| 84 |
|
| 85 |
### async processDB.readProcessInfos() |
| 86 |
|
| 87 |
Read all the data files in the processinfo folder, and return an object mapping |
| 88 |
the file basename to the resulting object. Used in tree generation. |
| 89 |
|
| 90 |
### async processDB.renderTree(nyc) |
| 91 |
|
| 92 |
Render the tree as a string using archy, suitable for printing to the terminal. |
| 93 |
|
| 94 |
### async processDB.buildProcessTree() |
| 95 |
|
| 96 |
Build the hierarchical tree of nodes for tree rendering. Populates the `nodes` |
| 97 |
array of this object and all `ProcessInfo` objects in the tree. |
| 98 |
|
| 99 |
### async processDB.getCoverageMap(nyc) |
| 100 |
|
| 101 |
Used in tree rendering, to show the total coverage of all the processinfo files |
| 102 |
in the data folder. |
| 103 |
|
| 104 |
### async processDB.spawn(name, file, args, options) |
| 105 |
|
| 106 |
Spawn a child process with a unique name provided by the caller. This name is |
| 107 |
stored as the `externalId` property in the child process's `ProcessInfo` data, |
| 108 |
and is tracked in the `externalIds` section of the index. |
| 109 |
|
| 110 |
Note that if the current process is not already wrapped by nyc, then you must |
| 111 |
prefix the spawned program with nyc, in order for this to take effect. For |
| 112 |
example, instead of `processDB.spawn('foo', 'node', ['foo.js'])`, you would run |
| 113 |
`processDB.spawn('foo', 'nyc', ['node', 'foo.js'])`. |
| 114 |
|
| 115 |
If a process with that name already exists in the index, then it will be |
| 116 |
expunged. |
| 117 |
|
| 118 |
Unlike `child_process.spawn` this function returns a Promise which resolves to |
| 119 |
the `ChildProcess` object. |
| 120 |
|
| 121 |
WARNING: Calling `expunge` (which this method does) will result in the index |
| 122 |
being out of date. It is the caller's responsibility to call |
| 123 |
`processDB.writeIndex()` when all named processes are completed. |
| 124 |
|
| 125 |
### async processDB.expunge(name) |
| 126 |
|
| 127 |
If a process exists in the process info data folder with the specified name |
| 128 |
(ie, it had previously been run with `processDB.spawn(name, ...)`) then the |
| 129 |
coverage and processinfo files for it and all of its children are removed. |
| 130 |
|
| 131 |
This allows for a test harness to re-run or resume test suites, without |
| 132 |
spurious coverage results. |
| 133 |
|
| 134 |
WARNING: Calling `expunge` will result in the index being out of date. It is |
| 135 |
the caller's responsibility to call `processDB.writeIndex()` when all named |
| 136 |
processes are completed. |
| 137 |
|
| 138 |
## DATA STRUCTURES and FILES |
| 139 |
|
| 140 |
ProcessInfo files MUST match the following structure: |
| 141 |
|
| 142 |
``` |
| 143 |
{ |
| 144 |
"uuid": "UUID of the process itself", |
| 145 |
"parent": "UUID of the parent process, or null", |
| 146 |
"pid": Number, |
| 147 |
"ppid": Number (pid of parent process), |
| 148 |
"argv": Array<String>, |
| 149 |
"execArgv": Array<String>, |
| 150 |
"cwd": path, |
| 151 |
"time": Number (timestamp in ms), |
| 152 |
"coverageFilename": "Path to NYC coverage info for this process", |
| 153 |
"externalId": "The externally specified name for this process, or null", |
| 154 |
} |
| 155 |
``` |
| 156 |
|
| 157 |
The index file is saved to `${this.directory}/index.json`. It has |
| 158 |
the following structure: |
| 159 |
|
| 160 |
``` |
| 161 |
{ |
| 162 |
"processes": { |
| 163 |
"<uuid>": { |
| 164 |
"parent": "parent uuid, or null", |
| 165 |
"children": ["children", "uuids", "or empty array"], |
| 166 |
"externalId": "externally specified name, if provided" |
| 167 |
}, |
| 168 |
... |
| 169 |
}, |
| 170 |
"files": { |
| 171 |
"/path/to/covered/file.js": [ |
| 172 |
"<uuids of processes that covered this file>", |
| 173 |
... |
| 174 |
], |
| 175 |
... |
| 176 |
}, |
| 177 |
"externalIds": { |
| 178 |
"externally specified name": { |
| 179 |
"root": "<uuid of process run under this name>", |
| 180 |
"children": [ |
| 181 |
"<uuids of all descendant processes from this point in the tree>", |
| 182 |
... |
| 183 |
] |
| 184 |
}, |
| 185 |
... |
| 186 |
} |
| 187 |
} |
| 188 |
``` |