1 | var fs = require('fs')
|
2 | var _ = require('lodash')
|
3 | var Writer = require('./writer')
|
4 |
|
5 | function stringify(obj) {
|
6 | return JSON.stringify(obj, null, 2)
|
7 | }
|
8 |
|
9 | function Store(filename) {
|
10 | if (fs.existsSync(filename)) {
|
11 | this.object = JSON.parse(fs.readFileSync(filename))
|
12 | } else {
|
13 | fs.writeFileSync(filename, '{}')
|
14 | this.object = {}
|
15 | }
|
16 |
|
17 | this.writer = new Writer(filename)
|
18 |
|
19 | return this
|
20 | }
|
21 |
|
22 | Store.prototype.save = _.throttle(function() {
|
23 | this.writer.write(stringify(this.object))
|
24 | }, 10)
|
25 |
|
26 | module.exports = Store |
\ | No newline at end of file |