@@ -2,7 +2,7 @@ const styledMessage = require('./styled-message');
2
2
3
3
module . exports = report ;
4
4
5
- function report ( options , result ) {
5
+ async function report ( options , result ) {
6
6
if ( options . report === 'json' ) {
7
7
return print ( options , jsonReport ( result . errors ) ) ;
8
8
}
@@ -22,22 +22,46 @@ function jsonReport(errors) {
22
22
function print ( options , json ) {
23
23
if ( options . reportOnOneLine ) {
24
24
if ( json . type === 'review-errors' ) {
25
- json . errors . forEach ( ( errorForFile ) => {
26
- errorForFile . errors . forEach ( ( error ) => {
27
- console . log (
28
- JSON . stringify ( {
29
- path : errorForFile . path ,
30
- ...error
25
+ if ( json . errors . length > 0 ) {
26
+ return safeConsoleLog (
27
+ json . errors
28
+ . map ( ( errorForFile ) => {
29
+ return errorForFile . errors
30
+ . map ( ( error ) => {
31
+ return JSON . stringify ( {
32
+ path : errorForFile . path ,
33
+ ...error
34
+ } ) ;
35
+ } )
36
+ . join ( '\n' ) ;
31
37
} )
32
- ) ;
33
- } ) ;
34
- } ) ;
38
+ . join ( '\n' )
39
+ ) ;
40
+ }
35
41
} else {
36
- console . log ( JSON . stringify ( json ) ) ;
42
+ return safeConsoleLog ( JSON . stringify ( json ) ) ;
37
43
}
38
44
} else {
39
- console . log (
45
+ return safeConsoleLog (
40
46
JSON . stringify ( json , null , options . debug || options . forTests ? 2 : 0 )
41
47
) ;
42
48
}
43
49
}
50
+
51
+ // Printing large outputs to stdout is not recommended because at times
52
+ // console.log is asynchronous and returns before ensuring that the whole
53
+ // output has been printed. Check out these links for more details:
54
+ //
55
+ // - https://nodejs.org/api/process.html#process_process_exit_code
56
+ // - https://github.com/nodejs/node/issues/6456
57
+ // - https://github.com/nodejs/node/issues/19218
58
+ //
59
+ // Using process.stdout.write and passing a function ensures that
60
+ // the whole output has been written.
61
+ function safeConsoleLog ( message ) {
62
+ return new Promise ( ( resolve ) => {
63
+ process . stdout . write ( message + '\n' , ( ) => {
64
+ resolve ( ) ;
65
+ } ) ;
66
+ } ) ;
67
+ }
0 commit comments