monocart-coverage-reports 2.6.3 → 2.6.5

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/index.d.ts CHANGED
@@ -185,10 +185,10 @@ declare namespace MCR {
185
185
  js?: boolean;
186
186
  /** V8 only */
187
187
  data?: {
188
- bytes: CoverageRange[];
189
- statements: CoverageRange[];
190
- branches: CoverageRange[];
191
- functions: CoverageRange[];
188
+ bytes?: CoverageRange[];
189
+ statements?: CoverageRange[];
190
+ branches?: CoverageRange[];
191
+ functions?: CoverageRange[];
192
192
  ignores?: IgnoredRange[];
193
193
  /** codecov json format */
194
194
  lines: {
@@ -211,11 +211,8 @@ declare namespace MCR {
211
211
  /** {string} logging levels: off, error, info, debug */
212
212
  logging?: string;
213
213
 
214
- /** {string} output dir */
215
- outputDir?: string;
216
-
217
- /** {string|string[]} input raw dir(s) */
218
- inputDir?: string | string[];
214
+ /** {string} Report name. Defaults to "Coverage Report". */
215
+ name?: string;
219
216
 
220
217
  /**
221
218
  *
@@ -224,25 +221,21 @@ declare namespace MCR {
224
221
  * {array} ['v8'], ['v8', ['console-details', { skipPercent: 80 }]]
225
222
  *
226
223
  * By default, `v8` for V8 data, `html` for Istanbul data
227
- */
228
- reports?: string | ReportDescription[];
224
+ */
225
+ reports?: string | string[] | ReportDescription[];
229
226
 
230
- /** {string} Report name. Defaults to "Coverage Report". */
231
- name?: string;
227
+ /** {string} output dir */
228
+ outputDir?: string;
232
229
 
233
- /** (V8 only) {string} Output [sub dir/]filename. Defaults to "index.html" */
234
- outputFile?: string;
235
- /** (V8 only) {boolean} Inline all scripts to the single HTML file. Defaults to false. */
236
- inline?: boolean;
237
- /** (V8 only) {string} Assets path if not inline. Defaults to "./assets" */
238
- assetsPath?: string;
230
+ /** {string|string[]} input raw dir(s) */
231
+ inputDir?: string | string[];
239
232
 
240
233
  /** (V8 only)
241
234
  *
242
235
  * {string} `minimatch` pattern for entry url; {object} multiple patterns;
243
236
  *
244
237
  * {function} A filter function for each entry file in the V8 list.
245
- * */
238
+ */
246
239
  entryFilter?: string | {
247
240
  [pattern: string]: boolean;
248
241
  } | ((entry: V8CoverageEntry) => boolean);
@@ -252,11 +245,33 @@ declare namespace MCR {
252
245
  * {string} `minimatch` pattern for source path; {object} multiple patterns;
253
246
  *
254
247
  * {function} A filter function for each source path when the source is unpacked from the source map.
255
- * */
248
+ */
256
249
  sourceFilter?: string | {
257
250
  [pattern: string]: boolean;
258
251
  } | ((sourcePath: string) => boolean);
259
252
 
253
+ /**
254
+ * {function} Source path handler.
255
+ *
256
+ * {object} Replace key with value.
257
+ * */
258
+ sourcePath?: ((filePath: string) => string) | {
259
+ [key: string]: string;
260
+ };
261
+
262
+
263
+ /** (V8 only) {string} Output [sub dir/]filename. Defaults to "index.html" */
264
+ outputFile?: string;
265
+ /** (V8 only) {boolean} Inline all scripts to the single HTML file. Defaults to false. */
266
+ inline?: boolean;
267
+ /** (V8 only) {string} Assets path if not inline. Defaults to "./assets" */
268
+ assetsPath?: string;
269
+
270
+ /** (Istanbul only) defaultSummarizer, sourceFinder */
271
+
272
+ /** {boolean} Generate lcov.info file, same as lcovonly report. Defaults to false. */
273
+ lcov?: boolean;
274
+
260
275
  /**
261
276
  * options for adding empty coverage for all files
262
277
  */
@@ -278,20 +293,6 @@ declare namespace MCR {
278
293
  /** (V8 only) {boolean} Enable/Disable ignoring uncovered codes with the special comments: v8 ignore next/next N/start/stop */
279
294
  v8Ignore?: boolean;
280
295
 
281
- /** (Istanbul only) defaultSummarizer, sourceFinder */
282
-
283
- /** {boolean} Generate lcov.info file, same as lcovonly report. Defaults to false. */
284
- lcov?: boolean;
285
-
286
- /**
287
- * {function} Source path handler.
288
- *
289
- * {object} Replace key with value.
290
- * */
291
- sourcePath?: ((filePath: string) => string) | {
292
- [key: string]: string;
293
- };
294
-
295
296
  /** {string|function} Specify the report path, especially when there are multiple reports. Defaults to outputDir/index.html. */
296
297
  reportPath?: string | (() => string);
297
298
 
@@ -299,11 +300,15 @@ declare namespace MCR {
299
300
  * {object} { bytes:[50,80], statements:[50,80], branches:[50,80], functions:[50,80], lines:[50,80] } */
300
301
  watermarks?: Watermarks;
301
302
 
302
- /** {boolean} Indicates whether to clean previous files in output dir before generating report. Defaults to true. */
303
+ /**
304
+ * {boolean} Indicates whether to clean previous reports in output dir before generating new reports. Defaults to true.
305
+ *
306
+ * If true, the API `clean()` will execute automatically.
307
+ * */
303
308
  clean?: boolean;
304
309
 
305
310
  /**
306
- * {boolean} Indicates whether to clean previous cache in output dir before generating report. Defaults to false.
311
+ * {boolean} Indicates whether to clean previous cache in output dir on start. Defaults to false.
307
312
  *
308
313
  * If true, the API `cleanCache()` will execute automatically.
309
314
  */
@@ -319,10 +324,10 @@ declare namespace MCR {
319
324
 
320
325
  export interface McrCliOptions extends CoverageReportOptions {
321
326
 
322
- /** (cli only) {function} onStart hook */
327
+ /** (CLI only) {function} onStart hook */
323
328
  onStart?: (coverageReport: CoverageReport) => Promise<void>;
324
329
 
325
- /** (cli only) {function} onReady hook before adding coverage data.
330
+ /** (CLI only) {function} onReady hook before adding coverage data.
326
331
  *
327
332
  * Sometimes, the child process has not yet finished writing the coverage data, and it needs to wait here.
328
333
  */
@@ -332,9 +337,14 @@ declare namespace MCR {
332
337
 
333
338
  export class CoverageReport {
334
339
 
340
+ /**
341
+ * @param options coverage report options
342
+ */
335
343
  constructor(options?: CoverageReportOptions);
336
344
 
337
- /** add coverage data: {array} V8 format, {object} Istanbul format */
345
+ /** add coverage data
346
+ *
347
+ * @param coverageData {array} V8 format, {object} Istanbul format */
338
348
  add: (coverageData: any[] | any) => Promise<AddedResults>;
339
349
 
340
350
  /** generate report */
@@ -345,6 +355,27 @@ declare namespace MCR {
345
355
 
346
356
  /** clean previous cache, return `false` if no cache */
347
357
  cleanCache: () => boolean;
358
+
359
+ /** clean previous reports except cache dir and v8 coverage dir */
360
+ clean: () => void;
361
+
362
+ /** get entry filter handler, it can be used to filter the coverage data list before adding it. */
363
+ getEntryFilter: () => ((entry: V8CoverageEntry) => boolean);
364
+
365
+ /** load config file.
366
+ *
367
+ * @param configFile custom config file path
368
+ *
369
+ * Supports loading default config file if no custom config specified:
370
+ 'mcr.config.js',
371
+ 'mcr.config.cjs',
372
+ 'mcr.config.mjs',
373
+ 'mcr.config.json',
374
+ '.mcrrc.js',
375
+ '.mcrrc',
376
+ 'mcr.config.ts'
377
+ */
378
+ loadConfig: (configFile?: string) => Promise<void>;
348
379
  }
349
380
 
350
381
  export interface CoverageSnapshot {
package/lib/index.js CHANGED
@@ -1,5 +1,8 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
+ const { pathToFileURL } = require('url');
4
+ const EC = require('eight-colors');
5
+
3
6
  const defaultOptions = require('./default/options.js');
4
7
  const Util = require('./utils/util.js');
5
8
 
@@ -7,26 +10,46 @@ const { initV8ListAndSourcemap, getEntryFilter } = require('./v8/v8.js');
7
10
  const { getInputData, generateCoverageReports } = require('./generate.js');
8
11
  const { getSnapshot, diffSnapshot } = require('./utils/snapshot.js');
9
12
 
13
+ const resolveConfigOptions = async (configPath) => {
14
+ // json format
15
+ const ext = path.extname(configPath);
16
+ if (ext === '.json' || configPath.slice(-2) === 'rc') {
17
+ return JSON.parse(Util.readFileSync(configPath));
18
+ }
19
+
20
+ let configOptions;
21
+ let err;
22
+ try {
23
+ configOptions = await import(pathToFileURL(configPath));
24
+ } catch (ee) {
25
+ err = ee;
26
+ }
27
+
28
+ if (err || !configOptions) {
29
+ Util.logError(`ERROR: failed to load config "${configPath}": ${err && err.message} `);
30
+ return;
31
+ }
32
+
33
+ configOptions = configOptions.default || configOptions;
34
+
35
+ return configOptions;
36
+ };
37
+
10
38
  class CoverageReport {
11
39
 
12
40
  constructor(options = {}) {
13
41
  this.cacheDirName = '.cache';
42
+ this.constructorOptions = options;
14
43
  this.options = {
15
44
  ... defaultOptions,
16
45
  ... options
17
46
  };
18
-
19
47
  this.initOptions();
20
- // If true, the API `cleanCache()` will execute automatically.
21
- if (this.options.cleanCache) {
22
- this.cleanCache();
23
- }
24
-
25
48
  }
26
49
 
27
- initOptions() {
50
+ initOptions(force) {
28
51
 
29
- if (this.fixedOptions) {
52
+ if (this.fixedOptions && !force) {
30
53
  return;
31
54
  }
32
55
 
@@ -43,6 +66,11 @@ class CoverageReport {
43
66
  const cacheDir = path.resolve(outputDir, this.cacheDirName);
44
67
  this.options.cacheDir = cacheDir;
45
68
 
69
+ // If true, the API `cleanCache()` will execute automatically.
70
+ if (this.options.cleanCache) {
71
+ this.cleanCache();
72
+ }
73
+
46
74
  this.fixedOptions = true;
47
75
  }
48
76
 
@@ -125,7 +153,7 @@ class CoverageReport {
125
153
  if (fs.existsSync(outputDir)) {
126
154
  // if assets dir is out of output dir will be ignore
127
155
  if (this.options.clean) {
128
- this.cleanFiles();
156
+ this.clean();
129
157
  }
130
158
  } else {
131
159
  fs.mkdirSync(outputDir, {
@@ -155,7 +183,7 @@ class CoverageReport {
155
183
  return fs.existsSync(this.options.cacheDir);
156
184
  }
157
185
 
158
- // cache clean
186
+ // cache clean dir
159
187
  cleanCache() {
160
188
  // clean previous artifacts
161
189
  if (this.hasCache()) {
@@ -165,7 +193,8 @@ class CoverageReport {
165
193
  return false;
166
194
  }
167
195
 
168
- cleanFiles() {
196
+ // clean previous reports except cache dir and v8 coverage dir
197
+ clean() {
169
198
  const outputDir = this.options.outputDir;
170
199
  let v8DirName;
171
200
  const nodeV8CoverageDir = process.env.NODE_V8_COVERAGE;
@@ -183,10 +212,56 @@ class CoverageReport {
183
212
  });
184
213
  }
185
214
 
215
+ // get entry filter handler
186
216
  getEntryFilter() {
187
217
  return getEntryFilter(this.options);
188
218
  }
189
219
 
220
+ // load config file
221
+ async loadConfig(customConfigFile) {
222
+
223
+ // order by priority
224
+ const configList = customConfigFile ? [customConfigFile] : [
225
+ 'mcr.config.js',
226
+ 'mcr.config.cjs',
227
+ 'mcr.config.mjs',
228
+ 'mcr.config.json',
229
+ '.mcrrc.js',
230
+ '.mcrrc',
231
+ 'mcr.config.ts'
232
+ ];
233
+
234
+ const existsList = configList.filter((p) => p && fs.existsSync(p));
235
+ if (!existsList.length) {
236
+ if (customConfigFile) {
237
+ Util.logError(`The config file does not exist: ${customConfigFile}`);
238
+ }
239
+ // no config
240
+ return;
241
+ }
242
+
243
+ // first one, high priority
244
+ const configPath = existsList[0];
245
+ const configOptions = await resolveConfigOptions(configPath);
246
+ if (!configOptions) {
247
+ // failed to load config options
248
+ if (customConfigFile) {
249
+ Util.logError(`Failed to load config file: ${customConfigFile}`);
250
+ }
251
+ return;
252
+ }
253
+
254
+ Util.logInfo(`Loaded config: ${EC.cyan(configPath)}`);
255
+ // init options again
256
+ this.options = {
257
+ ... defaultOptions,
258
+ ... configOptions,
259
+ ... this.constructorOptions
260
+ };
261
+ this.initOptions(true);
262
+
263
+ }
264
+
190
265
  }
191
266
 
192
267
  /** create coverage report */