git-stats/bin/git-stats

99 lines
3.3 KiB
Text
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 = require("../lib")
2015-02-09 10:53:09 +01:00
, Moment = require("moment")
2015-01-30 14:14:28 +01:00
, Fs = require("fs")
2015-02-01 09:46:51 +01:00
, AnsiParser = require("ansi-parser")
2015-02-01 14:28:22 +01:00
, Logger = require("bug-killer")
2015-02-09 13:19:16 +01:00
, GitStatsColors = require("git-stats-colors")
, CLP = require("clp")
, Package = require("../package")
2015-01-30 14:14:28 +01:00
;
2015-01-25 20:48:59 +01:00
// Configurations
2015-02-01 14:28:22 +01:00
Logger.config.displayDate = false;
Logger.config.logLevel = 4;
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.")
, 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
])
, options = null
;
2015-02-09 10:53:09 +01:00
// --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
}
if (authorsOpt.is_provided) {
options.repo = process.cwd();
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 {
options.theme = noAnsiOpt.is_provided ? null
: lightOpt.is_provided ? "LIGHT": "DARK"
;
}
2015-05-05 15:56:26 +02:00
// Show the graphs
GitStats[authorsOpt.is_provided ? "authorsPie" : "ansiCalendar"](options, function (err, data) {
if (err) { return Logger.log(err, "error"); }
process.stdout.write(data + "\n");
});