git-stats/bin/git-stats

179 lines
5.5 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-07-12 16:51:02 +02:00
, Ul = require("ul")
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-07-12 16:51:02 +02:00
, ReadJson = require("r-json")
2015-01-30 14:14:28 +01:00
;
2015-01-25 20:48:59 +01:00
2015-07-12 16:51:02 +02:00
// Constants
const CONFIG_PATH = Abs("~/.git-stats-config.json")
, 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
// This defaults to "DARK" -- this can be a
// string or an object
, theme: undefined
// Don't show authors by default
, authors: false
// No global activity by default
, global_activity: false
}
;
// Configurations
Moment.suppressDeprecationWarnings = true;
2015-07-12 16:51:02 +02:00
GitStats.config = {};
try {
GitStats.config = ReadJson(CONFIG_PATH);
} catch (err) {
if (err.code !== "ENOENT") {
Logger.log("Failed to read the config file: " + err.stack, "warn");
}
}
GitStats.config = Ul.deepMerge(GitStats.config, DEFAULT_CONFIG);
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")
2015-07-12 16:51:02 +02:00
, sinceDateOpt = new CLP.Option(["s", "since"], "Optional start date.", "date", GitStats.config.since)
, untilDateOpt = new CLP.Option(["u", "until"], "Optional end date.", "date", GitStats.config.until)
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.")
2015-07-12 16:51:02 +02:00
, dataPathOpt = new CLP.Option(["d", "data"], "Sets a custom data store file.", "path", GitStats.config.path)
2015-07-12 14:39:07 +02:00
, globalActivityOpt = new CLP.Option(["g", "global-activity"], "Shows global activity calendar in the current repository.")
2015-07-12 16:51:02 +02:00
, firstDayOpt = new CLP.Option(["f", "first-day"], "Sets the first day of the week.", "day", GitStats.config.first_day)
, 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"
2015-07-12 16:51:02 +02:00
, "git-stats -s '1 January 2012' -u '31 December 2012' # All the commits from 2012"
]
, docs_url: "https://github.com/IonicaBizau/git-stats"
2015-07-12 16:51:02 +02:00
, notes: "Your commit history is kept in ~/.git-stats by default. You can create ~/.git-stats-config to specify different defaults."
, process: true
}, [
2015-07-12 16:51:02 +02:00
sinceDateOpt
, untilDateOpt
, noAnsiOpt
, lightOpt
, recordOpt
, authorsOpt
2015-07-08 10:00:32 +02:00
, globalActivityOpt
2015-07-12 16:51:02 +02:00
, dataPathOpt
2015-07-12 14:39:07 +02:00
, firstDayOpt
])
, options = null
;
2015-02-09 10:53:09 +01:00
2015-07-12 16:51:02 +02:00
// Handle data path
if (dataPathOpt.is_provided) {
GitStats.path = Abs(dataPathOpt.value);
GitStats.config.data_path = GitStats.path;
if (!IsThere(GitStats.path)) {
Logger.log("Cannot find the the specified data path file.", "warn");
}
}
2015-07-12 16:52:00 +02:00
if (GitStats.config.authors) {
authorsOpt.is_provided = true;
}
if (GitStats.config.global_activity) {
globalActivityOpt.is_provided = true;
}
// --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 = {
2015-07-12 16:51:02 +02:00
start: sinceDateOpt.value ? Moment(sinceDateOpt.value) : Moment().subtract(1, "years")
, end: untilDateOpt.value ? Moment(untilDateOpt.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
2015-07-12 16:51:02 +02:00
// Handle time range options
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-12 16:51:02 +02:00
// Add the repo path
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
}
2015-07-12 16:51:02 +02:00
// Handle authors
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-07-12 16:51:02 +02:00
// This can be a string or an object
if (GitStats.config.theme) {
options.theme = GitStats.config.theme;
} else {
options.theme = noAnsiOpt.is_provided ? null
: lightOpt.is_provided ? "LIGHT": "DARK"
;
}
}
2015-07-12 16:51:02 +02:00
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);