json-file-plus
Advanced tools
Comparing version
@@ -0,1 +1,5 @@ | ||
[3.2.0](https://github.com/ljharb/node-json-file/releases/tag/v3.1.0) / 2015-10-17 | ||
================== | ||
* [New] Add `readJSON.sync`, and `JSONFile#saveSync`, for command line scripts | ||
[3.1.0](https://github.com/ljharb/node-json-file/releases/tag/v3.1.0) / 2015-10-17 | ||
@@ -2,0 +6,0 @@ ================== |
12
index.js
@@ -64,2 +64,6 @@ 'use strict'; | ||
JSONFile.prototype.saveSync = function () { | ||
fs.writeFileSync(this.filename, this.stringify()); | ||
}; | ||
var readJSON = function readJSON(filename) { | ||
@@ -90,2 +94,10 @@ var callback; | ||
}; | ||
var readJSONSync = function readJSONSync(filename) { | ||
var raw = fs.readFileSync(filename, 'utf8'); | ||
return new JSONFile(filename, raw); | ||
}; | ||
readJSON.sync = readJSONSync; | ||
readJSON.JSONFile = JSONFile; | ||
@@ -92,0 +104,0 @@ readJSON.JSONData = JSONData; |
{ | ||
"name": "json-file-plus", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"author": "Jordan Harband", | ||
@@ -5,0 +5,0 @@ "description": "Read from and write to a JSON file, minimizing diffs and preserving formatting.", |
{ | ||
"name": "json-file-plus", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"author": "Jordan Harband", | ||
@@ -5,0 +5,0 @@ "description": "Read from and write to a JSON file, minimizing diffs and preserving formatting.", |
@@ -22,4 +22,6 @@ 'use strict'; | ||
var NODE_011_NOT_FOUND = -2; | ||
var NODE_010_NOT_FOUND = 34; | ||
var isFileNotFoundError = function (err) { | ||
return [34, -2].indexOf(err.errno) > -1; | ||
return [NODE_010_NOT_FOUND, NODE_011_NOT_FOUND].indexOf(err.errno) > -1; | ||
}; | ||
@@ -250,1 +252,23 @@ | ||
test('#saveSync', function (t) { | ||
var file = jsonFile.sync(testFilename); | ||
file.set({ foo: !testContents.foo }); | ||
try { | ||
file.saveSync(); | ||
t.ok(true, 'saveSync: success'); | ||
} finally { | ||
file.set({ foo: testContents.foo }); | ||
file.saveSync(); | ||
t.ok(true, 'saveSync, restore original: success'); | ||
t.end(); | ||
} | ||
}); | ||
test('sync', function (t) { | ||
t['throws'](function () { jsonFile.sync('not a filename'); }, 'nonexistent filename throws'); | ||
var file = jsonFile.sync(testFilename); | ||
t.deepEqual(file.data, testContents, 'sync file data is expected data'); | ||
t.equal(true, file instanceof jsonFile.JSONFile, 'file is JSONFile'); | ||
t.equal(true, file instanceof jsonFile.JSONData, 'file is JSONData'); | ||
t.end(); | ||
}); |
27888
4.72%444
7.51%