11#!/usr/bin/env node
22const chalk = require ( "chalk" ) ;
3- const getJSON = require ( "get-json " ) ;
3+ const request = require ( "request " ) ;
44const opn = require ( "opn" ) ;
55const ora = require ( "ora" ) ;
66const Configstore = require ( "configstore" ) ;
77const pkg = require ( "./package.json" ) ;
88const jsonLink =
9- "https://raw.githubusercontent.com/excalith/Git-Cheats /master/assets/commands.json" ;
9+ "https://raw.githubusercontent.com/excalith/git-cheats /master/assets/commands.json" ;
1010const conf = new Configstore ( pkg . name , { lang : "en" } ) ;
1111
1212let cmd = process . argv [ 2 ] ;
@@ -75,28 +75,38 @@ function FetchCommand(cmd) {
7575 spinner : "dots"
7676 } ) . start ( ) ;
7777
78- getJSON ( jsonLink , function ( error , json ) {
79- let data = json . commands [ cmd ] ;
80- console . log ( "\n" ) ;
78+ request (
79+ {
80+ url : jsonLink ,
81+ json : true
82+ } ,
83+ function ( error , response , body ) {
84+ if ( ! error && response . statusCode === 200 ) {
85+ let data = body . commands [ cmd ] ;
8186
82- if ( data == null ) {
83- spinner . fail ( "Command " + chalk . red ( cmd ) + " not found in gitcheats.com" ) ;
84- spinner . info (
85- chalk . gray (
86- "If gitcheats is missing a command, please consider contributing " +
87- chalk . blue ( "https://github.com/excalith/Git-Cheats" )
88- )
89- ) ;
90- } else {
91- LogCommand ( cmd , data . desc [ lang ] ) ;
87+ if ( data === undefined ) {
88+ spinner . fail (
89+ "Command " + chalk . red ( cmd ) + " not found in gitcheats.com"
90+ ) ;
91+ spinner . info (
92+ chalk . gray (
93+ "If gitcheats is missing a command, please consider contributing " +
94+ chalk . blue ( "https://github.com/excalith/git-cheats" )
95+ )
96+ ) ;
97+ } else {
98+ console . log ( "\n" ) ;
99+ LogCommand ( cmd , data . desc [ lang ] ) ;
92100
93- data . options . forEach ( element => {
94- LogCommand ( element . code , element . desc [ lang ] ) ;
95- } ) ;
96- }
101+ data . options . forEach ( element => {
102+ LogCommand ( element . code , element . desc [ lang ] ) ;
103+ } ) ;
97104
98- spinner . stop ( ) ;
99- } ) ;
105+ spinner . stop ( ) ;
106+ }
107+ }
108+ }
109+ ) ;
100110}
101111
102112/**
@@ -112,24 +122,32 @@ function SetLanguage(key) {
112122 spinner : "dots"
113123 } ) . start ( ) ;
114124
115- getJSON ( jsonLink , function ( error , json ) {
116- let data = json . settings . languages [ key ] ;
117- console . log ( "\n" ) ;
125+ request (
126+ {
127+ url : jsonLink ,
128+ json : true
129+ } ,
130+ function ( error , response , body ) {
131+ if ( ! error && response . statusCode === 200 ) {
132+ let data = body . settings . languages [ key ] ;
118133
119- if ( data == null ) {
120- spinner . fail ( "Language not found. Available languages:" ) ;
121- Object . keys ( json . settings . languages ) . forEach ( function ( key ) {
122- console . log (
123- key . padStart ( 4 ) + ": " . padStart ( 5 ) + json . settings . languages [ key ]
124- ) ;
125- } ) ;
126- } else {
127- conf . set ( "lang" , key ) ;
128- spinner . succeed ( "Language set to " + data + "\n" ) ;
129- }
134+ if ( data === undefined ) {
135+ spinner . fail ( "Language not found. Available languages:" ) ;
136+ Object . keys ( body . settings . languages ) . forEach ( function ( key ) {
137+ console . log (
138+ key . padStart ( 4 ) + ": " . padStart ( 5 ) + body . settings . languages [ key ]
139+ ) ;
140+ } ) ;
141+ } else {
142+ console . log ( "\n" ) ;
143+ conf . set ( "lang" , key ) ;
144+ spinner . succeed ( "Language set to " + data + "\n" ) ;
130145
131- spinner . stop ( ) ;
132- } ) ;
146+ spinner . stop ( ) ;
147+ }
148+ }
149+ }
150+ ) ;
133151}
134152
135153/**
0 commit comments