monocart-coverage-reports 2.6.3 → 2.6.4

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.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 */