git-stats/bin/git-stats

114 lines
3.8 KiB
Plaintext
Raw Normal View History

2015-01-25 20:44:18 +01:00
#!/usr/bin/env node
2015-01-30 14:14:28 +01:00
// Dependencies
var GitStats = new (require("../lib"))()
2015-02-09 10:53:09 +01:00
, Moment = require("moment")
2015-02-01 14:28:22 +01:00
, Logger = require("bug-killer")
, CLP = require("clp")
, Abs = require("abs")
, Package = require("../package")
2015-01-30 14:14:28 +01:00
;
2015-01-25 20:48:59 +01:00
// Configurations
Moment.suppressDeprecationWarnings = true;
2015-02-01 09:46:51 +01:00
// Parse the command line arguments
var recordOpt = new CLP.Option(["record"], "Records a new commit. Don't use this unless you are a mad scientist. If you are a developer, just use this option as part of the module.", "data")
, startDateOpt = new CLP.Option(["s", "start"], "Optional start date", "date")
, endDateOpt = new CLP.Option(["e", "end"], "Optional end date", "date")
2015-05-05 15:56:26 +02:00
, authorsOpt = new CLP.Option(["a", "authors"], "Shows a pie chart with the author related contributions in the current repository.")
, noAnsiOpt = new CLP.Option(["n", "no-ansi"], "Forces the tool not to use ANSI styles.")
, lightOpt = new CLP.Option(["l", "light"], "Enables the light theme.")
, configPathOpt = new CLP.Option(["c", "config"], "Sets a custom config file.", "path")
2015-07-12 14:39:07 +02:00
, globalActivityOpt = new CLP.Option(["g", "global-activity"], "Shows global activity calendar in the current repository.")
, firstDayOpt = new CLP.Option(["d", "first-day"], "Sets the first day of the week.", "day", "Sun")
, parser = new CLP({
name: "Git Stats"
, version: Package.version
, exe: Package.name
, examples: [
2015-05-03 12:47:10 +02:00
"git-stats # Default behavior (stats in the last year)"
, "git-stats -l # Light mode"
, "git-stats -s '1 January 2012' # All the commits from 1 January 2012 to now"
, "git-stats -s '1 January 2012' -s '31 December 2012' # All the commits from 2012"
]
, docs_url: "https://github.com/IonicaBizau/git-stats"
, notes: "Your commit history is kept in the .git-stats, in your $HOME directory (~/)"
, process: true
}, [
startDateOpt
, endDateOpt
, noAnsiOpt
, lightOpt
, recordOpt
, authorsOpt
2015-07-08 10:00:32 +02:00
, globalActivityOpt
, configPathOpt
2015-07-12 14:39:07 +02:00
, firstDayOpt
])
, options = null
;
2015-02-09 10:53:09 +01:00
if (configPathOpt.is_provided) {
GitStats.path = Abs(configPathOpt.value);
}
// --record
if (recordOpt.is_provided) {
2015-02-09 10:53:09 +01:00
try {
options = JSON.parse(recordOpt.value.replace(/^\"|\"$/g, ""));
} catch (e) {
Logger.log(e, "error");
return process.exit(1);
}
2015-02-09 10:53:09 +01:00
return GitStats.record(options, function (err) {
if (err) { return Logger.log(err, "error"); }
process.exit(0);
});
}
// Create the options
options = {
start: startDateOpt.value ? Moment(startDateOpt.value) : Moment().subtract(1, "years")
, end: endDateOpt.value ? Moment(endDateOpt.value) : Moment()
};
// Validate the dates
if (!options.start || !options.start.isValid()) {
options.start = Moment().subtract(1, "years");
Logger.log("Invalid start date. Using default instead (" + options.start.format("LL") + ").", "warn");
}
2015-02-01 12:45:00 +01:00
if (!options.end || !options.end.isValid()) {
options.end = Moment();
Logger.log("Invalid end date. Using default instead (" + options.end.format("LL") + ").", "warn");
2015-01-25 20:48:59 +01:00
}
2015-07-08 10:00:32 +02:00
if (authorsOpt.is_provided || globalActivityOpt.is_provided) {
options.repo = process.cwd();
2015-07-08 10:00:32 +02:00
}
if (authorsOpt.is_provided) {
2015-05-28 18:56:41 +02:00
options.no_ansi = noAnsiOpt.is_provided;
options.radius = (process.stdout.rows / 2) - 4;
2015-05-28 20:51:49 +02:00
} else {
2015-07-12 14:39:07 +02:00
options.firstDay = firstDayOpt.value;
2015-05-28 20:51:49 +02:00
options.theme = noAnsiOpt.is_provided ? null
: lightOpt.is_provided ? "LIGHT": "DARK"
;
}
2015-07-08 10:00:32 +02:00
function display (err, data) {
if (err) { return Logger.log(err, "error"); }
process.stdout.write(data + "\n");
2015-07-08 10:00:32 +02:00
}
if (globalActivityOpt.is_provided) {
return GitStats.globalActivity(options, display);
}
// Show the graphs
GitStats[authorsOpt.is_provided ? "authorsPie" : "ansiCalendar"](options, display);