git-stats/bin/git-stats

161 lines
5.3 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
2015-08-03 06:40:00 +02:00
var GitStatsLib = require("../lib")
, GitStats = new GitStatsLib()
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")
2015-09-13 16:36:32 +02:00
, Clp = require("clp")
, Abs = require("abs")
2015-07-12 17:21:55 +02:00
, Typpy = require("typpy")
, 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
2015-08-03 06:40:00 +02:00
const CONFIG_PATH = GitStatsLib.CONFIG_PATH
, DEFAULT_CONFIG = GitStatsLib.DEFAULT_CONFIG
2015-07-12 16:51:02 +02:00
;
// Configurations
Moment.suppressDeprecationWarnings = true;
2015-09-14 16:29:40 +02:00
2015-07-12 16:51:02 +02:00
try {
2015-09-14 16:29:40 +02:00
GitStats.initConfig();
2015-07-12 16:51:02 +02:00
} catch (err) {
if (err.code !== "ENOENT") {
2015-09-14 16:29:40 +02:00
Logger.log("Failed to read the config file:\n" + err.stack, "warn");
2015-07-12 16:51:02 +02:00
}
}
// Parse the command line arguments
2015-09-13 16:36:32 +02:00
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")
, 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)
, 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.")
, dataPathOpt = new Clp.Option(["d", "data"], "Sets a custom data store file.", "path", GitStats.config.path)
, globalActivityOpt = new Clp.Option(["g", "global-activity"], "Shows global activity calendar in the current repository.")
, firstDayOpt = new Clp.Option(["f", "first-day"], "Sets the first day of the week.", "day", GitStats.config.first_day)
2015-10-11 11:15:15 +02:00
, rawOpt = new Clp.Option(["r", "raw"], "Outputs a dump of the raw JSON data.")
2015-09-13 16:36:32 +02:00
, 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"
]
2015-08-03 06:40:43 +02:00
, docs_url: Package.homepage
2015-09-21 20:07:58 +02:00
, notes: "Your commit history is kept in ~/.git-stats by default. You can create ~/.git-stats-config.json to specify different defaults."
, process: true
}, [
2015-07-12 16:51:02 +02:00
sinceDateOpt
, untilDateOpt
, noAnsiOpt
, lightOpt
, 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
, recordOpt
])
, 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) {
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()
2015-10-11 11:15:15 +02:00
, raw: rawOpt.is_provided
};
// 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-07-12 17:21:55 +02:00
}
if (!authorsOpt.is_provided || globalActivityOpt.is_provided) {
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
2015-09-21 19:59:18 +02:00
if (/^object|string$/.test(Typpy(GitStats.config.theme)) && !noAnsiOpt.is_provided && !lightOpt.is_provided) {
2015-07-12 16:51:02 +02:00
options.theme = GitStats.config.theme;
2015-07-13 07:23:24 +02:00
if (typeof GitStats.config.theme === "string") {
if (!/^DARK|LIGHT$/.test(options.theme)) {
options.theme = null;
}
2015-07-12 17:21:55 +02:00
}
2015-07-12 16:51:02 +02:00
} else {
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);
}
2015-10-11 11:15:15 +02:00
2015-07-08 10:00:32 +02:00
// Show the graphs
GitStats[authorsOpt.is_provided ? "authorsPie" : "ansiCalendar"](options, display);