mirror of
https://github.com/IonicaBizau/git-stats.git
synced 2025-01-03 10:22:11 +01:00
Validate data.
This commit is contained in:
parent
b5fc45ba6a
commit
704f1c9a42
1 changed files with 42 additions and 2 deletions
44
lib/index.js
44
lib/index.js
|
@ -1,6 +1,7 @@
|
||||||
// Dependencies
|
// Dependencies
|
||||||
var FsExtra = require("fs-extra")
|
var FsExtra = require("fs-extra")
|
||||||
, Ul = require("ul")
|
, Ul = require("ul")
|
||||||
|
, Moment = require("moment")
|
||||||
;
|
;
|
||||||
|
|
||||||
const STORE_PATH = Ul.USER_DIR + "/.git-stats";
|
const STORE_PATH = Ul.USER_DIR + "/.git-stats";
|
||||||
|
@ -8,11 +9,50 @@ const STORE_PATH = Ul.USER_DIR + "/.git-stats";
|
||||||
// Constructor
|
// Constructor
|
||||||
var GitStats = module.exports = {};
|
var GitStats = module.exports = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* record
|
||||||
|
* Records a new commit.
|
||||||
|
*
|
||||||
|
* @name record
|
||||||
|
* @function
|
||||||
|
* @param {Object} data The commit data containing:
|
||||||
|
*
|
||||||
|
* - `date` (String|Date): The date object or a string in this format: `DDD MMM dd HH:mm:ss YYYY`
|
||||||
|
* - `url` (String): The repository remote url.
|
||||||
|
* - `hash` (String): The commit hash.
|
||||||
|
*
|
||||||
|
* @param {Function} callback The callback function.
|
||||||
|
* @return {undefined}
|
||||||
|
*/
|
||||||
GitStats.record = function (data, callback) {
|
GitStats.record = function (data, callback) {
|
||||||
|
callback = callback || function (err) { if (err) throw err; };
|
||||||
|
data = Object(data);
|
||||||
|
|
||||||
|
if (typeof data.date === "string") {
|
||||||
|
data.date = Moment(data.date, "DDD MMM dd HH:mm:ss YYYY");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data.date || data.date.constructor !== Date || isNaN(data.date.getTime())) {
|
||||||
|
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."));
|
||||||
|
}
|
||||||
|
|
||||||
FsExtra.readJSON(STORE_PATH, function (err, stats) {
|
FsExtra.readJSON(STORE_PATH, function (err, stats) {
|
||||||
stats = stats || {};
|
stats = stats || {};
|
||||||
var thisRepo = stats[data.url] = Object(stats[data.url]);
|
var day = data.date.format("MMM DDD dd, YYYY")
|
||||||
thisRepo[data.hash] = { date: data.date };
|
, today = stats[day] = Object(stats[day])
|
||||||
|
, repo = today[data.url] = Object(today[data.url])
|
||||||
|
;
|
||||||
|
|
||||||
|
repo[data.hash] = { date: data.date };
|
||||||
|
|
||||||
FsExtra.writeJSON(STORE_PATH, stats, callback);
|
FsExtra.writeJSON(STORE_PATH, stats, callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue