Added the getConfig and initConfig methods

This commit is contained in:
Ionică Bizău 2015-09-14 17:15:14 +03:00
parent 7865ddcbcc
commit f28e390309

View file

@ -35,6 +35,7 @@ const DATE_FORMAT = "MMM D, YYYY"
*/
function GitStats(dataPath) {
this.path = Abs(Deffy(dataPath, DEFAULT_STORE));
this.config = {};
}
// Defaults
@ -66,6 +67,50 @@ GitStats.DEFAULT_CONFIG = {
, global_activity: false
};
GitStats.prototype.getConfig = function (callback) {
if (callback) {
ReadJson(CONFIG_PATH, function (err, data) {
callback(err && err.code !== "ENOENT" ? err : null, data || {});
});
} else {
try {
return ReadJson(CONFIG_PATH);
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
return {};
}
}
};
GitStats.prototype.initConfig = function (input, callback) {
var self = this;
if (Typpy(input, Function)) {
callback = input;
input = null;
}
input = input || CONFIG_PATH;
// Handle object input
if (Typpy(input, Object)) {
this.config = Ul.deepMerge(this.config, GitStats.DEFAULT_CONFIG);
callback && callback(null, this.config);
return this.config;
}
if (callback) {
this.getConfig(function (err, data) {
if (err) { return callback(err); }
self.initConfig(data, callback);
});
} else {
this.initConfig(this.getConfig());
}
};
/**
* record
* Records a new commit.