1 | var sty = require('sty');
|
2 | var helper = require('./helper');
|
3 |
|
4 | function Reporter(options){
|
5 | var options = options || {};
|
6 |
|
7 | this._runner = options.runner;
|
8 | this._lastLine = null;
|
9 | this._totalNum = 0;
|
10 | this._failed = 0;
|
11 | this._passed = 0;
|
12 | this._progress = 0;
|
13 | this._started = null;
|
14 | this._succinct = options.runner._options.succinct;
|
15 |
|
16 | this._runner.on('start', this._start.bind(this));
|
17 | this._runner.on('fileComplete', this._fileComplete.bind(this));
|
18 | this._runner.on('complete', this._end.bind(this));
|
19 | }
|
20 |
|
21 | Reporter.prototype._start = function(files){
|
22 | var self = this;
|
23 | this._started = Date.now();
|
24 | this._totalNum = files.length;
|
25 | this.updateStatus();
|
26 | this._interval = setInterval(function(){
|
27 | self.updateStatus();
|
28 | }, 1000);
|
29 | }
|
30 |
|
31 | Reporter.prototype._fileComplete = function(err, file, output){
|
32 | if( err ) {
|
33 | this._failed++;
|
34 |
|
35 | if(!this._succinct) {
|
36 | console.log(sty.yellow('\n' + 'node ' + file + '\n' + helper.strRepeat('=', file.length + 5)));
|
37 | output.forEach(function(line){
|
38 | console.log(sty.bold(line.data));
|
39 | });
|
40 | }
|
41 | } else {
|
42 | this._passed++;
|
43 | }
|
44 | this._progress++;
|
45 | this.updateStatus();
|
46 | }
|
47 |
|
48 | Reporter.prototype._end = function(data){
|
49 | var self = this;
|
50 | clearInterval(this._interval);
|
51 | this._runner.setTime(this.timeTaken());
|
52 | var code = ( this._failed > 0 || data.code > 0 ) ? 1 : 0;
|
53 |
|
54 | var out = '\n';
|
55 |
|
56 | if( code > 0 && data.message != '' ){
|
57 | out += sty.red(data.message) + '\n';
|
58 | }
|
59 |
|
60 | process.stdout.write(out);
|
61 | process.stdout.once('drain', function(){
|
62 | if( self._runner._cb === null){
|
63 | process.exit(code);
|
64 | }
|
65 | });
|
66 | }
|
67 |
|
68 | Reporter.prototype.clearLine = function() {
|
69 | if (!this._lastLine) return;
|
70 |
|
71 | var spaces = helper.strRepeat(' ', this._lastLine.length);
|
72 |
|
73 | process.stdout.write('\r' + spaces + '\r');
|
74 | };
|
75 |
|
76 | Reporter.prototype.updateStatus = function() {
|
77 | var lineout = '\r~ ' + this.timeTaken() + ' ' + this._failed + ' ' + this.getProgress() + ' ~';
|
78 | if( this._failed > 0 ){
|
79 | lineout = sty.red(lineout);
|
80 | }
|
81 | this.clearLine();
|
82 | process.stdout.write(lineout);
|
83 | this._lastLine = lineout;
|
84 | };
|
85 |
|
86 | Reporter.prototype.timeTaken = function(){
|
87 | var diff = new Date - this._started;
|
88 |
|
89 | var seconds = Math.floor((diff) / 1000);
|
90 | var minutes = Math.floor(seconds / 60);
|
91 | var hours = Math.floor(minutes / 60);
|
92 |
|
93 |
|
94 | seconds = seconds % 60;
|
95 | minutes = minutes % 60;
|
96 |
|
97 |
|
98 | seconds = seconds < 10 ? '0' + seconds : seconds;
|
99 | minutes = minutes < 10 ? '0' + minutes : minutes;
|
100 | hours = hours < 10 ? '0' + hours : hours;
|
101 |
|
102 |
|
103 | return hours + ':' + minutes + ':' + seconds;
|
104 | }
|
105 |
|
106 | Reporter.prototype.getProgress = function(){
|
107 | if( this._totalNum < 1 ){
|
108 | return '0/0 0%';
|
109 | }
|
110 |
|
111 | var percent = ( (this._progress / this._totalNum) * 100 ).toFixed(1);
|
112 |
|
113 | return this._progress + '/' + this._totalNum + ' ' + percent + '%' + this.getProgressBar(percent);
|
114 | }
|
115 |
|
116 | Reporter.prototype.getProgressBar = function (percent) {
|
117 | if(!this._runner._options.progressBar) return '';
|
118 | var chunks = Math.floor(percent / 5);
|
119 | return ' [' + helper.strRepeat('=', chunks - 1) + '>' + helper.strRepeat('-', 20 - chunks) + ']';
|
120 | }
|
121 |
|
122 | module.exports = Reporter; |
\ | No newline at end of file |