monocart-coverage-reports 2.10.9 → 2.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/generate.js CHANGED
@@ -5,6 +5,7 @@ const { fileURLToPath } = require('url');
5
5
  const Util = require('./utils/util.js');
6
6
  const { convertV8List } = require('./converter/converter.js');
7
7
  const { resolveSourceMap } = require('./converter/collect-source-maps.js');
8
+ const { StreamZip } = require('./packages/monocart-coverage-vendor.js');
8
9
 
9
10
  // ========================================================================================================
10
11
  // built-in reports
@@ -229,7 +230,7 @@ const getInputList = (mcr) => {
229
230
  const hasDir = fs.existsSync(dir);
230
231
  if (!hasDir) {
231
232
  // could be empty
232
- Util.logInfo(`Not found coverage data dir: ${Util.relativePath(dir)}`);
233
+ Util.logInfo(`Input coverage not exists: ${Util.relativePath(dir)}`);
233
234
  }
234
235
  return hasDir;
235
236
  });
@@ -241,17 +242,16 @@ const getInputList = (mcr) => {
241
242
  return inputList;
242
243
  };
243
244
 
244
- const addJsonData = async (mcr, dataList, sourceCache, dir, filename) => {
245
+ const addJsonData = async (mcr, dataList, sourceCache, input, filename) => {
245
246
  const isCoverage = filename.startsWith('coverage-');
246
247
  const isSource = filename.startsWith('source-');
247
248
  if (isCoverage || isSource) {
248
- let json;
249
- if (mcr.fileCache.has(filename)) {
250
- json = mcr.fileCache.get(filename);
251
- } else {
252
- const content = await Util.readFile(path.resolve(dir, filename));
253
- if (content) {
254
- json = JSON.parse(content);
249
+ let json = input;
250
+ if (typeof input === 'string') {
251
+ if (mcr.fileCache.has(filename)) {
252
+ json = mcr.fileCache.get(filename);
253
+ } else {
254
+ json = await Util.readJson(path.resolve(input, filename));
255
255
  }
256
256
  }
257
257
  if (json) {
@@ -264,6 +264,41 @@ const addJsonData = async (mcr, dataList, sourceCache, dir, filename) => {
264
264
  }
265
265
  };
266
266
 
267
+ const addDirData = async (mcr, dataList, sourceCache, dir) => {
268
+ const allFiles = fs.readdirSync(dir);
269
+ if (!allFiles.length) {
270
+ return;
271
+ }
272
+ for (const filename of allFiles) {
273
+ // only json file
274
+ if (filename.endsWith('.json')) {
275
+ await addJsonData(mcr, dataList, sourceCache, dir, filename);
276
+ }
277
+ }
278
+ };
279
+
280
+ const addZipData = async (mcr, dataList, sourceCache, dir) => {
281
+ const zip = new StreamZip({
282
+ file: dir
283
+ });
284
+ const entries = await zip.entries();
285
+ for (const entry of Object.values(entries)) {
286
+ if (entry.isDirectory) {
287
+ continue;
288
+ }
289
+ const entryName = entry.name;
290
+ const filename = path.basename(entryName);
291
+ // console.log('============================', filename);
292
+ if (filename.endsWith('.json')) {
293
+ const buf = await zip.entryData(entryName);
294
+ const json = JSON.parse(buf.toString('utf-8'));
295
+ await addJsonData(mcr, dataList, sourceCache, json, filename);
296
+ }
297
+
298
+ }
299
+ // Do not forget to close the file once you're done
300
+ await zip.close();
301
+ };
267
302
 
268
303
  const getInputData = async (mcr) => {
269
304
 
@@ -275,16 +310,15 @@ const getInputData = async (mcr) => {
275
310
 
276
311
  for (const dir of inputList) {
277
312
 
278
- const allFiles = fs.readdirSync(dir);
279
- if (!allFiles.length) {
280
- continue;
281
- }
282
- for (const filename of allFiles) {
283
- // only json file
284
- if (filename.endsWith('.json')) {
285
- await addJsonData(mcr, dataList, sourceCache, dir, filename);
286
- }
313
+ const info = fs.statSync(dir);
314
+ if (info.isDirectory()) {
315
+ await addDirData(mcr, dataList, sourceCache, dir);
316
+ } else if (info.isFile()) {
317
+ await addZipData(mcr, dataList, sourceCache, dir);
318
+ } else {
319
+ Util.logError(`Invalid input: ${dir}`);
287
320
  }
321
+
288
322
  }
289
323
 
290
324
  if (!dataList.length) {
@@ -306,7 +340,7 @@ const resolveEntrySource = (entry, sourceMapCache, sourceCache) => {
306
340
  const url = entry.url;
307
341
 
308
342
  // source from `source-id.json`
309
- const sourceData = sourceCache[url];
343
+ const sourceData = sourceCache.get(url);
310
344
  if (sourceData) {
311
345
  entry.source = sourceData.source;
312
346
  return;
@@ -390,7 +424,7 @@ const readCoverageData = async (dir, filename, entryFilter, sourceCache) => {
390
424
  };
391
425
 
392
426
  const readSourceList = async (dir, sourceList) => {
393
- const sourceCache = {};
427
+ const sourceCache = new Map();
394
428
 
395
429
  for (const filename of sourceList) {
396
430
  const content = await Util.readFile(path.resolve(dir, filename));
@@ -402,7 +436,7 @@ const readSourceList = async (dir, sourceList) => {
402
436
  continue;
403
437
  }
404
438
  if (json.url) {
405
- sourceCache[json.url] = json;
439
+ sourceCache.set(json.url, json);
406
440
  }
407
441
  }
408
442
 
@@ -442,16 +476,16 @@ const readFromDir = async (mcr, dir) => {
442
476
 
443
477
  const entryFilter = mcr.getEntryFilter();
444
478
 
445
- const results = [];
446
479
  for (const filename of coverageList) {
447
480
  const coverageData = await readCoverageData(dir, filename, entryFilter, sourceCache);
448
481
  if (coverageData) {
449
- const res = await mcr.add(coverageData);
450
- results.push(res);
482
+ await mcr.add(coverageData);
451
483
  }
452
484
  }
453
485
 
454
- return results;
486
+ // GC
487
+ sourceCache.clear();
488
+
455
489
  };
456
490
 
457
491
  module.exports = {
package/lib/index.d.ts CHANGED
@@ -156,6 +156,8 @@ declare namespace MCR {
156
156
  outputFile?: string;
157
157
  }] |
158
158
  ['raw'] | ['raw', {
159
+ merge?: boolean;
160
+ zip?: boolean;
159
161
  outputDir?: string;
160
162
  }] |
161
163
  [string] | [string, {
@@ -402,6 +404,13 @@ declare namespace MCR {
402
404
  */
403
405
  cleanCache?: boolean;
404
406
 
407
+ /**
408
+ * {number} gc threshold
409
+ * for example: sets gc to 1024 means that force gc when the memory > 1024M at certain critical stages
410
+ * https://nodejs.org/docs/latest/api/v8.html#v8setflagsfromstringflags
411
+ */
412
+ gc?: number;
413
+
405
414
  /** (V8 only) {function} onEntry hook */
406
415
  onEntry?: (entry: V8CoverageEntry) => Promise<void>;
407
416
 
package/lib/index.js CHANGED
@@ -68,6 +68,10 @@ class CoverageReport {
68
68
  // init logging for clean after generated
69
69
  this.logging = Util.initLoggingLevel(this.options.logging);
70
70
 
71
+ if (Util.isNum(this.options.gc)) {
72
+ Util.setGC(this.options.gc);
73
+ }
74
+
71
75
  // init outputDir
72
76
  const outputDir = `${this.options.outputDir || defaultOptions.outputDir}`;
73
77
  this.options.outputDir = outputDir;
@@ -124,9 +128,8 @@ class CoverageReport {
124
128
  // add coverage from dir
125
129
  async addFromDir(dir) {
126
130
  const time_start = Date.now();
127
- const results = await readFromDir(this, dir);
131
+ await readFromDir(this, dir);
128
132
  Util.logTime(`added from dir: ${dir}`, time_start);
129
- return results;
130
133
  }
131
134
 
132
135
  // generate report
@@ -144,16 +144,19 @@ const addUntestedFiles = async (istanbulData, options) => {
144
144
 
145
145
  };
146
146
 
147
- const mergeIstanbulCoverage = async (dataList, options) => {
147
+ const mergeIstanbulDataList = (dataList, options) => {
148
148
  const istanbulCoverageList = dataList.map((it) => it.data);
149
149
  const coverageMap = istanbulLibCoverage.createCoverageMap();
150
150
  istanbulCoverageList.forEach((coverage) => {
151
151
  coverageMap.merge(coverage);
152
152
  });
153
153
  const istanbulData = coverageMap.toJSON();
154
+ return istanbulData;
155
+ };
154
156
 
157
+ const mergeIstanbulCoverage = async (dataList, options) => {
158
+ const istanbulData = mergeIstanbulDataList(dataList, options);
155
159
  await addUntestedFiles(istanbulData, options);
156
-
157
160
  return istanbulData;
158
161
  };
159
162
 
@@ -169,6 +172,7 @@ const initIstanbulData = (istanbulData, options) => {
169
172
 
170
173
  module.exports = {
171
174
  saveIstanbulReports,
175
+ mergeIstanbulDataList,
172
176
  mergeIstanbulCoverage,
173
177
  initIstanbulData
174
178
  };