Permalink
Browse files

added json-prettify (was json-pp)

  • Loading branch information...
1 parent f9a2082 commit 880abe28c5283d892e2d2b4a0f4e72ef45c78ad9 @coolaj86 committed Jan 22, 2012
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,7 +1,7 @@
{
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
"name": "json-minify",
- "description": "A commandline utility to pretty-print JSON files",
+ "description": "A commandline utility to minify JSON files",
"version": "1.0.0",
"main": "index.js",
"bin": {
View
@@ -1,16 +0,0 @@
-Pretty-Print JSON
-===
-
-Usage
----
-
- pp-json /path/to/data.json
-
- curl -s http://foobar3000.com/echo/echo.json | pp-json
-
- wget -qO- http://foobar3000.com/echo/echo.json | pp-json
-
-Installation
----
-
- npm install -g pp-json
View
@@ -1,65 +0,0 @@
-#!/usr/bin/env node
-(function () {
- "use strict";
-
- var fs = require('fs')
- , prettyPrint = require('./json2').stringify
- , filename = process.argv[2]
- , json
- ;
-
- function ppJson() {
- json = prettyPrint(json, null, ' ');
- console.log(json);
- }
-
- function readJsonFile() {
- try {
- json = JSON.parse(fs.readFileSync(filename));
- } catch(e) {
- console.error("[ERROR] couldn't parse JSON from '" + filename + "':");
- console.error(e.message);
- }
-
- ppJson();
- }
-
- function readJsonStdin() {
- var timeoutToken
- , stdin = process.openStdin()
- ;
-
- timeoutToken = setTimeout(function () {
- console.warn("Usage: pp-json /path/to/data.json");
- console.warn("Usage: echo '{ \"foo\": \"bar\" }' | pp-json");
- process.exit();
- }, 1000);
-
- json = '';
-
- stdin.on('data', function (chunk) {
- clearTimeout(timeoutToken);
- json += chunk;
- });
-
- stdin.on('end', function () {
- try {
- json = JSON.parse(json);
- } catch(e) {
- console.error("[ERROR] couldn't parse JSON from stdin:");
- console.error(e.message);
- return;
- }
-
- ppJson();
- });
- }
-
- if (filename) {
- readJsonFile();
- }
- else {
- readJsonStdin();
- }
-
-}());
View
@@ -0,0 +1,27 @@
+Pretty-Print JSON
+===
+
+The opposite of `json-minify`;
+For all the times you want to add readable whitespace.
+
+ {"foo":"bar"}
+
+becomes
+
+ {
+ "foo": "bar"
+ }
+
+Usage
+---
+
+ json-prettify /path/to/data.json
+
+ curl -s http://foobar3000.com/echo/echo.json | json-prettify
+
+ wget -qO- http://foobar3000.com/echo/echo.json | json-prettify
+
+Installation
+---
+
+ npm install -g json-prettify
File renamed without changes.
View
@@ -0,0 +1,95 @@
+#!/usr/bin/env node
+(function () {
+ "use strict";
+
+ var fs = require('fs')
+ , filename = process.argv[2]
+ // real handler
+ , prettyPrint = require('./json2').stringify
+ , json
+ ;
+
+ /*
+ *
+ * Begin real handler
+ *
+ */
+
+ function printUsage() {
+ console.warn("Usages:");
+ console.warn("json-prettify example.json");
+ console.warn("cat example.json | json-prettify");
+ }
+
+ function handleInput(err, text) {
+ var data
+ ;
+
+ if (err) {
+ printUsage();
+ return;
+ }
+
+ data = JSON.parse(text);
+ console.info(prettyPrint(data, null, ' '));
+ }
+
+ /*
+ *
+ * End real handler
+ *
+ */
+
+ readInput(handleInput, filename);
+
+ //
+ // this could (and probably should) be its own module
+ //
+ function readInput(cb, filename) {
+
+ function readFile() {
+ fs.readFile(filename, 'utf8', function (err, text) {
+ if (err) {
+ console.error("[ERROR] couldn't read from '" + filename + "':");
+ console.error(err.message);
+ return;
+ }
+
+ cb(err, text);
+ });
+ }
+
+ function readStdin() {
+ var text = ''
+ , timeoutToken
+ , stdin = process.stdin
+ ;
+
+ stdin.resume();
+
+ // how to tell piping vs waiting for user input?
+ timeoutToken = setTimeout(function () {
+ cb(new Error('no stdin data'));
+ stdin.pause();
+ }, 1000);
+
+ stdin.on('data', function (chunk) {
+ clearTimeout(timeoutToken);
+ text += chunk;
+ });
+
+ stdin.on('end', function () {
+ cb(null, text);
+ });
+ }
+
+ if (filename) {
+ readFile();
+ }
+ else {
+ readStdin();
+ }
+
+ }
+
+}());
File renamed without changes.
@@ -1,16 +1,16 @@
{
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
- "name": "pp-json",
+ "name": "json-prettify",
"description": "A commandline utility to pretty-print JSON files",
- "version": "1.1.0",
+ "version": "1.2.0",
"main": "index.js",
"bin": {
- "pp-json": "./index.js"
+ "json-prettify": "./index.js"
},
"engines": {
"node": ">= 0.2.0"
},
- "test": ["a", "b", "c"],
+ "test": ["cat example.json | node index.js example.json", "node index.js example.json"],
"dependencies": {},
"devDependencies": {},
"preferGlobal": true

0 comments on commit 880abe2

Please sign in to comment.