Skip to content

Commit 0c4c68f

Browse files
author
Ju Liu
committed
Ensure we finish writing the report to stdout
1 parent 0324675 commit 0c4c68f

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

lib/report.js

+36-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const styledMessage = require('./styled-message');
22

33
module.exports = report;
44

5-
function report(options, result) {
5+
async function report(options, result) {
66
if (options.report === 'json') {
77
return print(options, jsonReport(result.errors));
88
}
@@ -22,22 +22,46 @@ function jsonReport(errors) {
2222
function print(options, json) {
2323
if (options.reportOnOneLine) {
2424
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');
3137
})
32-
);
33-
});
34-
});
38+
.join('\n')
39+
);
40+
}
3541
} else {
36-
console.log(JSON.stringify(json));
42+
return safeConsoleLog(JSON.stringify(json));
3743
}
3844
} else {
39-
console.log(
45+
return safeConsoleLog(
4046
JSON.stringify(json, null, options.debug || options.forTests ? 2 : 0)
4147
);
4248
}
4349
}
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+
}

lib/run-review.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function runReview(options, app) {
2323
});
2424
Benchmark.end(options, 'review');
2525

26-
report(options, result);
26+
await report(options, result);
2727

2828
return result.success;
2929
}

lib/runner.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ async function initializeApp(options, elmModulePath, reviewElmJson) {
7676
autofix.subscribe(options, app);
7777
if (options.watch) {
7878
AppState.subscribe(app.ports.reviewReport, (result) => {
79-
report(options, result);
80-
const shouldReReview = AppState.reviewFinished();
81-
if (shouldReReview) {
82-
return startReview(options, app);
83-
}
79+
return report(options, result).then(() => {
80+
const shouldReReview = AppState.reviewFinished();
81+
if (shouldReReview) {
82+
return startReview(options, app);
83+
}
84+
});
8485
});
8586
}
8687

0 commit comments

Comments
 (0)