allow inplace overwrite
This commit is contained in:
19
lib/cli.js
19
lib/cli.js
@@ -17,9 +17,13 @@ var options = require("nomnom")
|
||||
return JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")).version;
|
||||
}
|
||||
},
|
||||
inplace : {
|
||||
string: '-i, --in-place',
|
||||
help: 'overwrite the file'
|
||||
},
|
||||
indent : {
|
||||
string: '-t CHAR, --indent CHAR',
|
||||
default: " ",
|
||||
default: " ",
|
||||
help: 'character(s) to use for indentation'
|
||||
}
|
||||
})
|
||||
@@ -28,7 +32,7 @@ var options = require("nomnom")
|
||||
|
||||
function parse (source) {
|
||||
try {
|
||||
sys.puts(JSON.stringify(parser.parse(source),null," "));
|
||||
return JSON.stringify(parser.parse(source),null,options.indent);
|
||||
} catch (e) {
|
||||
sys.puts(e);
|
||||
process.exit(1);
|
||||
@@ -38,8 +42,13 @@ function parse (source) {
|
||||
function main (args) {
|
||||
var source = '';
|
||||
if (options.file) {
|
||||
source = fs.readFileSync(require('path').join(process.cwd(), options.file), "utf8");
|
||||
parse(source);
|
||||
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.pupts(parse(source));
|
||||
}
|
||||
} else {
|
||||
var stdin = process.openStdin();
|
||||
stdin.setEncoding('utf8');
|
||||
@@ -48,7 +57,7 @@ function main (args) {
|
||||
source += chunk.toString('utf8');
|
||||
});
|
||||
stdin.on('end', function () {
|
||||
parse(source);
|
||||
sys.puts(parse(source));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user