diff --git a/lib/index.js b/lib/index.js index 08a91c1..8777251 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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.