-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (69 loc) · 2.04 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var readJSON = require('read-json'),
writeJSON = require('write-json'),
mkdirp = require('mkdirp-omen'),
path = require('path'),
fs = require('fs');
/*
git remote add origin https://github.com/hollowdoor/quick_config.git
git push -u origin master
*/
module.exports = function(file, defaults){
return new QuickConfig(file, defaults);
};
function QuickConfig(file, defaults){
this._file = file;
this._defaults = defaults || {};
var parts = file.split(path.sep);
if(parts.length){
parts.splice(-1, 1);
this._base = parts.join(path.sep);
}else{
this._base = null;
}
}
QuickConfig.prototype = {
constructor: QuickConfig,
save: function(json){
var self = this;
json = this._mergeDefaults(json);
return new Promise(function(resolve, reject){
if(!self._base){
writeJSON(self._file, json, function(err){
if(err) return reject(err);
resolve(json);
});
}else{
mkdirp(self._base).then(function(){
writeJSON(self._file, json, function(err){
if(err) return reject(err);
resolve(json);
});
}, function(err){
reject(err);
});
}
});
},
load: function(){
var self = this;
return new Promise(function(resolve, reject){
fs.exists(self._file, function(exists){
if(!exists){
return resolve(self._mergeDefaults({}));
}
readJSON(self._file, function(err, json){
if(err) return reject(err);
resolve(self._mergeDefaults(json));
});
});
});
},
_mergeDefaults: function(json){
for(var n in this._defaults){
if(!(n in json)){
json[n] = this._defaults[n];
}
}
return json;
}
};