mirror of
https://github.com/overte-org/overte.git
synced 2025-04-19 16:23:39 +02:00
Add config.js
This commit is contained in:
parent
d1cf68b4e8
commit
0ccf3d41a0
1 changed files with 47 additions and 0 deletions
47
console/src/modules/config.js
Normal file
47
console/src/modules/config.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
var fs = require('fs');
|
||||
var extend = require('extend');
|
||||
|
||||
function Config() {
|
||||
this.data = {};
|
||||
}
|
||||
Config.prototype = {
|
||||
load: function(filePath) {
|
||||
// open file
|
||||
var rawData = null;
|
||||
try {
|
||||
rawData = fs.readFileSync(filePath);
|
||||
} catch(e) {
|
||||
console.log("Config file not found");
|
||||
}
|
||||
var configData = {};
|
||||
|
||||
// read file and json parse
|
||||
try {
|
||||
if (rawData) {
|
||||
configData = JSON.parse(rawData);
|
||||
} else {
|
||||
configData = {};
|
||||
}
|
||||
} catch(e) {
|
||||
console.error("Error parsing config file", filePath)
|
||||
}
|
||||
|
||||
this.data = {};
|
||||
extend(true, this.data, configData);
|
||||
},
|
||||
save: function(filePath) {
|
||||
fs.writeFileSync(filePath, JSON.stringify(this.data));
|
||||
},
|
||||
get: function(key, defaultValue) {
|
||||
if (this.data.hasOwnProperty(key)) {
|
||||
return this.data[key];
|
||||
}
|
||||
return defaultValue;
|
||||
},
|
||||
set: function(key, value) {
|
||||
console.log("Setting", key, "to", value);
|
||||
this.data[key] = value;
|
||||
}
|
||||
};
|
||||
|
||||
exports.Config = Config;
|
Loading…
Reference in a new issue