-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
86 lines (76 loc) · 2.09 KB
/
cli.js
File metadata and controls
86 lines (76 loc) · 2.09 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
#!/usr/bin/env node
const cliProgress = require("cli-progress");
const chalk = require("chalk");
const yargs = require("yargs");
const themes = require("./themes");
const themeKeys = Object.keys(themes);
// Parse CLI arguments
const argv = yargs
.option("total", {
alias: "t",
type: "number",
default: 50,
describe: "Total number of steps in the progress bar",
})
.option("speed", {
alias: "s",
type: "number",
default: 100,
describe: "Interval speed in milliseconds",
})
.option("theme", {
alias: "m",
type: "string",
default: "classic",
describe: "Theme name or 'random' for a surprise",
choices: [...themeKeys, "random"],
})
.option("message", {
alias: "msg",
type: "string",
describe: "Final message after progress completes",
})
.option("list-themes", {
type: "boolean",
describe: "List all available themes with preview",
})
.help()
.alias("help", "h").argv;
// Handle --list-themes command
if (argv["list-themes"]) {
console.log(chalk.bold("\n🎨 Available Themes:\n"));
themeKeys.forEach((key) => {
const t = themes[key];
console.log(
`${key.padEnd(10)} → ${chalk.green(t.complete.repeat(3))}${chalk.gray(" ")}${chalk.white(t.incomplete.repeat(3))}`
);
});
console.log();
process.exit(0);
}
// Select theme
const selectedTheme =
argv.theme === "random"
? themes[themeKeys[Math.floor(Math.random() * themeKeys.length)]]
: themes[argv.theme] || themes.classic;
// Create and start the progress bar
const bar = new cliProgress.SingleBar({
format: `${chalk.magenta("{bar}")} | ${chalk.cyan("{percentage}%")} | ${chalk.green("{value}/{total}")}`,
barCompleteChar: selectedTheme.complete,
barIncompleteChar: selectedTheme.incomplete,
hideCursor: true,
});
const total = argv.total;
let value = 0;
bar.start(total, 0);
const interval = setInterval(() => {
value++;
bar.update(value);
if (value >= total) {
clearInterval(interval);
bar.stop();
console.log(
chalk.green(`\n${argv.message ? `🎉 ${argv.message}` : "✅ All done!"}`)
);
}
}, argv.speed);