git-stats/lib/index.js

108 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-01-25 20:49:49 +01:00
// Dependencies
var FsExtra = require("fs-extra")
, Ul = require("ul")
2015-01-26 08:56:49 +01:00
, Moment = require("moment")
2015-01-25 20:49:49 +01:00
;
2015-01-25 20:54:02 +01:00
const STORE_PATH = Ul.USER_DIR + "/.git-stats";
2015-01-25 20:49:49 +01:00
// Constructor
var GitStats = module.exports = {};
2015-01-26 08:56:49 +01:00
/**
* record
* Records a new commit.
*
* @name record
* @function
* @param {Object} data The commit data containing:
*
2015-01-26 09:47:13 +01:00
* - `date` (String|Date): The date object or a string in a format that can be parsed.
2015-01-26 08:56:49 +01:00
* - `url` (String): The repository remote url.
* - `hash` (String): The commit hash.
*
* @param {Function} callback The callback function.
* @return {undefined}
*/
2015-01-25 20:54:02 +01:00
GitStats.record = function (data, callback) {
2015-01-26 08:58:03 +01:00
// Validate data
2015-01-26 08:56:49 +01:00
callback = callback || function (err) { if (err) throw err; };
data = Object(data);
if (typeof data.date === "string") {
2015-01-26 09:47:13 +01:00
data.date = new Moment(new Date(data.date));
2015-01-26 08:56:49 +01:00
}
2015-01-26 09:21:40 +01:00
if (!data.date || !/^Moment|Date$/.test(data.date.constructor.name)) {
2015-01-26 08:56:49 +01:00
return callback(new Error("The date field should be a string or a date object."));
}
if (typeof data.hash !== "string" || !data.hash) {
return callback(new Error("Invalid hash."));
}
if (typeof data.url !== "string" || !data.url) {
return callback(new Error("Invalid url field."));
}
2015-01-26 08:58:03 +01:00
// Get stats
GitStats.get(function (err, stats) {
2015-01-25 20:54:02 +01:00
stats = stats || {};
2015-01-26 10:27:48 +01:00
var day = data.date.format("MMM D, YYYY")
2015-01-26 08:56:49 +01:00
, today = stats[day] = Object(stats[day])
, repo = today[data.url] = Object(today[data.url])
;
repo[data.hash] = { date: data.date };
2015-01-25 20:54:02 +01:00
FsExtra.writeJSON(STORE_PATH, stats, callback);
});
};
2015-01-26 09:05:46 +01:00
/**
* get
* Gets the git stats.
*
* @name get
* @function
* @param {Object} data The stats filter. **Not yet implemented**.
* @param {Function} callback The callback function.
* @return {undefined}
*/
2015-01-25 20:54:02 +01:00
GitStats.get = function (data, callback) {
if (typeof data === "function") {
callback = data;
data = {};
}
FsExtra.readJSON(STORE_PATH, callback);
2015-01-25 20:49:49 +01:00
};
2015-01-26 10:27:48 +01:00
GitStats.graph = function (data, callback) {
GitStats.get(data, function (err, stats) {
if (err) { return callback(err); }
var year = {}
, end = Moment()
, start = Moment().subtract({ years: 1 })
, cDay = null
, cDayObj = null
;
while (!(start > end)) {
cDay = start.format("MMM D, YYYY");
cDayObj = year[cDay] = {
_: stats[cDay] || {}
, c: 0
};
Object.keys(cDayObj._).forEach(function (c) {
cDayObj.c += Object.keys(cDayObj._[c]).length;
});
start.add(1, "days")
}
callback(null, year);
// ⬚ ▢ ▤ ▣ ⬛
});
};