Skip to content

Commit 582f49c

Browse files
authored
Merge pull request #941 from nellh/json-exit-cleanly
Support large JSON output from CLI without truncating
2 parents ce34c3c + 4315903 commit 582f49c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

bids-validator/cli.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ const errorToString = err => {
2525
else return err
2626
}
2727

28+
/**
29+
* Write a large string or buffer to stdout and wait for the drain event
30+
*
31+
* This is needed to avoid truncating buffered output when piped
32+
* @param {string} data
33+
* @param {function} cb
34+
*/
35+
const writeStdout = (data, cb) => {
36+
if (!process.stdout.write(data)) {
37+
process.stdout.once('drain', cb)
38+
} else {
39+
process.nextTick(cb)
40+
}
41+
}
42+
2843
export default function(dir, options) {
2944
process.on('unhandledRejection', err => {
3045
console.log(
@@ -39,8 +54,9 @@ export default function(dir, options) {
3954
if (fs.existsSync(dir)) {
4055
if (options.json) {
4156
validate.BIDS(dir, options, function(issues, summary) {
42-
console.log(JSON.stringify({ issues, summary }))
43-
exitProcess(issues)
57+
writeStdout(JSON.stringify({ issues, summary }), () =>
58+
exitProcess(issues),
59+
)
4460
})
4561
} else {
4662
validate.BIDS(dir, options, function(issues, summary) {

0 commit comments

Comments
 (0)