From f28e3903094bde31d40138d9bf72cef4876f0e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionic=C4=83=20Biz=C4=83u?= Date: Mon, 14 Sep 2015 17:15:14 +0300 Subject: [PATCH] Added the getConfig and initConfig methods --- lib/index.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) 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.