mirror of
https://github.com/IonicaBizau/git-stats.git
synced 2025-01-03 10:22:11 +01:00
Use the CLP library to parse the arguments. Fixed #43.
This commit is contained in:
parent
c941e03f6b
commit
25e77fb1b0
2 changed files with 69 additions and 75 deletions
|
@ -1,28 +0,0 @@
|
||||||
git-stats --help
|
|
||||||
A GitHub-like contributions calendar, but locally, with all your git commits.
|
|
||||||
|
|
||||||
usage: git-stats [start] [end] [options] [data]
|
|
||||||
|
|
||||||
start: Optional start date
|
|
||||||
end: Optional end date
|
|
||||||
|
|
||||||
options:
|
|
||||||
-v Displays version information.
|
|
||||||
-h --help Displays this help.
|
|
||||||
--no-ansi Doesn't use ANSI colors in the squares.
|
|
||||||
--record <data> 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.
|
|
||||||
--light Enable the light theme.
|
|
||||||
|
|
||||||
examples:
|
|
||||||
git-stats # Displays your commit calendar
|
|
||||||
git-stats -v
|
|
||||||
git-stats -h
|
|
||||||
git-stats --light # Light mode
|
|
||||||
git-stats '1 January 2012' # All the commits from 1 January 2012, to now
|
|
||||||
git-stats '1 January 2012' '31 December 2012' # All the commits from 2012
|
|
||||||
|
|
||||||
Your commit history is kept in the .git-stats, in your $HOME directory (~/)
|
|
||||||
|
|
||||||
Documentation can be found at https://github.com/IonicaBizau/git-stats
|
|
116
bin/git-stats
116
bin/git-stats
|
@ -7,6 +7,8 @@ var GitStats = require("../lib")
|
||||||
, AnsiParser = require("ansi-parser")
|
, AnsiParser = require("ansi-parser")
|
||||||
, Logger = require("bug-killer")
|
, Logger = require("bug-killer")
|
||||||
, GitStatsColors = require("git-stats-colors")
|
, GitStatsColors = require("git-stats-colors")
|
||||||
|
, CLP = require("clp")
|
||||||
|
, Package = require("../package")
|
||||||
;
|
;
|
||||||
|
|
||||||
// Configurations
|
// Configurations
|
||||||
|
@ -14,55 +16,75 @@ Logger.config.displayDate = false;
|
||||||
Logger.config.logLevel = 4;
|
Logger.config.logLevel = 4;
|
||||||
Moment.suppressDeprecationWarnings = true;
|
Moment.suppressDeprecationWarnings = true;
|
||||||
|
|
||||||
// CLI options
|
// Parse the command line arguments
|
||||||
switch (process.argv[2]) {
|
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")
|
||||||
case "--record":
|
, startDateOpt = new CLP.Option(["s", "start"], "Optional start date", "date")
|
||||||
var data = process.argv[3].replace(/^\"|\"$/g, "");
|
, endDateOpt = new CLP.Option(["e", "end"], "Optional end date", "date")
|
||||||
try {
|
, noAnsiOpt = new CLP.Option(["n", "no-ansi"], "Forces the tool not to use ANSI styles.")
|
||||||
data = JSON.parse(data);
|
, lightOpt = new CLP.Option(["l", "light"], "Enables the light theme.")
|
||||||
} catch (e) {
|
, parser = new CLP({
|
||||||
throw e;
|
name: "Git Stats"
|
||||||
}
|
, version: Package.version
|
||||||
GitStats.record(data, function (err) {
|
, exe: Package.name
|
||||||
if (err) { return Logger.log(err, "error"); }
|
, examples: [
|
||||||
});
|
"git-stats # Displays your commit calendar"
|
||||||
break;
|
, "git-stats --light # Light mode"
|
||||||
case "--help":
|
, "git-stats '1 January 2012' # All the commits from 1 January 2012, to now"
|
||||||
console.log(Fs.readFileSync(__dirname + "/docs/help", "utf-8"));
|
, "git-stats '1 January 2012' '31 December 2012' # All the commits from 2012"
|
||||||
break;
|
]
|
||||||
case "-v":
|
, docs_url: "https://github.com/IonicaBizau/git-stats"
|
||||||
console.log(require("../package.json").version);
|
, notes: "Your commit history is kept in the .git-stats, in your $HOME directory (~/)"
|
||||||
break;
|
, process: true
|
||||||
default:
|
}, [
|
||||||
var options = {};
|
startDateOpt
|
||||||
|
, endDateOpt
|
||||||
|
, noAnsiOpt
|
||||||
|
, lightOpt
|
||||||
|
, recordOpt
|
||||||
|
])
|
||||||
|
, options = null
|
||||||
|
;
|
||||||
|
|
||||||
if (process.argv.length !== 2) {
|
|
||||||
if (process.argv[2]) {
|
|
||||||
options.start = Moment(process.argv[2]);
|
|
||||||
|
|
||||||
}
|
// --record
|
||||||
|
if (recordOpt.is_provided) {
|
||||||
|
|
||||||
if (process.argv[3]) {
|
try {
|
||||||
options.end = Moment(process.argv[3]);
|
options = JSON.parse(recordOpt.value.replace(/^\"|\"$/g, ""));
|
||||||
}
|
} catch (e) {
|
||||||
}
|
Logger.log(e, "error");
|
||||||
|
return process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
if (!options.start || !options.start.isValid()) {
|
return GitStats.record(options, function (err) {
|
||||||
options.start = Moment().subtract(1, "years");
|
if (err) { return Logger.log(err, "error"); }
|
||||||
}
|
process.exit(0);
|
||||||
|
});
|
||||||
if (!options.end || !options.end.isValid()) {
|
|
||||||
options.end = Moment();
|
|
||||||
}
|
|
||||||
|
|
||||||
GitStats.ansiCalendar(options, function (err, data) {
|
|
||||||
if (err) { return Logger.log(err, "error"); }
|
|
||||||
data = AnsiParser.removeAnsi(data);
|
|
||||||
if (process.argv.indexOf("--no-ansi") === -1) {
|
|
||||||
data = GitStatsColors(data, process.argv.indexOf("--light") !== -1 ? "LIGHT": "DARK");
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(err || data);
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the ANSI calendar
|
||||||
|
GitStats.ansiCalendar(options, function (err, data) {
|
||||||
|
if (err) { return Logger.log(err, "error"); }
|
||||||
|
data = AnsiParser.removeAnsi(data);
|
||||||
|
if (!noAnsiOpt.is_provided) {
|
||||||
|
data = GitStatsColors(data, lightOpt.is_provided ? "LIGHT": "DARK");
|
||||||
|
}
|
||||||
|
process.stdout.write(data + "\n");
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue