Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit eae0768

Browse files
committed
Clean-Up cli.js
1 parent c6773de commit eae0768

File tree

1 file changed

+82
-59
lines changed

1 file changed

+82
-59
lines changed

cli.js

Lines changed: 82 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
11
#!/usr/bin/env node
2-
const chalk = require('chalk');
3-
const getJSON = require('get-json')
4-
const opn = require('opn');
5-
const ora = require('ora')
2+
const chalk = require("chalk");
3+
const getJSON = require("get-json");
4+
const opn = require("opn");
5+
const ora = require("ora");
66

7-
const jsonLink = "https://raw.githubusercontent.com/excalith/Git-Cheats/master/assets/commands.json";
7+
const jsonLink =
8+
"https://raw.githubusercontent.com/excalith/Git-Cheats/master/assets/commands.json";
89
let cmd = process.argv[2];
910
let bar = chalk.green("║ ");
1011

1112
// Check Arguments
1213
if (cmd == "-h" || cmd == "--help") {
13-
ShowHelp();
14-
}
15-
else if (cmd == "-l" || cmd == "--launch") {
16-
LaunchBrowser(process.argv[3]);
17-
}
18-
else if (cmd) {
19-
FetchCommand(process.argv[2])
20-
}
21-
else {
22-
LaunchBrowser(false);
14+
ShowHelp();
15+
} else if (cmd == "-l" || cmd == "--launch") {
16+
LaunchBrowser(process.argv[3]);
17+
} else if (cmd) {
18+
FetchCommand(process.argv[2]);
19+
} else {
20+
LaunchBrowser(false);
2321
}
2422

2523
/**
2624
* Shows CLI help
2725
*/
2826
function ShowHelp() {
29-
console.log("");
30-
console.log(chalk.white.bold("GitCheats CLI - A Companion For GitCheats"));
31-
console.log("");
32-
console.log(chalk.white.bold("Commands:"));
33-
console.log("gitcheats Launch gitcheats.com directly");
34-
console.log("gitcheats " + chalk.yellow("[command]") + " Print command descriptions right into your terminal");
35-
console.log("gitcheats " + chalk.green("-l --launch") + chalk.yellow(" [command]") + " Launch gitcheats.com in browser with your command filtered");
36-
console.log("gitcheats " + chalk.green("-h --help") + " Display this help");
27+
console.log(
28+
chalk.white.bold("\nGitCheats CLI - A Companion For GitCheats\n")
29+
);
30+
console.log(chalk.white.bold("Commands:"));
31+
console.log("gitcheats".padEnd(39) + "Launch gitcheats.com directly");
32+
console.log(
33+
"gitcheats " +
34+
chalk.yellow("[command]").padEnd(39) +
35+
"Print command descriptions right into your terminal"
36+
);
37+
console.log(
38+
"gitcheats " +
39+
chalk.green("-l --launch") +
40+
chalk.yellow(" [command]").padEnd(28) +
41+
"Launch gitcheats.com in browser with your command filtered"
42+
);
43+
console.log(
44+
"gitcheats " + chalk.green("-h --help").padEnd(39) + "Display this help"
45+
);
3746
}
3847

3948
/**
@@ -42,31 +51,39 @@ function ShowHelp() {
4251
* @param {String} cmd Command to search for within json file
4352
*/
4453
function FetchCommand(cmd) {
45-
const spinner = ora({
46-
text: chalk.white.bold("Fetching '" + chalk.green(cmd) + "' from " + chalk.blue("http://gitcheats.com")),
47-
color: 'green',
48-
interval: 80,
49-
spinner: "dots"
50-
}).start();
54+
const spinner = ora({
55+
text: chalk.white.bold(
56+
"Fetching " +
57+
chalk.green(cmd) +
58+
" from " +
59+
chalk.blue("http://gitcheats.com")
60+
),
61+
color: "green",
62+
interval: 80,
63+
spinner: "dots"
64+
}).start();
5165

52-
getJSON(jsonLink, function (error, json) {
53-
let data = json.commands[cmd];
54-
console.log("\n");
66+
getJSON(jsonLink, function(error, json) {
67+
let data = json.commands[cmd];
68+
console.log("\n");
5569

56-
if (data == null) {
57-
spinner.fail("Command '" + chalk.red(cmd) + "' not found in gitcheats.com");
58-
spinner.info(chalk.gray("If gitcheats is missing a command, please consider contributing https://github.com/excalith/Git-Cheats"));
59-
}
60-
else {
61-
LogCommand(cmd, data.desc.en);
70+
if (data == null) {
71+
spinner.fail("Command " + chalk.red(cmd) + " not found in gitcheats.com");
72+
spinner.info(
73+
chalk.gray(
74+
"If gitcheats is missing a command, please consider contributing https://github.com/excalith/Git-Cheats"
75+
)
76+
);
77+
} else {
78+
LogCommand(cmd, data.desc.en);
6279

63-
data.options.forEach(element => {
64-
LogCommand(element.code, element.desc.en);
65-
});
66-
}
80+
data.options.forEach(element => {
81+
LogCommand(element.code, element.desc.en);
82+
});
83+
}
6784

68-
spinner.stop();
69-
})
85+
spinner.stop();
86+
});
7087
}
7188

7289
/**
@@ -76,9 +93,9 @@ function FetchCommand(cmd) {
7693
* @param {String} description Command description
7794
*/
7895
function LogCommand(command, description) {
79-
console.log(bar + chalk.green.bold("> git " + command));
80-
console.log(bar + description);
81-
console.log(bar);
96+
console.log(bar + chalk.green.bold("> git " + command));
97+
console.log(bar + description);
98+
console.log(bar);
8299
}
83100

84101
/**
@@ -87,15 +104,21 @@ function LogCommand(command, description) {
87104
* @param {boolean} hasCommand Checks if argv has a git command
88105
*/
89106
function LaunchBrowser(hasCommand) {
90-
if (hasCommand)
91-
{
92-
let command = process.argv[3];
93-
console.log(chalk.white.bold("Launching '" + chalk.yellow(command) + "' on " + chalk.blue("http://gitcheats.com")))
94-
opn("http://gitcheats.com?c=" + command)
95-
}
96-
else
97-
{
98-
console.log(chalk.white.bold("Launching " + chalk.blue("http://gitcheats.com")))
99-
opn("http://gitcheats.com")
100-
}
101-
}
107+
if (hasCommand) {
108+
let command = process.argv[3];
109+
console.log(
110+
chalk.white.bold(
111+
"Launching " +
112+
chalk.yellow(command) +
113+
" on " +
114+
chalk.blue("http://gitcheats.com")
115+
)
116+
);
117+
opn("http://gitcheats.com?c=" + command);
118+
} else {
119+
console.log(
120+
chalk.white.bold("Launching " + chalk.blue("http://gitcheats.com"))
121+
);
122+
opn("http://gitcheats.com");
123+
}
124+
}

0 commit comments

Comments
 (0)