UNPKG

3.3 kBJavaScriptView Raw
1var sty = require('sty');
2var helper = require('./helper');
3
4function 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
21Reporter.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
31Reporter.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){ // Write each line from the child process
38 console.log(sty.bold(line.data));
39 });
40 }
41 } else {
42 this._passed++;
43 }
44 this._progress++;
45 this.updateStatus();
46}
47
48Reporter.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
68Reporter.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
76Reporter.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
86Reporter.prototype.timeTaken = function(){
87 var diff = new Date - this._started;
88
89 var seconds = Math.floor((diff) / 1000); // Milliseconds to seconds
90 var minutes = Math.floor(seconds / 60); // Seconds to minutes
91 var hours = Math.floor(minutes / 60); // Minutes to hours
92
93 // Get the remainder from 60 so we don't get higher than that!
94 seconds = seconds % 60;
95 minutes = minutes % 60;
96
97 // Pad with zeroes
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
106Reporter.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
116Reporter.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
122module.exports = Reporter;
\No newline at end of file