@@ -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,44 @@ 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
+ . flatMap ( ( errorForFile ) => {
29
+ return errorForFile . errors . map ( ( error ) => {
30
+ return JSON . stringify ( {
31
+ path : errorForFile . path ,
32
+ ...error
33
+ } ) ;
34
+ } ) ;
31
35
} )
32
- ) ;
33
- } ) ;
34
- } ) ;
36
+ . join ( '\n' )
37
+ ) ;
38
+ }
35
39
} else {
36
- console . log ( JSON . stringify ( json ) ) ;
40
+ return safeConsoleLog ( JSON . stringify ( json ) ) ;
37
41
}
38
42
} else {
39
- console . log (
43
+ return safeConsoleLog (
40
44
JSON . stringify ( json , null , options . debug || options . forTests ? 2 : 0 )
41
45
) ;
42
46
}
43
47
}
48
+
49
+ // Printing large outputs to stdout is not recommended because at times
50
+ // console.log is asynchronous and returns before ensuring that the whole
51
+ // output has been printed. Check out these links for more details:
52
+ //
53
+ // - https://nodejs.org/api/process.html#process_process_exit_code
54
+ // - https://github.com/nodejs/node/issues/6456
55
+ // - https://github.com/nodejs/node/issues/19218
56
+ //
57
+ // Using process.stdout.write and passing a function ensures that
58
+ // the whole output has been written.
59
+ function safeConsoleLog ( message ) {
60
+ return new Promise ( ( resolve ) => {
61
+ process . stdout . write ( message + '\n' , ( ) => {
62
+ resolve ( ) ;
63
+ } ) ;
64
+ } ) ;
65
+ }
0 commit comments