Code style, new dependencies

Use the prototype way to create git-stats instances. With this feature we could create as many git-stats instances we need, by providing the configuration path.

Added typpy and deffy packages as dependencies.
This commit is contained in:
Ionică Bizău 2015-07-12 15:17:46 +03:00
parent e93ece67f7
commit 28a2881f9e
3 changed files with 60 additions and 37 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env node
// Dependencies
var GitStats = require("../lib")
var GitStats = new (require("../lib"))()
, Moment = require("moment")
, Logger = require("bug-killer")
, CLP = require("clp")

View file

@ -10,17 +10,18 @@ var Ul = require("ul")
, CliGhCal = require("cli-gh-cal")
, GitLogParser = require("gitlog-parser").parse
, ChildProcess = require("child_process")
, Deffy = require("deffy")
, Typpy = require("typpy")
, Exec = ChildProcess.exec
, Spawn = ChildProcess.spawn
;
// Constants
const STORE_PATH = Abs("~/.git-stats")
, DATE_FORMAT = "MMM D, YYYY"
;
const DATE_FORMAT = "MMM D, YYYY";
// Constructor
var GitStats = module.exports = {};
function GitStats(confPath) {
this.path = Deffy(confPath, Abs("~/.git-stats"));
}
/**
* record
@ -37,16 +38,19 @@ var GitStats = module.exports = {};
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` object.
*/
GitStats.record = function (data, callback) {
GitStats.prototype.record = function (data, callback) {
var self = this;
// Validate data
callback = callback || function (err) { if (err) throw err; };
data = Object(data);
if (typeof data.date === "string") {
data.date = new Moment(new Date(data.date));
}
if (!data.date || !/^Moment|Date$/.test(data.date.constructor.name)) {
if (!/^moment|date$/.test(Typpy(data.date))) {
callback(new Error("The date field should be a string or a date object."));
return GitStats;
}
@ -62,8 +66,9 @@ GitStats.record = function (data, callback) {
}
// Get stats
GitStats.get(function (err, stats) {
self.get(function (err, stats) {
stats = stats || {};
var day = data.date.format(DATE_FORMAT)
, today = stats[day] = Object(stats[day])
, repo = today[data.url] = Object(today[data.url])
@ -71,10 +76,10 @@ GitStats.record = function (data, callback) {
repo[data.hash] = { date: data.date };
GitStats.save(stats, callback);
self.save(stats, callback);
});
return GitStats;
return self;
};
/**
@ -86,11 +91,12 @@ GitStats.record = function (data, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` object.
*/
GitStats.get = function (callback) {
ReadJson(STORE_PATH, function (err, data) {
GitStats.prototype.get = function (callback) {
var self = this;
ReadJson(self.path, function (err, data) {
if (err && err.code === "ENOENT") {
return GitStats.save({}, function (err) {
return self.save({}, function (err) {
callback(err, {});
});
}
@ -98,7 +104,7 @@ GitStats.get = function (callback) {
if (err) { return callback(err); }
callback(null, data);
});
return GitStats;
return self;
};
/**
@ -111,9 +117,9 @@ GitStats.get = function (callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` object.
*/
GitStats.save = function (stats, callback) {
WriteJson(STORE_PATH, stats, callback);
return GitStats;
GitStats.prototype.save = function (stats, callback) {
WriteJson(self.path, stats, callback);
return this;
};
/**
@ -131,7 +137,7 @@ GitStats.save = function (stats, callback) {
* @param {Function} callback The callback function called with the current day formatted (type: string) and the `Moment` date object.
* @return {GitStats} The `GitStats` object.
*/
GitStats.iterateDays = function (data, callback) {
GitStats.prototype.iterateDays = function (data, callback) {
if (typeof data === "function") {
callback = data;
@ -156,7 +162,7 @@ GitStats.iterateDays = function (data, callback) {
start.add(1, "days");
}
return GitStats;
return this;
};
/**
@ -169,15 +175,17 @@ GitStats.iterateDays = function (data, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` object.
*/
GitStats.graph = function (data, callback) {
GitStats.prototype.graph = function (data, callback) {
if (typeof data === "function") {
callback = data;
data = undefined;
}
var self = this;
// Get commits
GitStats.get(function (err, stats) {
self.get(function (err, stats) {
if (err) { return callback(err); }
var cDayObj = null
@ -185,7 +193,7 @@ GitStats.graph = function (data, callback) {
;
// Iterate days
GitStats.iterateDays(data, function (cDay) {
self.iterateDays(data, function (cDay) {
cDayObj = year[cDay] = {
_: stats[cDay] || {}
, c: 0
@ -199,7 +207,7 @@ GitStats.graph = function (data, callback) {
callback(null, year);
});
return GitStats;
return self;
};
/**
@ -212,8 +220,11 @@ GitStats.graph = function (data, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` object.
*/
GitStats.calendar = function (data, callback) {
GitStats.graph(data, function (err, graph) {
GitStats.prototype.calendar = function (data, callback) {
var self = this;
self.graph(data, function (err, graph) {
if (err) { return callback(err); }
var cal = { total: 0, days: {}, cStreak: 0, lStreak: 0, max: 0 }
@ -252,7 +263,7 @@ GitStats.calendar = function (data, callback) {
callback(null, cal);
});
return GitStats;
return self;
};
/**
@ -265,17 +276,19 @@ GitStats.calendar = function (data, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` object.
*/
GitStats.ansiCalendar = function (data, callback) {
GitStats.prototype.ansiCalendar = function (data, callback) {
if (typeof data === "function") {
callback = data;
data = undefined;
}
GitStats.graph(data, function (err, graph) {
var self = this;
self.graph(data, function (err, graph) {
var cal = [];
GitStats.iterateDays(data, function (cDay) {
self.iterateDays(data, function (cDay) {
cDayObj = graph[cDay];
if (!cDayObj) { return; }
cal.push([cDay, cDayObj.c]);
@ -288,10 +301,10 @@ GitStats.ansiCalendar = function (data, callback) {
}));
});
return GitStats;
return self;
};
GitStats.authors = function (options, callback) {
GitStats.prototype.authors = function (options, callback) {
var repo = new Gry(options.repo);
repo.exec("shortlog -s -n --all", function (err, stdout) {
if (err) { return callback(err); }
@ -307,9 +320,10 @@ GitStats.authors = function (options, callback) {
});
callback(null, pieData);
});
return this;
};
GitStats.authorsPie = function (options, callback) {
GitStats.prototype.authorsPie = function (options, callback) {
if (typeof options === "string") {
options = {
repo: options
@ -324,12 +338,13 @@ GitStats.authorsPie = function (options, callback) {
return callback(new Error("The repository folder doesn't exist."));
}
var repo = new Gry(options.repo)
var self = this
, repo = new Gry(options.repo)
, pie = null
, pieData = []
;
GitStats.authors(options, function (err, authors) {
self.authors(options, function (err, authors) {
if (err) { return callback(err); }
if (authors.length > 50) {
var others = {
@ -350,9 +365,11 @@ GitStats.authorsPie = function (options, callback) {
callback(null, pie.toString());
});
return self;
};
GitStats.globalActivity = function (options, callback) {
GitStats.prototype.globalActivity = function (options, callback) {
if (typeof options === "string") {
options = {
@ -387,4 +404,8 @@ GitStats.globalActivity = function (options, callback) {
, end: options.end
}));
});
return this;
};
module.exports = GitStats;

View file

@ -44,6 +44,8 @@
"abs": "^1.0.0",
"gry": "^4.0.0",
"gitlog-parser": "0.0.2",
"progress": "1.1.8"
"progress": "1.1.8",
"deffy": "2.0.0",
"typpy": "2.0.0"
}
}