UNPKG

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