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