Update dependencies, restructure the code

This commit is contained in:
Ionică Bizău 2022-06-06 23:40:52 +03:00
parent 73cf0599ef
commit 3cec477879
3 changed files with 2478 additions and 642 deletions

View File

@ -1,6 +1,6 @@
"use strict";
var Ul = require("ul")
const Ul = require("ul")
, Abs = require("abs")
, ReadJson = require("r-json")
, WriteJson = require("w-json")
@ -26,6 +26,8 @@ const DATE_FORMAT = "MMM D, YYYY"
, CONFIG_PATH = Abs("~/.git-stats-config.js")
;
class GitStats {
/**
* GitStats
*
@ -34,36 +36,11 @@ const DATE_FORMAT = "MMM D, YYYY"
* @param {String} dataPath Path to the data file.
* @return {GitStats} The `GitStats` instance.
*/
function GitStats(dataPath) {
constructor (dataPath) {
this.path = Abs(Deffy(dataPath, DEFAULT_STORE));
this.config = {};
}
// Defaults
GitStats.CONFIG_PATH = CONFIG_PATH
GitStats.DEFAULT_CONFIG = {
// Dark theme by default
theme: "DARK"
// This defaults in library
, path: undefined
// This defaults in cli-gh-cal
, first_day: undefined
// This defaults to *one year ago*
, since: undefined
// This defaults to *now*
, until: undefined
// Don't show authors by default
, authors: false
// No global activity by default
, global_activity: false
};
/**
* getConfig
* Fetches the configuration object from file (`~/.git-stats-config.js`).
@ -73,8 +50,8 @@ GitStats.DEFAULT_CONFIG = {
* @param {Function} callback The callback function.
* @return {Object|Undefined} If no callback is provided, the configuration object will be returned.
*/
GitStats.prototype.getConfig = function (callback) {
var data = {}
getConfig (callback) {
let data = {}
, err = null
;
@ -96,7 +73,7 @@ GitStats.prototype.getConfig = function (callback) {
}
return data;
};
}
/**
* initConfig
@ -107,9 +84,9 @@ GitStats.prototype.getConfig = function (callback) {
* @param {Object|String} input The path to a custom git-stats configuration file or the configuration object.
* @param {Function} callback The callback function.
*/
GitStats.prototype.initConfig = function (input, callback) {
initConfig (input, callback) {
var self = this;
const self = this;
if (Typpy(input, Function)) {
callback = input;
@ -133,7 +110,7 @@ GitStats.prototype.initConfig = function (input, callback) {
} else {
this.initConfig(this.getConfig());
}
};
}
/**
* record
@ -152,9 +129,9 @@ GitStats.prototype.initConfig = function (input, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.record = function (data, callback) {
record (data, callback) {
var self = this;
const self = this;
// Validate data
callback = callback || function (err) { if (err) throw err; };
@ -184,7 +161,7 @@ GitStats.prototype.record = function (data, callback) {
function modify (err, stats) {
var commits = stats.commits
const commits = stats.commits
, day = data.date.format(DATE_FORMAT)
, today = commits[day] = Object(commits[day])
;
@ -209,7 +186,7 @@ GitStats.prototype.record = function (data, callback) {
}
return self;
};
}
/**
* removeCommit
@ -227,9 +204,9 @@ GitStats.prototype.record = function (data, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.removeCommit = function (data, callback) {
removeCommit (data, callback) {
var self = this;
const self = this;
// Validate data
callback = callback || function (err) { if (err) throw err; };
@ -258,7 +235,7 @@ GitStats.prototype.removeCommit = function (data, callback) {
delete todayObj[data.hash];
});
} else {
var commits = stats.commits
const commits = stats.commits
, day = data.date.format(DATE_FORMAT)
, today = commits[day] = Object(commits[day])
;
@ -284,7 +261,7 @@ GitStats.prototype.removeCommit = function (data, callback) {
}
return self;
};
}
/**
* get
@ -295,8 +272,8 @@ GitStats.prototype.removeCommit = function (data, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.get = function (callback) {
var self = this;
get (callback) {
const self = this;
ReadJson(self.path, function (err, data) {
if (err && err.code === "ENOENT") {
@ -309,7 +286,7 @@ GitStats.prototype.get = function (callback) {
callback(null, data);
});
return self;
};
}
/**
* save
@ -321,10 +298,10 @@ GitStats.prototype.get = function (callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.save = function (stats, callback) {
save (stats, callback) {
WriteJson(this.path, stats, callback);
return this;
};
}
/**
* iterateDays
@ -341,7 +318,7 @@ GitStats.prototype.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` instance.
*/
GitStats.prototype.iterateDays = function (data, callback) {
iterateDays (data, callback) {
if (typeof data === "function") {
callback = data;
@ -353,7 +330,7 @@ GitStats.prototype.iterateDays = function (data, callback) {
data.start = data.start || Moment().subtract(1, "years");
data.format = data.format || DATE_FORMAT;
var start = new Moment(data.start.format(DATE_FORMAT), DATE_FORMAT)
let start = new Moment(data.start.format(DATE_FORMAT), DATE_FORMAT)
, end = new Moment(data.end.format(DATE_FORMAT), DATE_FORMAT)
, tomrrow = Moment(end.format(DATE_FORMAT), DATE_FORMAT).add(1, "days")
, endStr = tomrrow.format(DATE_FORMAT)
@ -367,7 +344,7 @@ GitStats.prototype.iterateDays = function (data, callback) {
}
return this;
};
}
/**
* graph
@ -379,20 +356,20 @@ GitStats.prototype.iterateDays = function (data, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.graph = function (data, callback) {
graph (data, callback) {
if (typeof data === "function") {
callback = data;
data = undefined;
}
var self = this;
const self = this;
// Get commits
self.get(function (err, stats) {
if (err) { return callback(err); }
var cDayObj = null
let cDayObj = null
, year = {}
;
@ -409,7 +386,7 @@ GitStats.prototype.graph = function (data, callback) {
});
return self;
};
}
/**
* calendar
@ -421,14 +398,14 @@ GitStats.prototype.graph = function (data, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.calendar = function (data, callback) {
calendar (data, callback) {
var self = this;
const self = this;
self.graph(data, function (err, graph) {
if (err) { return callback(err); }
var cal = { total: 0, days: {}, cStreak: 0, lStreak: 0, max: 0 }
let cal = { total: 0, days: {}, cStreak: 0, lStreak: 0, max: 0 }
, cDay = null
, days = Object.keys(graph)
, levels = null
@ -465,7 +442,7 @@ GitStats.prototype.calendar = function (data, callback) {
callback(null, cal);
});
return self;
};
}
/**
* ansiCalendar
@ -477,17 +454,17 @@ GitStats.prototype.calendar = function (data, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.ansiCalendar = function (options, callback) {
ansiCalendar (options, callback) {
if (typeof options === "function") {
callback = options;
options = undefined;
}
var self = this;
const self = this;
self.graph(options, function (err, graph) {
var cal = []
let cal = []
, data = {
theme: options.theme
, start: options.start
@ -499,7 +476,7 @@ GitStats.prototype.ansiCalendar = function (options, callback) {
;
self.iterateDays(options, function (cDay) {
var cDayObj = graph[cDay];
const cDayObj = graph[cDay];
if (!cDayObj) { return; }
cal.push([cDay, cDayObj.c]);
});
@ -508,7 +485,7 @@ GitStats.prototype.ansiCalendar = function (options, callback) {
});
return self;
};
}
/**
* authors
@ -525,13 +502,13 @@ GitStats.prototype.ansiCalendar = function (options, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.authors = function (options, callback) {
var repo = new Gry(options.repo);
authors (options, callback) {
const repo = new Gry(options.repo);
repo.exec(['shortlog', '-s', '-n', '--all', '--since', options.start.toString(), '--until', options.end.toString()], function (err, stdout) {
if (err) { return callback(err); }
var lines = stdout.split("\n");
var pieData = stdout.split("\n").map(function (c) {
var splits = c.split("\t").map(function (cc) {
const lines = stdout.split("\n");
const pieData = stdout.split("\n").map(function (c) {
const splits = c.split("\t").map(function (cc) {
return cc.trim();
});
return {
@ -542,7 +519,7 @@ GitStats.prototype.authors = function (options, callback) {
callback(null, pieData);
});
return this;
};
}
/**
* authorsPie
@ -560,7 +537,7 @@ GitStats.prototype.authors = function (options, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.authorsPie = function (options, callback) {
authorsPie (options, callback) {
if (typeof options === "string") {
options = {
@ -576,7 +553,7 @@ GitStats.prototype.authorsPie = function (options, callback) {
return callback(new Error("The repository folder doesn't exist."));
}
var self = this
let self = this
, repo = new Gry(options.repo)
, pie = null
, pieData = []
@ -585,7 +562,7 @@ GitStats.prototype.authorsPie = function (options, callback) {
self.authors(options, function (err, authors) {
if (err) { return callback(err); }
if (authors.length > 50) {
var others = {
let others = {
value: authors.slice(50).reduce(function (a, b) {
return a + b.value;
}, 0)
@ -595,7 +572,7 @@ GitStats.prototype.authorsPie = function (options, callback) {
authors.push(others);
}
var data = {
let data = {
legend: true
, flat: true
, no_ansi: options.no_ansi
@ -606,7 +583,7 @@ GitStats.prototype.authorsPie = function (options, callback) {
});
return self;
};
}
/**
* globalActivity
@ -625,7 +602,7 @@ GitStats.prototype.authorsPie = function (options, callback) {
* @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance.
*/
GitStats.prototype.globalActivity = function (options, callback) {
globalActivity (options, callback) {
if (typeof options === "string") {
options = {
@ -639,12 +616,12 @@ GitStats.prototype.globalActivity = function (options, callback) {
return callback(new Error("The repository folder doesn't exist."));
}
var commits = {}
let commits = {}
, today = null
, cal = []
;
var logArgs = ["log","--since", options.start.format(DATE_FORMAT), "--until", options.end.format(DATE_FORMAT)]
let logArgs = ["log","--since", options.start.format(DATE_FORMAT), "--until", options.end.format(DATE_FORMAT)]
if(options.author){
logArgs = logArgs.concat(["--author", options.author])
}
@ -660,7 +637,7 @@ GitStats.prototype.globalActivity = function (options, callback) {
Object.keys(commits).forEach(function (c) {
cal.push([c, commits[c]])
});
var data = {
let data = {
theme: options.theme
, start: options.start
, end: options.end
@ -671,6 +648,32 @@ GitStats.prototype.globalActivity = function (options, callback) {
});
return this;
}
}
// Defaults
GitStats.CONFIG_PATH = CONFIG_PATH
GitStats.DEFAULT_CONFIG = {
// Dark theme by default
theme: "DARK"
// This defaults in library
, path: undefined
// This defaults in cli-gh-cal
, first_day: undefined
// This defaults to *one year ago*
, since: undefined
// This defaults to *now*
, until: undefined
// Don't show authors by default
, authors: false
// No global activity by default
, global_activity: false
};
module.exports = GitStats;

1842
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -17,7 +17,6 @@
"Fabian Furger <mystyfly@gmail.com>"
],
"license": "MIT",
"devDependencies": {},
"repository": {
"type": "git",
"url": "https://github.com/IonicaBizau/git-stats.git"
@ -42,7 +41,7 @@
"gry": "^6.1.0",
"is-there": "^4.0.0",
"iterate-object": "^1.1.0",
"moment": "^2.9.0",
"moment": "^2.29.3",
"r-json": "^1.0.0",
"tilda": "^4.3.3",
"typpy": "^2.1.0",