Bind Cson: Reactive way to read/write cson files using ES6 Proxies.
Shamelessly forked from bjson.
What do you need to do when you need to edit a cson file programmatically?
Without bcson | With bcson |
---|---|
Create cson file if not exists | |
Read cson file. | Read cson file (Will create if not exists) |
Deserialize cson file. | |
Edit parsed object. | Edit object (Will reactively save in cson file) |
Serialize new object. | |
Write back into file. |
Editing .cson file without bcson
fs = require('fs')
unless fs.existsSync('settings.cson')
cson.writeFileSync 'settings.cson', {}
settings = cson.readFileSync('settings.cson')
settings.foo = 'bar'
cson.writeFileSync 'settings.cson', settings
Editing .cson file with bcson
bcson = require('bcson')
settings = bcson('settings.cson')
settings.foo = 'bar'
settings.cson
{}
whatever.coffee
bcson = require('bcson')
settings = bcson('settings') # will read or create settings.cson
settings.prop = 'bar'
# one liner:
# settings = require('bcson', prop: 'bar')
settings.cson:
prop: "bar"
whatever.coffee
bcson = require('bcson')
settings = bcson 'settings', {}, (object) ->
console.log JSON.stringify(object)
return
settings.prop = 'foo'
settings.otherprop = 'bar'
Run npm test
. Uses mocha and chai.