Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Options:
-f, --file JSON filename.
-e, --encoding JSON file encoding. [default to utf8]
-i, --indent Number of indent for each line. [default to 2]
-r, --rewrite Rewrite file [false]
-o, --output Redirect to File []
-v, --vim-plugin-mode Whether it's in VIM plugin mode. (usually use this
option in vim plugins)
```
Expand Down
26 changes: 25 additions & 1 deletion bin/jjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ const opts = require('nomnom')
help: 'Number of indent for each line.',
default: 2,
})
.option('rewrite', {
abbr: 'r',
help: 'Rewrite file.',
flag: true,
default: false
})
.option('output', {
abbr: 'o',
help: 'Output to file.',
})
.script('jjson')
.parse();

Expand Down Expand Up @@ -61,5 +71,19 @@ fs.readFile(filename, { encoding: opts.encoding }, function(err, json) {
process.exit(1);
}

console.log(json);
if (opts.rewrite) {
fs.writeFile(filename, json, err => {
if (err) {
console.error(err);
}
});
} else if (opts.output && opts.output.length > 0) {
fs.writeFile(opts.output, json, err => {
if (err) {
console.error(err);
}
});
} else {
console.log(json)
}
});