This repository was archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·227 lines (208 loc) · 5.51 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env node
const chalk = require("chalk");
const request = require("request");
const opn = require("opn");
const ora = require("ora");
const Configstore = require("configstore");
const pkg = require("./package.json");
const jsonLink =
"https://raw.githubusercontent.com/excalith/git-cheats/master/assets/commands.json";
const boxen = require("boxen");
const checkForUpdate = require("update-check");
const conf = new Configstore(pkg.name, { lang: "en" });
let cmd = process.argv[2];
let bar = chalk.green("║ ");
let lang = conf.get("lang");
// Check Arguments
if (cmd === "-v" || cmd === "--version") {
ShowVersion();
} else if (cmd == "-h" || cmd == "--help") {
ShowHelp();
} else if (cmd == "-o" || cmd == "--open") {
LaunchBrowser(process.argv[3]);
} else if (cmd == "-l" || cmd == "--language") {
SetLanguage(process.argv[3]);
} else if (cmd) {
FetchCommand(process.argv[2]);
} else {
LaunchBrowser(false);
}
//** Check Updates */
async function ShowVersion() {
let update = null;
try {
update = await checkForUpdate(pkg);
} catch (error) {
console.log(chalk.red("\nFailed to check for updates:"));
console.error(error);
}
let updateText;
let commandText;
if (update) {
updateText =
"Update available " +
chalk.gray(pkg.version) +
" → " +
chalk.green(update.latest);
commandText = "Run " + chalk.cyan("npm i -g " + pkg.name) + " to update";
} else {
updateText = "You are using the latest version";
commandText = pkg.name + " v" + pkg.version;
}
console.log(
boxen(updateText + "\n" + commandText, {
padding: 1,
margin: 1,
align: "center"
})
);
}
/**
* Shows CLI help
*/
function ShowHelp() {
console.log(
chalk.white.bold("\nGitCheats CLI - A Companion For GitCheats\n")
);
console.log(chalk.white.bold("Commands:"));
console.log("gitcheats".padEnd(39) + "Open gitcheats.com in browser");
console.log(
"gitcheats " +
chalk.yellow("[command]").padEnd(39) +
"Print command descriptions right into your terminal"
);
console.log(
"gitcheats " +
chalk.green("-o --open") +
chalk.yellow(" [command]").padEnd(30) +
"Open gitcheats.com in browser with your command filtered"
);
console.log(
"gitcheats " +
chalk.green("-l --language") +
chalk.yellow(" [key]").padEnd(26) +
"Set your preffered language (Default: en)"
);
console.log(
"gitcheats " + chalk.green("-h --help").padEnd(39) + "Display this help"
);
}
/**
* Fetch command from original gitcheats json
*
* @param {String} cmd Command to search for within json file
*/
function FetchCommand(cmd) {
const spinner = ora({
text: chalk.white.bold(
"Fetching " +
chalk.green(cmd) +
" from " +
chalk.blue("http://gitcheats.com")
),
color: "green",
interval: 80,
spinner: "dots"
}).start();
request(
{
url: jsonLink,
json: true
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
let data = body.commands[cmd];
if (data === undefined) {
spinner.fail(
"Command " + chalk.red(cmd) + " not found in gitcheats.com"
);
spinner.info(
chalk.gray(
"If gitcheats is missing a command, please consider contributing " +
chalk.blue("https://github.com/excalith/git-cheats")
)
);
} else {
console.log("\n");
LogCommand(cmd, data.desc[lang]);
data.options.forEach(element => {
LogCommand(element.code, element.desc[lang]);
});
spinner.stop();
}
}
}
);
}
/**
* Check if language exists and set it to given key
*
* @param {String} key Language key
*/
function SetLanguage(key) {
const spinner = ora({
text: chalk.white.bold("Fetching Language: " + key),
color: "green",
interval: 80,
spinner: "dots"
}).start();
request(
{
url: jsonLink,
json: true
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
let data = body.settings.languages[key];
if (data === undefined) {
spinner.fail("Language not found. Available languages:");
Object.keys(body.settings.languages).forEach(function(key) {
console.log(
key.padStart(4) + ": ".padStart(5) + body.settings.languages[key]
);
});
} else {
console.log("\n");
conf.set("lang", key);
spinner.succeed("Language set to " + data + "\n");
spinner.stop();
}
}
}
);
}
/**
* Print command and description into terminal
*
* @param {String} command Command title
* @param {String} description Command description
*/
function LogCommand(command, description) {
console.log(bar + chalk.green.bold("> git " + command));
console.log(bar + description);
console.log(bar);
}
/**
* Fetch command from original gitcheats json
*
* @param {boolean} hasCommand Checks if argv has a git command
*/
function LaunchBrowser(hasCommand) {
if (hasCommand) {
let command = process.argv[3];
console.log(
chalk.white.bold(
"Launching " +
chalk.green(command) +
" on " +
chalk.blue("http://gitcheats.com")
)
);
opn("http://gitcheats.com?c=" + command);
} else {
console.log(
chalk.white.bold("Launching " + chalk.blue("http://gitcheats.com"))
);
opn("http://gitcheats.com");
}
}