-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata.js
62 lines (47 loc) · 1.12 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Data
* To store and retrieve data
*
* @author Mohammad Fares <[email protected]>
*/
var fs = require('fs-extra')
, onChange = require('on-change')
, yaml = require('js-yaml');
/**
* The path of the data file
* @type {String}
*/
var DATA_FILE_NAME = __dirname + '/data.yml';
/**
* The path of the default data file
* @type {String}
*/
var DEFAULT_DATA_FILE_NAME = __dirname + '/data.default.yml';
/**
* The content of the data file in YAML
* @type {String}
*/
var dataContent = null;
/**
* To store the configurations
* @type {Object}
*/
var data = null;
// The config file is not exist
if (!fs.existsSync(DATA_FILE_NAME)) {
// Copy the default config file
fs.copySync(DEFAULT_DATA_FILE_NAME, DATA_FILE_NAME);
}
// Read the data file
dataContent = fs.readFileSync(DATA_FILE_NAME, 'utf8');
// Parse the data file into an object
data = yaml.load(dataContent);
/**
* A deep proxy to trap the changes and save them
*/
module.exports = onChange(data, function() {
process.nextTick(function() {
// Save the data
fs.writeFileSync(DATA_FILE_NAME, yaml.dump(data), 'utf8');
});
});