Skip to content

Commit

Permalink
Ensure we finish writing the report to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Ju Liu committed Oct 15, 2020
1 parent 0324675 commit f6d9a1a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
46 changes: 34 additions & 12 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const styledMessage = require('./styled-message');

module.exports = report;

function report(options, result) {
async function report(options, result) {
if (options.report === 'json') {
return print(options, jsonReport(result.errors));
}
Expand All @@ -22,22 +22,44 @@ function jsonReport(errors) {
function print(options, json) {
if (options.reportOnOneLine) {
if (json.type === 'review-errors') {
json.errors.forEach((errorForFile) => {
errorForFile.errors.forEach((error) => {
console.log(
JSON.stringify({
path: errorForFile.path,
...error
if (json.errors.length > 0) {
return safeConsoleLog(
json.errors
.flatMap((errorForFile) => {
return errorForFile.errors.map((error) => {
return JSON.stringify({
path: errorForFile.path,
...error
});
});
})
);
});
});
.join('\n')
);
}
} else {
console.log(JSON.stringify(json));
return safeConsoleLog(JSON.stringify(json));
}
} else {
console.log(
return safeConsoleLog(
JSON.stringify(json, null, options.debug || options.forTests ? 2 : 0)
);
}
}

// Printing large outputs to stdout is not recommended because at times
// console.log is asynchronous and returns before ensuring that the whole
// output has been printed. Check out these links for more details:
//
// - https://nodejs.org/api/process.html#process_process_exit_code
// - /~https://github.com/nodejs/node/issues/6456
// - /~https://github.com/nodejs/node/issues/19218
//
// Using process.stdout.write and passing a function ensures that
// the whole output has been written.
function safeConsoleLog(message) {
return new Promise((resolve) => {
process.stdout.write(message + '\n', () => {
resolve();
});
});
}
2 changes: 1 addition & 1 deletion lib/run-review.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function runReview(options, app) {
});
Benchmark.end(options, 'review');

report(options, result);
await report(options, result);

return result.success;
}
Expand Down
11 changes: 6 additions & 5 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ async function initializeApp(options, elmModulePath, reviewElmJson) {
autofix.subscribe(options, app);
if (options.watch) {
AppState.subscribe(app.ports.reviewReport, (result) => {
report(options, result);
const shouldReReview = AppState.reviewFinished();
if (shouldReReview) {
return startReview(options, app);
}
return report(options, result).then(() => {
const shouldReReview = AppState.reviewFinished();
if (shouldReReview) {
return startReview(options, app);
}
});
});
}

Expand Down

0 comments on commit f6d9a1a

Please sign in to comment.