git-stats/bin/git-stats
2015-05-28 21:51:49 +03:00

98 lines
3.3 KiB
JavaScript
Executable file

#!/usr/bin/env node
// Dependencies
var GitStats = require("../lib")
, Moment = require("moment")
, Fs = require("fs")
, AnsiParser = require("ansi-parser")
, Logger = require("bug-killer")
, GitStatsColors = require("git-stats-colors")
, CLP = require("clp")
, Package = require("../package")
;
// Configurations
Logger.config.displayDate = false;
Logger.config.logLevel = 4;
Moment.suppressDeprecationWarnings = true;
// 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")
, 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: [
"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
;
// --record
if (recordOpt.is_provided) {
try {
options = JSON.parse(recordOpt.value.replace(/^\"|\"$/g, ""));
} catch (e) {
Logger.log(e, "error");
return process.exit(1);
}
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");
}
if (!options.end || !options.end.isValid()) {
options.end = Moment();
Logger.log("Invalid end date. Using default instead (" + options.end.format("LL") + ").", "warn");
}
if (authorsOpt.is_provided) {
options.repo = process.cwd();
options.no_ansi = noAnsiOpt.is_provided;
options.radius = (process.stdout.rows / 2) - 4;
} else {
options.theme = noAnsiOpt.is_provided ? null
: lightOpt.is_provided ? "LIGHT": "DARK"
;
}
// 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");
});