40 lines
767 B
JavaScript
40 lines
767 B
JavaScript
|
var fs = require('fs');
|
||
|
var path = require('path');
|
||
|
|
||
|
class confLoader {
|
||
|
constructor() {
|
||
|
this._conf = null;
|
||
|
this._path = null;
|
||
|
}
|
||
|
|
||
|
load(p) {
|
||
|
var self = this;
|
||
|
if (!p || p == undefined) {
|
||
|
p = path.resolve(__dirname, '..', 'config.json');
|
||
|
}
|
||
|
self._path = p;
|
||
|
if (!fs.existsSync(p)) {
|
||
|
throw `Config file not exists , file location : ${p}`;
|
||
|
}
|
||
|
|
||
|
var filecontent = fs.readFileSync(p);
|
||
|
if (!filecontent) {
|
||
|
throw `Config file read fail`;
|
||
|
}
|
||
|
filecontent = filecontent.toString();
|
||
|
var conf = null;
|
||
|
try {
|
||
|
conf = JSON.parse(filecontent);
|
||
|
self._conf = conf;
|
||
|
} catch (e) {
|
||
|
throw e;
|
||
|
}
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
get(key) {
|
||
|
return this._conf[key];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = confLoader;
|