11#!/usr/bin/env node
2-
3- // Dependencies
4- var GitStatsLib = require ( "../lib " )
5- , GitStats = new GitStatsLib ( )
6- , Ul = require ( "ul" )
7- , Moment = require ( "moment" )
8- , Logger = require ( "bug-killer" )
9- , Clp = require ( "clp" )
10- , Abs = require ( "abs" )
11- , Typpy = require ( "typpy" )
12- , Package = require ( "../package" )
13- , ReadJson = require ( "r-json" )
14- ;
2+ "use strict" ;
3+
4+ const Tilda = require ( "tilda " )
5+ , GitStatsLib = require ( ".." )
6+ , Ul = require ( "ul" )
7+ , Moment = require ( "moment" )
8+ , Logger = require ( "bug-killer" )
9+ , Clp = require ( "clp" )
10+ , Abs = require ( "abs" )
11+ , Typpy = require ( "typpy" )
12+ , Package = require ( "../package" )
13+ , ReadJson = require ( "r-json" )
14+ ;
1515
1616// Constants
17- const CONFIG_PATH = GitStatsLib . CONFIG_PATH
17+ const GitStats = new GitStatsLib ( )
18+ , CONFIG_PATH = GitStatsLib . CONFIG_PATH
1819 , DEFAULT_CONFIG = GitStatsLib . DEFAULT_CONFIG
1920 ;
2021
21- // Configurations
22- Moment . suppressDeprecationWarnings = true ;
23-
2422try {
2523 GitStats . initConfig ( ) ;
2624} catch ( err ) {
@@ -29,136 +27,168 @@ try {
2927 }
3028}
3129
32- // Parse the command line arguments
33- 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" )
34- , sinceDateOpt = new Clp . Option ( [ "s" , "since" ] , "Optional start date." , "date" , GitStats . config . since )
35- , untilDateOpt = new Clp . Option ( [ "u" , "until" ] , "Optional end date." , "date" , GitStats . config . until )
36- , authorsOpt = new Clp . Option ( [ "a" , "authors" ] , "Shows a pie chart with the author related contributions in the current repository." )
37- , noAnsiOpt = new Clp . Option ( [ "n" , "no-ansi" ] , "Forces the tool not to use ANSI styles." )
38- , lightOpt = new Clp . Option ( [ "l" , "light" ] , "Enables the light theme." )
39- , dataPathOpt = new Clp . Option ( [ "d" , "data" ] , "Sets a custom data store file." , "path" , GitStats . config . path )
40- , globalActivityOpt = new Clp . Option ( [ "g" , "global-activity" ] , "Shows global activity calendar in the current repository." )
41- , firstDayOpt = new Clp . Option ( [ "f" , "first-day" ] , "Sets the first day of the week." , "day" , GitStats . config . first_day )
42- , rawOpt = new Clp . Option ( [ "r" , "raw" ] , "Outputs a dump of the raw JSON data." )
43- , parser = new Clp ( {
44- name : "Git Stats"
45- , version : Package . version
46- , exe : Package . name
47- , examples : [
48- "git-stats # Default behavior (stats in the last year)"
49- , "git-stats -l # Light mode"
50- , "git-stats -s '1 January 2012' # All the commits from 1 January 2012 to now"
51- , "git-stats -s '1 January 2012' -u '31 December 2012' # All the commits from 2012"
52- ]
53- , docs_url : Package . homepage
54- , notes : "Your commit history is kept in ~/.git-stats by default. You can create ~/.git-stats-config.json to specify different defaults."
55- , process : true
56- } , [
57- sinceDateOpt
58- , untilDateOpt
59- , noAnsiOpt
60- , lightOpt
61- , authorsOpt
62- , globalActivityOpt
63- , dataPathOpt
64- , firstDayOpt
65- , recordOpt
66- , rawOpt
67- ] )
68- , options = null
69- ;
70-
71- // Handle data path
72- if ( dataPathOpt . is_provided ) {
73- GitStats . path = Abs ( dataPathOpt . value ) ;
74- GitStats . config . data_path = GitStats . path ;
75- if ( ! IsThere ( GitStats . path ) ) {
76- Logger . log ( "Cannot find the the specified data path file." , "warn" ) ;
77- }
78- }
30+ // Configurations
31+ Moment . suppressDeprecationWarnings = true ;
7932
80- if ( GitStats . config . authors ) {
81- authorsOpt . is_provided = true ;
82- }
33+ new Tilda ( `${ __dirname } /../package.json` , {
34+ options : [
35+ {
36+ opts : [ "record" ]
37+ , desc : "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."
38+ , name : "data"
39+ }
40+ , {
41+ opts : [ "s" , "since" ]
42+ , desc : "Optional start date."
43+ , name : "date"
44+ , default : GitStats . config . since
45+ }
46+ , {
47+ opts : [ "u" , "until" ]
48+ , desc : "Optional end date."
49+ , name : "date"
50+ , default : GitStats . config . until
51+ }
52+ , {
53+ opts : [ "a" , "authors" ]
54+ , desc : "Shows a pie chart with the author related contributions in the current repository."
55+ }
56+ , {
57+ opts : [ "n" , "no-ansi" ]
58+ , desc : "Forces the tool not to use ANSI styles."
59+ }
60+ , {
61+ opts : [ "l" , "light" ]
62+ , desc : "Enables the light theme."
63+ }
64+ , {
65+ opts : [ "d" , "data" ]
66+ , desc : "Sets a custom data store file."
67+ , name : "path"
68+ , default : GitStats . config . path
69+ }
70+ , {
71+ opts : [ "g" , "global-activity" ]
72+ , desc : "Shows global activity calendar in the current repository."
73+ }
74+ , {
75+ opts : [ "r" , "raw" ]
76+ , desc : "Outputs a dump of the raw JSON data."
77+ }
78+ ]
79+ , examples : [
80+ "git-stats # Default behavior (stats in the last year)"
81+ , "git-stats -l # Light mode"
82+ , "git-stats -s '1 January 2012' # All the commits from 1 January 2012 to now"
83+ , "git-stats -s '1 January 2012' -u '31 December 2012' # All the commits from 2012"
84+ ]
85+ , notes : "Your commit history is kept in ~/.git-stats by default. You can create ~/.git-stats-config.json to specify different defaults."
86+ } ) . main ( action => {
87+ debugger
88+
89+ let recordOpt = action . options . record
90+ , sinceDateOpt = action . options . since
91+ , untilDateOpt = action . options . until
92+ , authorsOpt = action . options . authors
93+ , noAnsiOpt = action . options . noAnsi
94+ , lightOpt = action . options . light
95+ , dataPathOpt = action . options . data
96+ , globalActivityOpt = action . options . globalActivity
97+ , rawOpt = action . options . raw
98+ ;
99+
100+ let options = { } ;
101+
102+ // Handle data path
103+ if ( dataPathOpt . is_provided ) {
104+ GitStats . path = Abs ( dataPathOpt . value ) ;
105+ GitStats . config . data_path = GitStats . path ;
106+ if ( ! IsThere ( GitStats . path ) ) {
107+ Logger . log ( "Cannot find the the specified data path file." , "warn" ) ;
108+ }
109+ }
83110
84- if ( GitStats . config . global_activity ) {
85- globalActivityOpt . is_provided = true ;
86- }
111+ if ( GitStats . config . authors ) {
112+ authorsOpt . is_provided = true ;
113+ }
87114
88- // --record
89- if ( recordOpt . is_provided ) {
90- try {
91- options = JSON . parse ( recordOpt . value . replace ( / ^ \" | \" $ / g, "" ) ) ;
92- } catch ( e ) {
93- Logger . log ( e , "error" ) ;
94- return process . exit ( 1 ) ;
115+ if ( GitStats . config . global_activity ) {
116+ globalActivityOpt . is_provided = true ;
95117 }
96118
97- return GitStats . record ( options , function ( err ) {
98- if ( err ) { return Logger . log ( err , "error" ) ; }
99- process . exit ( 0 ) ;
100- } ) ;
101- }
119+ // --record
120+ if ( recordOpt . is_provided ) {
121+ try {
122+ options = JSON . parse ( recordOpt . value . replace ( / ^ \" | \" $ / g, "" ) ) ;
123+ } catch ( e ) {
124+ Logger . log ( e , "error" ) ;
125+ return process . exit ( 1 ) ;
126+ }
102127
103- // Create the options
104- options = {
105- start : sinceDateOpt . value ? Moment ( sinceDateOpt . value ) : Moment ( ) . subtract ( 1 , "years" )
106- , end : untilDateOpt . value ? Moment ( untilDateOpt . value ) : Moment ( )
107- , raw : rawOpt . is_provided
108- } ;
109-
110- // Validate the dates
111- if ( ! options . start || ! options . start . isValid ( ) ) {
112- options . start = Moment ( ) . subtract ( 1 , "years" ) ;
113- Logger . log ( "Invalid start date. Using default instead (" + options . start . format ( "LL" ) + ")." , "warn" ) ;
114- }
128+ return GitStats . record ( options , function ( err ) {
129+ if ( err ) { return Logger . log ( err , "error" ) ; }
130+ process . exit ( 0 ) ;
131+ } ) ;
132+ }
115133
116- // Handle time range options
117- if ( ! options . end || ! options . end . isValid ( ) ) {
118- options . end = Moment ( ) ;
119- Logger . log ( "Invalid end date. Using default instead (" + options . end . format ( "LL" ) + ")." , "warn" ) ;
120- }
134+ // Create the options
135+ options = {
136+ start : sinceDateOpt . value ? Moment ( sinceDateOpt . value ) : Moment ( ) . subtract ( 1 , "years" )
137+ , end : untilDateOpt . value ? Moment ( untilDateOpt . value ) : Moment ( )
138+ , raw : rawOpt . is_provided
139+ } ;
140+
141+ // Validate the dates
142+ if ( ! options . start || ! options . start . isValid ( ) ) {
143+ options . start = Moment ( ) . subtract ( 1 , "years" ) ;
144+ Logger . log ( "Invalid start date. Using default instead (" + options . start . format ( "LL" ) + ")." , "warn" ) ;
145+ }
121146
122- // Add the repo path
123- if ( authorsOpt . is_provided || globalActivityOpt . is_provided ) {
124- options . repo = process . cwd ( ) ;
125- }
147+ // Handle time range options
148+ if ( ! options . end || ! options . end . isValid ( ) ) {
149+ options . end = Moment ( ) ;
150+ Logger . log ( "Invalid end date. Using default instead (" + options . end . format ( "LL" ) + ")." , "warn" ) ;
151+ }
126152
127- // Handle authors
128- if ( authorsOpt . is_provided ) {
129- options . no_ansi = noAnsiOpt . is_provided ;
130- options . radius = ( process . stdout . rows / 2 ) - 4 ;
131- }
153+ // Add the repo path
154+ if ( authorsOpt . is_provided || globalActivityOpt . is_provided ) {
155+ options . repo = process . cwd ( ) ;
156+ }
157+
158+ // Handle authors
159+ if ( authorsOpt . is_provided ) {
160+ options . no_ansi = noAnsiOpt . is_provided ;
161+ options . radius = ( process . stdout . rows / 2 ) - 4 ;
162+ }
132163
133- if ( ! authorsOpt . is_provided || globalActivityOpt . is_provided ) {
134- options . firstDay = firstDayOpt . value ;
135- // This can be a string or an object
136- if ( / ^ o b j e c t | s t r i n g $ / . test ( Typpy ( GitStats . config . theme ) ) && ! noAnsiOpt . is_provided && ! lightOpt . is_provided ) {
137- options . theme = GitStats . config . theme ;
138- if ( typeof GitStats . config . theme === "string" ) {
139- if ( ! / ^ D A R K | L I G H T $ / . test ( options . theme ) ) {
140- options . theme = null ;
164+ if ( ! authorsOpt . is_provided || globalActivityOpt . is_provided ) {
165+ // This can be a string or an object
166+ if ( / ^ o b j e c t | s t r i n g $ / . test ( Typpy ( GitStats . config . theme ) ) && ! noAnsiOpt . is_provided && ! lightOpt . is_provided ) {
167+ options . theme = GitStats . config . theme ;
168+ if ( typeof GitStats . config . theme === "string" ) {
169+ if ( ! / ^ D A R K | L I G H T $ / . test ( options . theme ) ) {
170+ options . theme = null ;
171+ }
141172 }
173+ } else {
174+ options . theme = noAnsiOpt . is_provided ? null
175+ : lightOpt . is_provided ? "LIGHT" : "DARK"
176+ ;
142177 }
143- } else {
144- options . theme = noAnsiOpt . is_provided ? null
145- : lightOpt . is_provided ? "LIGHT" : "DARK"
146- ;
147178 }
148- }
149179
150- function display ( err , data ) {
151- if ( err ) { return Logger . log ( err , "error" ) ; }
152- if ( typeof data !== "string" ) {
153- data = JSON . stringify ( data ) ;
180+ function display ( err , data ) {
181+ if ( err ) { return Logger . log ( err , "error" ) ; }
182+ if ( typeof data !== "string" ) {
183+ data = JSON . stringify ( data ) ;
184+ }
185+ process . stdout . write ( data + "\n" ) ;
154186 }
155- process . stdout . write ( data + "\n" ) ;
156- }
157-
158- if ( globalActivityOpt . is_provided ) {
159- return GitStats . globalActivity ( options , display ) ;
160- }
161187
188+ if ( globalActivityOpt . is_provided ) {
189+ return GitStats . globalActivity ( options , display ) ;
190+ }
162191
163- // Show the graphs
164- GitStats [ authorsOpt . is_provided ? "authorsPie" : "ansiCalendar" ] ( options , display ) ;
192+ // Show the graphs
193+ GitStats [ authorsOpt . is_provided ? "authorsPie" : "ansiCalendar" ] ( options , display ) ;
194+ } ) ;
0 commit comments