Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make report async #23

Merged
merged 2 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 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,46 @@ 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
.map((errorForFile) => {
return errorForFile.errors
.map((error) => {
return JSON.stringify({
path: errorForFile.path,
...error
});
})
.join('\n');
})
);
});
});
.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
2 changes: 1 addition & 1 deletion test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function runCommandAndCompareToSnapshot {
echo -e "\n \e[31mbut got:\e[0m\n"
cat "$TMP/$FILE"
echo -e "\n \e[31mHere is the difference:\e[0m\n"
diff "$TMP/$FILE" "$SNAPSHOTS/$FILE"
diff -p "$TMP/$FILE" "$SNAPSHOTS/$FILE"
exit 1
else
echo -e " \e[92mOK\e[0m"
Expand Down