-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonpp.js
executable file
·53 lines (47 loc) · 1.01 KB
/
jsonpp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env node
var options = {
infile: {
position: 0,
required: true,
help: "JSON file to parse"
},
outfile: {
position: 1,
required: false,
help: "file to write to. if not specified, output will be printed to stdout"
},
spaces: {
abbr: "s",
help: "number of spaces to use when indenting. default is 2",
default: 2
},
useTabs: {
abbr: "t",
full: "use-tabs",
flag: true,
help: "use tabs instead of spaces. overrides --spaces",
default: false
}
};
var opts = require("nomnom").options(options).parse();
var fs = require("fs");
fs.readFile(opts.infile, function(err, data) {
if (err) {
console.log(err);
return;
}
var whitespace = opts.spaces;
if (opts.useTabs)
whitespace = "\t";
prettyData = JSON.stringify(JSON.parse(data), null, whitespace);
if (opts.outfile) {
fs.writeFile(opts.outfile, prettyData, function(err) {
if (err) {
console.log(err);
}
});
}
else {
console.log(prettyData);
}
});