UNPKG

1.51 kBJavaScriptView Raw
1'use strict';
2
3const chalk = require('chalk');
4const program = require('commander');
5const pth = require('path');
6
7const stats = {
8 total: 0,
9 errors: 0,
10 ok: 0
11};
12const reports = [];
13const reportNames = {};
14const buffer = [];
15
16module.exports = {
17 addReports(names) {
18 names.forEach(function(name) {
19 var moduleName = pth.extname(name) === '.js' ? name : './report/' + name;
20 if (reportNames[moduleName]) {
21 return;
22 }
23
24 try {
25 reportNames[moduleName] = true;
26 reports.push(require(moduleName));
27 } catch (e) {
28 console.error(chalk.red('Can\'t load report module "' + moduleName + '".'));
29 console.error(chalk.red(e));
30 }
31 });
32 },
33 oneach(err, data, dictionary) {
34 var isError = err || (!err && data.data && data.data.length);
35 stats.total++;
36
37 if (isError) {
38 stats.errors++;
39
40 buffer.push([err, data]);
41 } else {
42 stats.ok++;
43
44 if (!program.onlyErrors) {
45 buffer.push([err, data]);
46 }
47 }
48
49 if ((program.onlyErrors && isError) || !program.onlyErrors) {
50 reports.forEach(function(name) {
51 name.oneach && name.oneach(err, data, dictionary);
52 });
53 }
54 },
55 onend() {
56 reports.forEach(function(name) {
57 name.onend && name.onend(buffer, stats);
58 });
59 }
60};