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"; "use strict";
var Ul = require("ul") const Ul = require("ul")
, Abs = require("abs") , Abs = require("abs")
, ReadJson = require("r-json") , ReadJson = require("r-json")
, WriteJson = require("w-json") , WriteJson = require("w-json")
@ -26,7 +26,9 @@ const DATE_FORMAT = "MMM D, YYYY"
, CONFIG_PATH = Abs("~/.git-stats-config.js") , CONFIG_PATH = Abs("~/.git-stats-config.js")
; ;
/**
class GitStats {
/**
* GitStats * GitStats
* *
* @name GitStats * @name GitStats
@ -34,37 +36,12 @@ const DATE_FORMAT = "MMM D, YYYY"
* @param {String} dataPath Path to the data file. * @param {String} dataPath Path to the data file.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
function GitStats(dataPath) { constructor (dataPath) {
this.path = Abs(Deffy(dataPath, DEFAULT_STORE)); this.path = Abs(Deffy(dataPath, DEFAULT_STORE));
this.config = {}; 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 * getConfig
* Fetches the configuration object from file (`~/.git-stats-config.js`). * Fetches the configuration object from file (`~/.git-stats-config.js`).
* *
@ -73,8 +50,8 @@ GitStats.DEFAULT_CONFIG = {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {Object|Undefined} If no callback is provided, the configuration object will be returned. * @return {Object|Undefined} If no callback is provided, the configuration object will be returned.
*/ */
GitStats.prototype.getConfig = function (callback) { getConfig (callback) {
var data = {} let data = {}
, err = null , err = null
; ;
@ -96,9 +73,9 @@ GitStats.prototype.getConfig = function (callback) {
} }
return data; return data;
}; }
/** /**
* initConfig * initConfig
* Inits the configuration field (`this.config`). * Inits the configuration field (`this.config`).
* *
@ -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 {Object|String} input The path to a custom git-stats configuration file or the configuration object.
* @param {Function} callback The callback function. * @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)) { if (Typpy(input, Function)) {
callback = input; callback = input;
@ -133,9 +110,9 @@ GitStats.prototype.initConfig = function (input, callback) {
} else { } else {
this.initConfig(this.getConfig()); this.initConfig(this.getConfig());
} }
}; }
/** /**
* record * record
* Records a new commit. * Records a new commit.
* *
@ -152,9 +129,9 @@ GitStats.prototype.initConfig = function (input, callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.record = function (data, callback) { record (data, callback) {
var self = this; const self = this;
// Validate data // Validate data
callback = callback || function (err) { if (err) throw err; }; callback = callback || function (err) { if (err) throw err; };
@ -184,7 +161,7 @@ GitStats.prototype.record = function (data, callback) {
function modify (err, stats) { function modify (err, stats) {
var commits = stats.commits const commits = stats.commits
, day = data.date.format(DATE_FORMAT) , day = data.date.format(DATE_FORMAT)
, today = commits[day] = Object(commits[day]) , today = commits[day] = Object(commits[day])
; ;
@ -209,9 +186,9 @@ GitStats.prototype.record = function (data, callback) {
} }
return self; return self;
}; }
/** /**
* removeCommit * removeCommit
* Deletes a specifc commit from the history. * Deletes a specifc commit from the history.
* *
@ -227,9 +204,9 @@ GitStats.prototype.record = function (data, callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.removeCommit = function (data, callback) { removeCommit (data, callback) {
var self = this; const self = this;
// Validate data // Validate data
callback = callback || function (err) { if (err) throw err; }; callback = callback || function (err) { if (err) throw err; };
@ -258,7 +235,7 @@ GitStats.prototype.removeCommit = function (data, callback) {
delete todayObj[data.hash]; delete todayObj[data.hash];
}); });
} else { } else {
var commits = stats.commits const commits = stats.commits
, day = data.date.format(DATE_FORMAT) , day = data.date.format(DATE_FORMAT)
, today = commits[day] = Object(commits[day]) , today = commits[day] = Object(commits[day])
; ;
@ -284,9 +261,9 @@ GitStats.prototype.removeCommit = function (data, callback) {
} }
return self; return self;
}; }
/** /**
* get * get
* Gets the git stats. * Gets the git stats.
* *
@ -295,8 +272,8 @@ GitStats.prototype.removeCommit = function (data, callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.get = function (callback) { get (callback) {
var self = this; const self = this;
ReadJson(self.path, function (err, data) { ReadJson(self.path, function (err, data) {
if (err && err.code === "ENOENT") { if (err && err.code === "ENOENT") {
@ -309,9 +286,9 @@ GitStats.prototype.get = function (callback) {
callback(null, data); callback(null, data);
}); });
return self; return self;
}; }
/** /**
* save * save
* Saves the provided stats. * Saves the provided stats.
* *
@ -321,12 +298,12 @@ GitStats.prototype.get = function (callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.save = function (stats, callback) { save (stats, callback) {
WriteJson(this.path, stats, callback); WriteJson(this.path, stats, callback);
return this; return this;
}; }
/** /**
* iterateDays * iterateDays
* Iterate through the days, calling the callback function on each day. * Iterate through the days, calling the callback function on each day.
* *
@ -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. * @param {Function} callback The callback function called with the current day formatted (type: string) and the `Moment` date object.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.iterateDays = function (data, callback) { iterateDays (data, callback) {
if (typeof data === "function") { if (typeof data === "function") {
callback = data; callback = data;
@ -353,7 +330,7 @@ GitStats.prototype.iterateDays = function (data, callback) {
data.start = data.start || Moment().subtract(1, "years"); data.start = data.start || Moment().subtract(1, "years");
data.format = data.format || DATE_FORMAT; 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) , end = new Moment(data.end.format(DATE_FORMAT), DATE_FORMAT)
, tomrrow = Moment(end.format(DATE_FORMAT), DATE_FORMAT).add(1, "days") , tomrrow = Moment(end.format(DATE_FORMAT), DATE_FORMAT).add(1, "days")
, endStr = tomrrow.format(DATE_FORMAT) , endStr = tomrrow.format(DATE_FORMAT)
@ -367,9 +344,9 @@ GitStats.prototype.iterateDays = function (data, callback) {
} }
return this; return this;
}; }
/** /**
* graph * graph
* Creates an object with the stats on the provided period (default: *last year*). * Creates an object with the stats on the provided period (default: *last year*).
* *
@ -379,20 +356,20 @@ GitStats.prototype.iterateDays = function (data, callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.graph = function (data, callback) { graph (data, callback) {
if (typeof data === "function") { if (typeof data === "function") {
callback = data; callback = data;
data = undefined; data = undefined;
} }
var self = this; const self = this;
// Get commits // Get commits
self.get(function (err, stats) { self.get(function (err, stats) {
if (err) { return callback(err); } if (err) { return callback(err); }
var cDayObj = null let cDayObj = null
, year = {} , year = {}
; ;
@ -409,9 +386,9 @@ GitStats.prototype.graph = function (data, callback) {
}); });
return self; return self;
}; }
/** /**
* calendar * calendar
* Creates the calendar data for the provided period (default: *last year*). * Creates the calendar data for the provided period (default: *last year*).
* *
@ -421,14 +398,14 @@ GitStats.prototype.graph = function (data, callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @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) { self.graph(data, function (err, graph) {
if (err) { return callback(err); } 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 , cDay = null
, days = Object.keys(graph) , days = Object.keys(graph)
, levels = null , levels = null
@ -465,9 +442,9 @@ GitStats.prototype.calendar = function (data, callback) {
callback(null, cal); callback(null, cal);
}); });
return self; return self;
}; }
/** /**
* ansiCalendar * ansiCalendar
* Creates the ANSI contributions calendar. * Creates the ANSI contributions calendar.
* *
@ -477,17 +454,17 @@ GitStats.prototype.calendar = function (data, callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.ansiCalendar = function (options, callback) { ansiCalendar (options, callback) {
if (typeof options === "function") { if (typeof options === "function") {
callback = options; callback = options;
options = undefined; options = undefined;
} }
var self = this; const self = this;
self.graph(options, function (err, graph) { self.graph(options, function (err, graph) {
var cal = [] let cal = []
, data = { , data = {
theme: options.theme theme: options.theme
, start: options.start , start: options.start
@ -499,7 +476,7 @@ GitStats.prototype.ansiCalendar = function (options, callback) {
; ;
self.iterateDays(options, function (cDay) { self.iterateDays(options, function (cDay) {
var cDayObj = graph[cDay]; const cDayObj = graph[cDay];
if (!cDayObj) { return; } if (!cDayObj) { return; }
cal.push([cDay, cDayObj.c]); cal.push([cDay, cDayObj.c]);
}); });
@ -508,9 +485,9 @@ GitStats.prototype.ansiCalendar = function (options, callback) {
}); });
return self; return self;
}; }
/** /**
* authors * authors
* Creates an array with the authors of a git repository. * Creates an array with the authors of a git repository.
* *
@ -525,13 +502,13 @@ GitStats.prototype.ansiCalendar = function (options, callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.authors = function (options, callback) { authors (options, callback) {
var repo = new Gry(options.repo); const repo = new Gry(options.repo);
repo.exec(['shortlog', '-s', '-n', '--all', '--since', options.start.toString(), '--until', options.end.toString()], function (err, stdout) { repo.exec(['shortlog', '-s', '-n', '--all', '--since', options.start.toString(), '--until', options.end.toString()], function (err, stdout) {
if (err) { return callback(err); } if (err) { return callback(err); }
var lines = stdout.split("\n"); const lines = stdout.split("\n");
var pieData = stdout.split("\n").map(function (c) { const pieData = stdout.split("\n").map(function (c) {
var splits = c.split("\t").map(function (cc) { const splits = c.split("\t").map(function (cc) {
return cc.trim(); return cc.trim();
}); });
return { return {
@ -542,9 +519,9 @@ GitStats.prototype.authors = function (options, callback) {
callback(null, pieData); callback(null, pieData);
}); });
return this; return this;
}; }
/** /**
* authorsPie * authorsPie
* Creates the authors pie. * Creates the authors pie.
* *
@ -560,7 +537,7 @@ GitStats.prototype.authors = function (options, callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.authorsPie = function (options, callback) { authorsPie (options, callback) {
if (typeof options === "string") { if (typeof options === "string") {
options = { options = {
@ -576,7 +553,7 @@ GitStats.prototype.authorsPie = function (options, callback) {
return callback(new Error("The repository folder doesn't exist.")); return callback(new Error("The repository folder doesn't exist."));
} }
var self = this let self = this
, repo = new Gry(options.repo) , repo = new Gry(options.repo)
, pie = null , pie = null
, pieData = [] , pieData = []
@ -585,7 +562,7 @@ GitStats.prototype.authorsPie = function (options, callback) {
self.authors(options, function (err, authors) { self.authors(options, function (err, authors) {
if (err) { return callback(err); } if (err) { return callback(err); }
if (authors.length > 50) { if (authors.length > 50) {
var others = { let others = {
value: authors.slice(50).reduce(function (a, b) { value: authors.slice(50).reduce(function (a, b) {
return a + b.value; return a + b.value;
}, 0) }, 0)
@ -595,7 +572,7 @@ GitStats.prototype.authorsPie = function (options, callback) {
authors.push(others); authors.push(others);
} }
var data = { let data = {
legend: true legend: true
, flat: true , flat: true
, no_ansi: options.no_ansi , no_ansi: options.no_ansi
@ -606,9 +583,9 @@ GitStats.prototype.authorsPie = function (options, callback) {
}); });
return self; return self;
}; }
/** /**
* globalActivity * globalActivity
* Creates the global contributions calendar (all commits made by all committers). * Creates the global contributions calendar (all commits made by all committers).
* *
@ -625,7 +602,7 @@ GitStats.prototype.authorsPie = function (options, callback) {
* @param {Function} callback The callback function. * @param {Function} callback The callback function.
* @return {GitStats} The `GitStats` instance. * @return {GitStats} The `GitStats` instance.
*/ */
GitStats.prototype.globalActivity = function (options, callback) { globalActivity (options, callback) {
if (typeof options === "string") { if (typeof options === "string") {
options = { options = {
@ -639,12 +616,12 @@ GitStats.prototype.globalActivity = function (options, callback) {
return callback(new Error("The repository folder doesn't exist.")); return callback(new Error("The repository folder doesn't exist."));
} }
var commits = {} let commits = {}
, today = null , today = null
, cal = [] , 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){ if(options.author){
logArgs = logArgs.concat(["--author", options.author]) logArgs = logArgs.concat(["--author", options.author])
} }
@ -660,7 +637,7 @@ GitStats.prototype.globalActivity = function (options, callback) {
Object.keys(commits).forEach(function (c) { Object.keys(commits).forEach(function (c) {
cal.push([c, commits[c]]) cal.push([c, commits[c]])
}); });
var data = { let data = {
theme: options.theme theme: options.theme
, start: options.start , start: options.start
, end: options.end , end: options.end
@ -671,6 +648,32 @@ GitStats.prototype.globalActivity = function (options, callback) {
}); });
return this; 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; 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>" "Fabian Furger <mystyfly@gmail.com>"
], ],
"license": "MIT", "license": "MIT",
"devDependencies": {},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/IonicaBizau/git-stats.git" "url": "https://github.com/IonicaBizau/git-stats.git"
@ -42,7 +41,7 @@
"gry": "^6.1.0", "gry": "^6.1.0",
"is-there": "^4.0.0", "is-there": "^4.0.0",
"iterate-object": "^1.1.0", "iterate-object": "^1.1.0",
"moment": "^2.9.0", "moment": "^2.29.3",
"r-json": "^1.0.0", "r-json": "^1.0.0",
"tilda": "^4.3.3", "tilda": "^4.3.3",
"typpy": "^2.1.0", "typpy": "^2.1.0",