11#!/usr/bin/env node
22const chalk = require ( 'chalk' ) ;
3- const Table = require ( 'cli-table' ) ;
43const getJSON = require ( 'get-json' )
54const opn = require ( 'opn' ) ;
65const ora = require ( 'ora' )
76
7+ const jsonLink = "https://raw.githubusercontent.com/excalith/Git-Cheats/master/assets/commands.json" ;
88let cmd = process . argv [ 2 ] ;
9+ let bar = chalk . green ( "║ " ) ;
910
10-
11+ // Check Arguments
1112if ( cmd == "-h" || cmd == "--help" ) {
1213 ShowHelp ( ) ;
1314}
1415else if ( cmd == "-l" || cmd == "--launch" ) {
15- opn ( "www.gitcheats.com?c=" + process . argv [ 3 ] )
16+ LaunchBrowser ( process . argv [ 3 ] ) ;
1617}
1718else if ( cmd ) {
18- GetCommand ( process . argv [ 2 ] )
19+ FetchCommand ( process . argv [ 2 ] )
1920}
2021else {
21- opn ( "www.gitcheats.com" )
22+ LaunchBrowser ( false ) ;
2223}
2324
25+ /**
26+ * Shows CLI help
27+ */
2428function ShowHelp ( ) {
2529 console . log ( "" ) ;
2630 console . log ( "A Terminal App For Reaching Gitcheats.com Easily" ) ;
2731 console . log ( "" ) ;
2832 console . log ( "Commands:" ) ;
29- console . log ( "gitcheats " + chalk . blue ( "[command]" ) + " Print command descriptions right into your terminal" ) ;
30- console . log ( "gitcheats " + chalk . green ( "-l --launch" ) + chalk . blue ( " [command]" ) + " Launch GitCheats.com in browser with your command filtered" ) ;
33+ console . log ( "gitcheats " + chalk . yellow ( "[command]" ) + " Print command descriptions right into your terminal" ) ;
34+ console . log ( "gitcheats " + chalk . green ( "-l --launch" ) + chalk . yellow ( " [command]" ) + " Launch GitCheats.com in browser with your command filtered" ) ;
3135 console . log ( "gitcheats " + chalk . green ( "-h --help" ) + " Display this help" ) ;
3236}
3337
34- function GetCommand ( cmd ) {
35- console . log ( "\n" ) ;
36- const spinner = ora ( ) . start ( ) ;
37- spinner . color = 'blue' ;
38- spinner . text = chalk . blue ( "Fetching '" + chalk . green ( cmd ) + "' from gitcheats.com" ) ;
38+ /**
39+ * Fetch command from original gitcheats json
40+ *
41+ * @param {String } cmd Command to search for within json file
42+ */
43+ function FetchCommand ( cmd ) {
44+ const spinner = ora ( {
45+ text : chalk . bold ( "Fetching '" + chalk . yellow ( cmd ) + "' from gitcheats.com" ) ,
46+ color : 'green' ,
47+ interval : 80 ,
48+ spinner : "dots"
49+ } ) . start ( ) ;
3950
40- getJSON ( 'https://raw.githubusercontent.com/excalith/Git-Cheats/master/assets/commands.json' , function ( error , json ) {
51+ getJSON ( jsonLink , function ( error , json ) {
4152 let data = json . commands [ cmd ] ;
42- console . log ( "\n\n " ) ;
53+ console . log ( "\n" ) ;
4354
4455 if ( data == null ) {
45- console . log ( "Command " + chalk . red ( cmd ) + " not found in gitcheats.com" ) ;
46- console . log ( chalk . gray ( "Please consider contributing " + " https://github.com/excalith/Git-Cheats") )
56+ spinner . fail ( "Command ' " + chalk . red ( cmd ) + "' not found in gitcheats.com" ) ;
57+ console . log ( chalk . gray ( "Please consider contributing https://github.com/excalith/Git-Cheats" ) )
4758 }
4859 else {
49- var table = new Table ( {
50- head : [ chalk . green . bold ( 'Option' ) , chalk . green . bold ( 'Description' ) ] ,
51- colWidths : [ 60 , 220 ] ,
52- chars : {
53- 'top' : '═' , 'top-mid' : '╤' , 'top-left' : '╔' , 'top-right' : '╗' ,
54- 'bottom' : '═' , 'bottom-mid' : '╧' , 'bottom-left' : '╚' ,
55- 'bottom-right' : '╝' , 'left' : '║' , 'left-mid' : '╟' , 'mid' : '─' ,
56- 'mid-mid' : '┼' , 'right' : '║' , 'right-mid' : '╢' , 'middle' : '│'
57- }
58- } ) ;
59-
60- table . push (
61- [ cmd , data . desc . en ]
62- ) ;
60+ LogCommand ( cmd , data . desc . en ) ;
6361
6462 data . options . forEach ( element => {
65- table . push (
66- [ element . code , element . desc . en ]
67- ) ;
63+ LogCommand ( element . code , element . desc . en ) ;
6864 } ) ;
69-
70- console . log ( table . toString ( ) ) ;
7165 }
72-
7366 spinner . stop ( ) ;
7467 } )
68+ }
69+
70+ /**
71+ * Print command and description into terminal
72+ *
73+ * @param {String } command Command title
74+ * @param {String } description Command description
75+ */
76+ function LogCommand ( command , description ) {
77+ console . log ( bar + chalk . green . bold ( "> git " + command ) ) ;
78+ console . log ( bar + description ) ;
79+ console . log ( bar ) ;
80+ }
81+
82+ /**
83+ * Fetch command from original gitcheats json
84+ *
85+ * @param {boolean } hasCommand Checks if argv has a git command
86+ */
87+ function LaunchBrowser ( hasCommand ) {
88+ if ( hasCommand )
89+ opn ( "www.gitcheats.com?c=" + process . argv [ 3 ] )
90+ else
91+ opn ( "www.gitcheats.com" )
7592}
0 commit comments