Skip to content

Commit

Permalink
add --single-json output option
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
zats authored and derhuerst committed Nov 30, 2020
1 parent 0532257 commit 5db1f11
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
24 changes: 18 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const argv = mri(process.argv.slice(2), {
'help', 'h',
'version', 'v',
'length-prefixed', 'l',
'json', 'j'
'json', 'j',
'single-json', 's',
]
})

Expand All @@ -22,7 +23,8 @@ Usage:
Options:
--length-prefixed -l Read input as length-prefixed.
See https://www.npmjs.com/package/length-prefixed-stream
--json -j Output JSON instead of a pretty represenation.
--json -j Output newline-delimeted JSON (http://ndjson.org).
--single-json -s Output a single JSON array.
--depth -d Number of nested levels to print. Default: infinite
Examples:
curl 'https://example.org/gtfs-rt.pbf' | print-gtfs-rt
Expand Down Expand Up @@ -60,7 +62,8 @@ const read = (readable) => {
}

const isLengthPrefixed = argv['length-prefixed'] || argv.l
const printAsJSON = argv.json || argv.j
const printAsNDJSON = argv.json || argv.j
const printAsJSON = argv['single-json'] || argv.s
const printWithColors = isatty(process.stdout.fd)
const depth = argv.depth || argv.d ? parseInt(argv.depth || argv.d) : null

Expand All @@ -70,11 +73,20 @@ const onFeedMessage = (buf) => {
throw new Error('invalid feed')
}

for (const entity of data.entity) {
const msg = printAsJSON
if (printAsJSON) {
process.stdout.write('[\n')
}
for (var i = 0; i < data.entity.length; ++i) {
const entity = data.entity[i];
const msg = printAsNDJSON || printAsJSON
? JSON.stringify(entity)
: inspect(entity, {depth, colors: printWithColors})
process.stdout.write(msg + '\n')
const isLastEntity = i == data.entity.length - 1;
const delimeter = (printAsJSON && !isLastEntity) ? ',\n' : '\n'
process.stdout.write(msg + delimeter)
}
if (printAsJSON) {
process.stdout.write(']\n')
}
}

Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Usage:
Options:
--length-prefixed -l Read input as length-prefixed.
See https://www.npmjs.com/package/length-prefixed-stream
--json -j Output JSON instead of a pretty represenation.
--json -j Output newline-delimeted JSON (http://ndjson.org).
--single-json -s Output a single JSON array.
--depth -d Number of nested levels to print. Default: infinite
Examples:
curl 'https://example.org/gtfs-rt.pbf' | print-gtfs-rt
```
Expand Down

0 comments on commit 5db1f11

Please sign in to comment.