Created
July 5, 2016 16:19
-
-
Save rumkin/c6462b7a7d95677940b13b33d5d405f6 to your computer and use it in GitHub Desktop.
Prettify json passed to stdio and output to stdout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Stdin JSON prettifier | |
// Partially based on https://gist.github.com/kristopherjohnson/5065599 | |
const {inspect} = require('util'); | |
const {stdin, stdout} = process; | |
var input = ''; | |
stdin.setEncoding('utf8'); | |
stdin.on('data', chunk => { | |
input += chunk; | |
}); | |
stdin.on('end', () => { | |
var data = JSON.parse(input); | |
var output; | |
if (process.env.COLOR) { | |
output = inspect(data, {colors: true}); | |
} else { | |
output = JSON.stringify(data, null, process.env.TAB_SIZE || 4); | |
} | |
stdout.write(output); | |
stdout.write('\n'); | |
}); | |
// Start reading input | |
stdin.resume(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment