jprichardson / node-jsonfile

Easily read/write JSON files.
MIT License
1.2k stars 321 forks source link

Add updateFile and updateFileSync methods #18

Closed piranna closed 9 years ago

piranna commented 9 years ago

Instead of needing to call readFile, modify the Json, and later call writeFile, add an updateFile method that accept a literal object and use it's attributes and hierarchy to update the content of the Json file. To remove entries, they can be set to undefined, so JSON.stringify() will ignore them later.

jprichardson commented 9 years ago

This is way out of scope for this library.

Ultimately, in a updateFile() method, you'd still need to call fs.writeFile(). There are so many json/object diff libraries out there, that you could easily create your own updateFile() and updateFileSync() methods in conjunction with this library.

piranna commented 9 years ago

I'm not sure if it doesn't falls in the scope of the library (I think it does). In any case, I've crafted my own updateFile function by using the merge:

function updateFile(file, obj, callback)
{
  jsonfile.readFile(file, function(error, orig)
  {
    if(error) return callback(error)

    jsonfile.writeFile(file, recursive(orig, obj), callback)
  })
}