forked from Quacky2200/Spotify-Web-Player-for-Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-settings.js
executable file
·50 lines (50 loc) · 1.43 KB
/
app-settings.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
/*
* @author Matthew James <[email protected]>
* appSettings file for local application
*/
module.exports = function(prefFile, defaults) {
const fs = require('fs');
const _file = prefFile;
const appSettings = {
save: function(callback){
fs.writeFile(prefFile, JSON.stringify(appSettings.export()), 'utf8', callback);
},
where: function(){
return _file;
},
open: function(callback){
//Check the file exists so that we can automatically create one.
fs.access(prefFile, fs.F_OK, function(err){
//If the file doesn't exist, save it.
if(err) return appSettings.save(callback);
//Otherwise open the file, parse it and use the callback
fs.readFile(prefFile, 'utf8', function(err, data){
if (err) return callback(err);
appSettings.import(JSON.parse(data));
callback(null, appSettings);
});
});
},
import: function(prefs){
//Append all appSettings
for(var i in prefs){
if(prefs.hasOwnProperty(i)){
appSettings[i] = prefs[i];
}
}
},
export: function() {
//JSON.stringify will remove any built in functions - leaving only the data.
//parse it again and we have extracted everything.
return JSON.parse(JSON.stringify(appSettings));
}
};
//Import defaults
appSettings.import(defaults);
//Always try to open our prefFile to check we don't have their settings
appSettings.open(function(err){
if(err) throw err;
});
//Give the appSettings
return appSettings;
};