Comparing version
@@ -14,3 +14,3 @@ #!/usr/bin/env node | ||
'<file> <key-path>', | ||
'<file> <key-path> <value>', | ||
'<file> <key-path> <value> [--indent=<n>]', | ||
'<file> <key-path> --delete', | ||
@@ -20,2 +20,3 @@ ]; | ||
var options = [ | ||
'--indent=<n> Indent with <n> of white space characters [default: 2]', | ||
'-d --delete Delete the key-path', | ||
@@ -33,4 +34,7 @@ '-h --help Show this message with options', | ||
var dot_json = new DotJson(args['<file>']); | ||
if (args['<value>']) { | ||
var value = args['<value>']; | ||
var indent = args['--indent']; | ||
switch(value) { | ||
@@ -62,3 +66,3 @@ case 'true': | ||
} | ||
dot_json.save(); | ||
dot_json.save(parseInt(indent)); | ||
} | ||
@@ -65,0 +69,0 @@ else if (args['--delete']) { |
@@ -20,3 +20,3 @@ var path = require('path'); | ||
this._file = undefined; | ||
this._stat = {}; | ||
@@ -27,3 +27,3 @@ | ||
_this._wait_for_read = false; | ||
this.file = function(file) { | ||
@@ -139,3 +139,3 @@ _this._file = path.resolve(file); | ||
if (parent_object === undefined) { | ||
this._setValueForKeyPath(parent_path, isIntString(tip_key) ? [] : {}); | ||
@@ -146,3 +146,3 @@ } | ||
this.save = function(callback) { | ||
this.save = function(indent, callback) { | ||
@@ -152,5 +152,9 @@ if (_this._file === undefined) { | ||
} | ||
var dump = function () { | ||
return JSON.stringify(_this._object, null, indent) + '\n'; | ||
} | ||
if (typeof callback !== 'function') { | ||
fs.writeFileSync(_this._file, JSON.stringify(_this._object, null, ' ')); | ||
fs.writeFileSync(_this._file, dump()); | ||
return this; | ||
@@ -163,3 +167,3 @@ } | ||
} | ||
fs.writeFile(_this._file, JSON.stringify(_this._object, null, ' '), function() { | ||
fs.writeFile(_this._file, dump(), function() { | ||
read_callback(); | ||
@@ -169,3 +173,3 @@ callback(); | ||
}); | ||
if ( ! _this._wait_for_read) { | ||
@@ -189,3 +193,3 @@ _this._exec_read_queue(); | ||
top = parseInt(top); | ||
} | ||
} | ||
var base = key_array.join('.'); | ||
@@ -204,3 +208,3 @@ var part = keypath(_this._object).valueForKeyPath(base); | ||
} | ||
} | ||
@@ -207,0 +211,0 @@ |
{ | ||
"name": "dot-json", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Easily edit a json file from the CLI or NodeJS", | ||
@@ -5,0 +5,0 @@ "main": "lib/dot-json.js", |
@@ -118,3 +118,3 @@ var expect = require('chai').expect; | ||
}); | ||
describe("#_readJsonSync()", function() { | ||
@@ -216,3 +216,3 @@ | ||
}); | ||
}); | ||
@@ -495,3 +495,3 @@ | ||
)); | ||
}); | ||
@@ -507,3 +507,3 @@ | ||
dot_json.set('ob4.3.', "fourth"); | ||
expect(JSON.stringify(dot_json._object)).equal(JSON.stringify( | ||
@@ -564,3 +564,3 @@ { | ||
var dot_json = new DotJson(); | ||
dot_json.set('my.path', "my_string").file(file).save(function() { | ||
dot_json.set('my.path', "my_string").file(file).save(2, function() { | ||
fs.readFile(file, function(err, content) { | ||
@@ -585,3 +585,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
expect(err).not.equal(null); | ||
dot_json.set('write_test_string', 'testing new file string').save(function() { | ||
dot_json.set('write_test_string', 'testing new file string').save(2, function() { | ||
fs.stat(file, function(err, stat) { | ||
@@ -618,3 +618,3 @@ expect(err).equal(null); | ||
var date_string = new Date().toString(); | ||
dot_json.set('set_get_string', date_string).save(function() { | ||
dot_json.set('set_get_string', date_string).save(2, function() { | ||
fs.readFile(file, function(err, content) { | ||
@@ -642,3 +642,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
var date_string = new Date().toString(); | ||
dot_json.set('set_get_string', date_string).save(function() { | ||
dot_json.set('set_get_string', date_string).save(2, function() { | ||
fs.readFile(file, function(err, content) { | ||
@@ -666,3 +666,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
var date_string = new Date().toString(); | ||
dot_json.set('set_get_string', date_string).save(function() { | ||
dot_json.set('set_get_string', date_string).save(2, function() { | ||
var dot_json_written = new DotJson(file); | ||
@@ -688,3 +688,3 @@ expect(dot_json_written._object).equal(undefined); | ||
dot_json.get('get_set_string', function(v) { | ||
dot_json.set('get_set_string', date_string).save(function() { | ||
dot_json.set('get_set_string', date_string).save(2, function() { | ||
dot_json.get('get_set_string', function(value) { | ||
@@ -701,3 +701,3 @@ expect(value).equal(date_string); | ||
var date_string = new Date().toString(); | ||
dot_json.set('deep.string.item', date_string).save(function() { | ||
dot_json.set('deep.string.item', date_string).save(2, function() { | ||
dot_json.get('deep.string.item', function(value) { | ||
@@ -718,3 +718,3 @@ expect(value).equal(date_string); | ||
fs.writeFile(file, JSON.stringify({delete_string: "this string is going to be deleted"}), function() { | ||
dot_json.delete('delete_string').save(function() { | ||
dot_json.delete('delete_string').save(2, function() { | ||
fs.readFile(file, function(err, content) { | ||
@@ -748,3 +748,3 @@ expect(JSON.stringify(JSON.parse(content))).equal('{}'); | ||
fs.writeFile(file, JSON.stringify(object, null, ' '), function() { | ||
dot_json.delete('object_item.deeper_object_item.very_deep_item_to_delete').save(function() { | ||
dot_json.delete('object_item.deeper_object_item.very_deep_item_to_delete').save(2, function() { | ||
fs.readFile(file, function(err, content) { | ||
@@ -770,3 +770,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify(result_object)); | ||
var myfile = new DotJson("test-files/myfile-write.json"); | ||
myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com').save(function() { | ||
myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com').save(2, function() { | ||
fs.readFile("test-files/myfile-write.json", function(err, content) { | ||
@@ -814,3 +814,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
var myfile = new DotJson("test-files/myfile-delete.json"); | ||
myfile.delete('user.name').save(function() { | ||
myfile.delete('user.name').save(2, function() { | ||
fs.readFile("test-files/myfile-delete.json", function(err, content) { | ||
@@ -838,3 +838,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com'); | ||
myfile.save(function() { | ||
myfile.save(2, function() { | ||
fs.readFile("test-files/myfile.json", function(err, content) { | ||
@@ -851,3 +851,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
expect(value).equal("John Doe"); | ||
myfile.delete('user.name').save(function() { | ||
myfile.delete('user.name').save(2, function() { | ||
fs.readFile("test-files/myfile.json", function(err, content) { | ||
@@ -877,3 +877,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
var myfile = new DotJson("test-files/myfile-write-sync.json"); | ||
myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com').save(); | ||
myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com').save(2); | ||
fs.readFile("test-files/myfile-write-sync.json", function(err, content) { | ||
@@ -918,3 +918,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
var myfile = new DotJson("test-files/myfile-delete-sync.json"); | ||
myfile.delete('user.name').save(); | ||
myfile.delete('user.name').save(2); | ||
fs.readFile("test-files/myfile-delete-sync.json", function(err, content) { | ||
@@ -942,3 +942,3 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com'); | ||
myfile.save(); | ||
myfile.save(2); | ||
var content = fs.readFileSync("test-files/myfile-sync.json"); | ||
@@ -953,7 +953,7 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
)); | ||
var value = myfile.get('user.name'); | ||
expect(value).equal("John Doe"); | ||
myfile.delete('user.name').save(); | ||
myfile.delete('user.name').save(2); | ||
var content = fs.readFileSync("test-files/myfile-sync.json"); | ||
@@ -971,2 +971,2 @@ expect(JSON.stringify(JSON.parse(content))).equal(JSON.stringify( | ||
}); | ||
}); |
35465
0.46%1101
0.55%6
-14.29%