1
0
mirror of synced 2025-12-16 03:14:09 +08:00

allow inplace overwrite

This commit is contained in:
Zach Carter
2011-06-01 17:42:36 -04:00
parent 26d697c45f
commit 7d7e4b499f

View File

@@ -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));
});
}
}