Skip to content

Instantly share code, notes, and snippets.

@rumkin
Created July 5, 2016 16:19
Show Gist options
  • Save rumkin/c6462b7a7d95677940b13b33d5d405f6 to your computer and use it in GitHub Desktop.
Save rumkin/c6462b7a7d95677940b13b33d5d405f6 to your computer and use it in GitHub Desktop.
Prettify json passed to stdio and output to stdout
#!/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