-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
493 lines (433 loc) · 12.5 KB
/
cli.js
File metadata and controls
493 lines (433 loc) · 12.5 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/usr/bin/env node
const inquirer = require("inquirer");
const chalk = require("chalk");
const boxen = require("boxen").default;
const figlet = require("figlet");
const clipboardy = require("clipboardy");
// Import the command modules loader
const { getAvailableDevTools, getDevToolModule, searchAllCommands } = require('./commands');
// Function to clear terminal screen
function clearTerminal() {
// Clear terminal for cross-platform compatibility
process.stdout.write('\x1Bc');
// Alternative method for some terminals
console.clear();
}
// Display the beautiful ASCII art header
function displayHeader() {
const header = figlet.textSync("Helpsheet", {
font: "Standard",
horizontalLayout: "default",
verticalLayout: "default"
});
console.log(chalk.cyan(header));
console.log(chalk.yellow("🚀 Your comprehensive offline terminal help system"));
console.log(chalk.gray("Navigate through developer tools and find the commands you need\n"));
}
// Display available tools summary with collapsible interface
function displayToolsSummary(devTools) {
console.log(chalk.blue(`📚 ${devTools.length} Development Knowledge Bases Available:`));
console.log(chalk.gray(" (Use arrow keys and Enter to navigate)\n"));
}
// Display expanded tools list
function displayExpandedTools(devTools) {
console.log(chalk.blue(`📚 ${devTools.length} Development Knowledge Bases Available:\n`));
devTools.forEach(tool => {
console.log(chalk.white(`${tool.icon} ${tool.name} - ${tool.description}`));
console.log(chalk.gray(` ${tool.categoryCount} command categories\n`));
});
}
// Main menu to select development tool
async function selectDevTool() {
const devTools = getAvailableDevTools();
if (devTools.length === 0) {
console.log(chalk.red("❌ No command modules found. Please check the commands directory."));
process.exit(1);
}
// Clear terminal and show fresh interface
clearTerminal();
displayHeader();
displayToolsSummary(devTools);
// Ask if user wants to see details
const { showDetails } = await inquirer.prompt([
{
type: "list",
name: "showDetails",
message: "What would you like to do?",
choices: [
{
name: "🔽 Expand knowledge bases details",
value: "expand"
},
{
name: "🚀 Start exploring tools",
value: "start"
},
{
name: "❌ Exit",
value: "exit"
}
]
}
]);
if (showDetails === "exit") {
console.log(chalk.blue("👋 Thanks for using Helpsheet!"));
process.exit(0);
}
if (showDetails === "expand") {
// Clear and show expanded view
clearTerminal();
displayHeader();
displayExpandedTools(devTools);
// Wait for user to continue
await inquirer.prompt([
{
type: "input",
name: "continue",
message: "Press Enter to continue to tool selection...",
default: ""
}
]);
// Clear and show main interface
clearTerminal();
displayHeader();
displayToolsSummary(devTools);
}
// Now show the main tool selection
const { selectedTool } = await inquirer.prompt([
{
type: "list",
name: "selectedTool",
message: "🔧 Which development tool would you like to explore?",
choices: [
...devTools.map(tool => ({
name: `${tool.icon} ${tool.name} - ${tool.description} (${tool.categoryCount} categories)`,
value: tool.key
})),
new inquirer.Separator(),
{
name: "🔍 Search across all tools",
value: "search"
},
{
name: "❌ Exit",
value: "exit"
}
]
}
]);
if (selectedTool === "exit") {
console.log(chalk.blue("👋 Thanks for using Helpsheet!"));
process.exit(0);
}
if (selectedTool === "search") {
await handleGlobalSearch();
return;
}
await selectCategory(selectedTool);
}
// Handle global search across all tools
async function handleGlobalSearch() {
const { searchQuery } = await inquirer.prompt([
{
type: "input",
name: "searchQuery",
message: "🔍 What command are you looking for?",
validate: (input) => input.trim().length > 0 ? true : "Please enter a search term"
}
]);
const results = searchAllCommands(searchQuery.trim());
if (results.length === 0) {
console.log(chalk.yellow("🔍 No commands found matching your search."));
await selectDevTool();
return;
}
// Group results by dev tool
const groupedResults = {};
results.forEach(result => {
if (!groupedResults[result.devTool]) {
groupedResults[result.devTool] = [];
}
groupedResults[result.devTool].push(result);
});
console.log(chalk.green(`\n🔍 Found ${results.length} commands matching "${searchQuery}":\n`));
Object.entries(groupedResults).forEach(([devTool, commands]) => {
console.log(chalk.cyan(`\n${commands[0].devToolIcon} ${devTool}:`));
commands.forEach(command => {
console.log(chalk.white(` ${command.cmd}`));
console.log(chalk.gray(` ${command.desc}`));
});
});
// Ask what to do next after search
const { nextAction } = await inquirer.prompt([
{
type: "list",
name: "nextAction",
message: "\nWhat would you like to do next?",
choices: [
{
name: "🔄 Search again",
value: "search_again"
},
{
name: "🏠 Back to main menu",
value: "main_menu"
},
{
name: "❌ Exit",
value: "exit"
}
]
}
]);
switch (nextAction) {
case "search_again":
await handleGlobalSearch();
break;
case "main_menu":
await selectDevTool();
break;
case "exit":
console.log(chalk.blue("👋 Thanks for using Helpsheet!"));
process.exit(0);
}
}
// Select category within a specific dev tool
async function selectCategory(toolKey) {
const devTool = getDevToolModule(toolKey);
if (!devTool) {
console.log(chalk.red(`❌ Failed to load ${toolKey} module.`));
await selectDevTool();
return;
}
const categories = devTool.getCategories();
const { selectedCategory } = await inquirer.prompt([
{
type: "list",
name: "selectedCategory",
message: `${devTool.icon} ${devTool.name} - Select a category:`,
choices: [
...categories.map(category => ({
name: category,
value: category
})),
new inquirer.Separator(),
{
name: "🔍 Search within this tool",
value: "search"
},
{
name: "⬅️ Back to dev tools",
value: "back"
},
{
name: "❌ Exit",
value: "exit"
}
]
}
]);
if (selectedCategory === "exit") {
console.log(chalk.blue("👋 Thanks for using Helpsheet!"));
process.exit(0);
}
if (selectedCategory === "back") {
await selectDevTool();
return;
}
if (selectedCategory === "search") {
await handleToolSearch(toolKey);
return;
}
await displayCommands(toolKey, selectedCategory);
}
// Handle search within a specific tool
async function handleToolSearch(toolKey) {
const devTool = getDevToolModule(toolKey);
const { searchQuery } = await inquirer.prompt([
{
type: "input",
name: "searchQuery",
message: `🔍 Search within ${devTool.name}:`,
validate: (input) => input.trim().length > 0 ? true : "Please enter a search term"
}
]);
const results = devTool.searchCommands(searchQuery.trim());
if (results.length === 0) {
console.log(chalk.yellow("🔍 No commands found matching your search."));
await selectCategory(toolKey);
return;
}
console.log(chalk.green(`\n🔍 Found ${results.length} commands in ${devTool.name} matching "${searchQuery}":\n`));
results.forEach(command => {
console.log(chalk.white(` ${command.cmd}`));
console.log(chalk.gray(` ${command.desc}`));
console.log(chalk.cyan(` Category: ${command.category}\n`));
});
// Ask what to do next after search
const { nextAction } = await inquirer.prompt([
{
type: "list",
name: "nextAction",
message: "\nWhat would you like to do next?",
choices: [
{
name: "🔄 Search again in this tool",
value: "search_again"
},
{
name: "📁 Browse categories",
value: "browse_categories"
},
{
name: "🏠 Back to main menu",
value: "main_menu"
},
{
name: "❌ Exit",
value: "exit"
}
]
}
]);
switch (nextAction) {
case "search_again":
await handleToolSearch(toolKey);
break;
case "browse_categories":
await selectCategory(toolKey);
break;
case "main_menu":
await selectDevTool();
break;
case "exit":
console.log(chalk.blue("👋 Thanks for using Helpsheet!"));
process.exit(0);
}
}
// Display commands for a specific category
async function displayCommands(toolKey, category) {
const devTool = getDevToolModule(toolKey);
const commands = devTool.getCommands(category);
console.log(chalk.cyan(`\n${devTool.icon} ${devTool.name} - ${category}`));
console.log(chalk.gray("=".repeat(50)));
const { selectedCommand } = await inquirer.prompt([
{
type: "list",
name: "selectedCommand",
message: "📋 Select a command to copy to clipboard:",
choices: [
...commands.map(command => ({
name: `${command.cmd}`,
value: command
})),
new inquirer.Separator(),
{
name: "⬅️ Back to categories",
value: "back"
},
{
name: "🏠 Back to dev tools",
value: "home"
},
{
name: "❌ Exit",
value: "exit"
}
]
}
]);
if (selectedCommand === "exit") {
console.log(chalk.blue("👋 Thanks for using Helpsheet!"));
process.exit(0);
}
if (selectedCommand === "back") {
await selectCategory(toolKey);
return;
}
if (selectedCommand === "home") {
await selectDevTool();
return;
}
// Copy command to clipboard and show details
try {
await clipboardy.write(selectedCommand.cmd);
const commandBox = boxen(
`${chalk.green("✅ Command copied to clipboard!")}\n\n` +
`${chalk.white("Command:")} ${chalk.yellow(selectedCommand.cmd)}\n\n` +
`${chalk.white("Description:")} ${chalk.cyan(selectedCommand.desc)}\n\n` +
`${chalk.gray("You can now paste this command in your terminal")}`,
{
padding: 1,
margin: 1,
borderStyle: "round",
borderColor: "green"
}
);
console.log(commandBox);
// Ask what to do next
const { nextAction } = await inquirer.prompt([
{
type: "list",
name: "nextAction",
message: "What would you like to do next?",
choices: [
{
name: "🔄 View another command from this category",
value: "same_category"
},
{
name: "📁 Browse another category",
value: "other_category"
},
{
name: "🔧 Switch to another dev tool",
value: "other_tool"
},
{
name: "❌ Exit",
value: "exit"
}
]
}
]);
switch (nextAction) {
case "same_category":
await displayCommands(toolKey, category);
break;
case "other_category":
await selectCategory(toolKey);
break;
case "other_tool":
await selectDevTool();
break;
case "exit":
console.log(chalk.blue("👋 Thanks for using Helpsheet!"));
process.exit(0);
}
} catch (error) {
console.log(chalk.red(`❌ Failed to copy command to clipboard: ${error.message}`));
console.log(chalk.yellow(`Command: ${selectedCommand.cmd}`));
console.log(chalk.cyan(`Description: ${selectedCommand.desc}`));
await displayCommands(toolKey, category);
}
}
// Main application entry point
async function main() {
try {
displayHeader();
await selectDevTool();
} catch (error) {
console.error(chalk.red("❌ An error occurred:"), error.message);
process.exit(1);
}
}
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log(chalk.blue("\n👋 Thanks for using Helpsheet!"));
process.exit(0);
});
// Start the application
if (require.main === module) {
main();
}