Comparing version 1.0.1 to 1.1.0
#!/usr/bin/env node | ||
var sys = require("sys"); | ||
var fs = require("fs"); | ||
var parser = require("./jsonlint").parser; | ||
var options = require("nomnom") | ||
.scriptName("jsonlint") | ||
.opts({ | ||
file: { | ||
position: 0, | ||
help: "file to parse; otherwise uses stdin" | ||
}, | ||
version: { | ||
string: '-v, --version', | ||
help: 'print version and exit', | ||
callback: function() { | ||
return JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")).version; | ||
} | ||
}, | ||
sort : { | ||
string: '-s, --sort-keys', | ||
help: 'sort object keys' | ||
}, | ||
inplace : { | ||
string: '-i, --in-place', | ||
help: 'overwrite the file' | ||
}, | ||
indent : { | ||
string: '-t CHAR, --indent CHAR', | ||
default: " ", | ||
help: 'character(s) to use for indentation' | ||
} | ||
}) | ||
.parseArgs(); | ||
function parse (source) { | ||
try { | ||
sys.puts(JSON.stringify(parser.parse(source),null," ")); | ||
var parsed = options.sort ? | ||
sortObject(parser.parse(source)) : | ||
parser.parse(source); | ||
return JSON.stringify(parsed,null,options.indent); | ||
} catch (e) { | ||
@@ -17,5 +51,10 @@ sys.puts(e); | ||
var source = ''; | ||
if (args[1]) { | ||
source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8"); | ||
parse(source); | ||
if (options.file) { | ||
var path = require('path').join(process.cwd(), options.file); | ||
source = fs.readFileSync(path, "utf8"); | ||
if (options.inplace) { | ||
fs.writeSync(fs.openSync(path,'w+'), parse(source), 0, "utf8"); | ||
} else { | ||
sys.puts(parse(source)); | ||
} | ||
} else { | ||
@@ -29,3 +68,3 @@ var stdin = process.openStdin(); | ||
stdin.on('end', function () { | ||
parse(source); | ||
sys.puts(parse(source)); | ||
}); | ||
@@ -35,2 +74,23 @@ } | ||
// from http://stackoverflow.com/questions/1359761/sorting-a-json-object-in-javascript | ||
function sortObject(o) { | ||
if (Object.prototype.toString.call(o) != '[object Object]') | ||
return o; | ||
var sorted = {}, | ||
key, a = []; | ||
for (key in o) { | ||
if (o.hasOwnProperty(key)) { | ||
a.push(key); | ||
} | ||
} | ||
a.sort(); | ||
for (key = 0; key < a.length; key++) { | ||
sorted[a[key]] = sortObject(o[a[key]]); | ||
} | ||
return sorted; | ||
} | ||
main(process.argv.slice(1)); |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"preferGlobal": true, | ||
@@ -28,3 +28,5 @@ "repository": { | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"nomnom": ">= 0.4.3" | ||
}, | ||
"devDependencies": { | ||
@@ -31,0 +33,0 @@ "test": "*", |
53057
954
1
+ Addednomnom@>= 0.4.3
+ Addedansi-styles@1.0.0(transitive)
+ Addedchalk@0.4.0(transitive)
+ Addedhas-color@0.1.7(transitive)
+ Addednomnom@1.8.1(transitive)
+ Addedstrip-ansi@0.1.1(transitive)
+ Addedunderscore@1.6.0(transitive)