-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·123 lines (106 loc) · 4.23 KB
/
Copy pathcli.js
File metadata and controls
executable file
·123 lines (106 loc) · 4.23 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
#!/usr/bin/env node
//=================================================================
// CRYSTAL SYSTEM.JS — CLI Binary
// Crystal Studio Labs | Author: SahooShuvranshu
//=================================================================
'use strict';
const { crystalsystem, buildSystemTable, getAllStats } = require('./index.js');
const args = process.argv.slice(2);
function parseArgs(argv) {
const opts = {};
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (arg === '--help' || arg === '-h') { opts.help = true; }
else if (arg === '--once' || arg === '-o') { opts.once = true; }
else if (arg === '--json' || arg === '-j') { opts.json = true; }
else if (arg === '--no-color') { opts.showColors = false; }
else if (arg === '--no-network') { opts.showNetwork = false; }
else if (arg === '--no-disk') { opts.showDisk = false; }
else if (arg === '--no-battery') { opts.showBattery = false; }
else if (arg === '--no-gpu') { opts.showGPU = false; }
else if (arg === '--no-processes') { opts.showProcesses = false; }
else if (arg === '--no-clear') { opts.clearConsole = false; }
else if ((arg === '--interval' || arg === '-i') && argv[i + 1]) {
opts.interval = parseInt(argv[++i], 10) * 1000;
} else if ((arg === '--port' || arg === '-p') && argv[i + 1]) {
opts.httpPort = parseInt(argv[++i], 10);
} else if ((arg === '--log' || arg === '-l') && argv[i + 1]) {
opts.logFile = argv[++i];
}
}
return opts;
}
const HELP = `
\x1b[1m\x1b[36mCrystal System.JS v1.0.3\x1b[0m — System Monitor CLI
\x1b[2mCrystal Studio Labs | Author: SahooShuvranshu\x1b[0m
\x1b[2mhttps://github.com/Crystal-Studio-Labs/crystalsystem.js\x1b[0m
\x1b[1mUsage:\x1b[0m
crystalsystem [options]
\x1b[1mOptions:\x1b[0m
-h, --help Show this help message
-o, --once Display once and exit (no loop)
-j, --json Output raw JSON snapshot and exit
-i, --interval <sec> Refresh interval in seconds (default: 5)
-p, --port <port> Start HTTP API on given port
-l, --log <file> Log stats to a JSON file
--no-color Disable colored output
--no-network Hide network section
--no-disk Hide disk section
--no-battery Hide battery section
--no-gpu Hide GPU section
--no-processes Hide top processes section
--no-clear Don't clear console between updates
\x1b[1mExamples:\x1b[0m
crystalsystem # Live monitor (5s refresh)
crystalsystem -i 2 # Refresh every 2 seconds
crystalsystem -o # Show once and exit
crystalsystem -j # JSON snapshot to stdout
crystalsystem -p 3001 # + HTTP API at :3001
crystalsystem -l stats.json # + log to file
crystalsystem --no-disk --no-gpu # Hide sections
\x1b[1mHTTP API Routes (when --port is used):\x1b[0m
GET / All stats (JSON)
GET /cpu CPU info
GET /memory Memory info
GET /network Network info
GET /disk Disk info
GET /battery Battery info
GET /health Health check
`;
const opts = parseArgs(args);
if (opts.help) {
console.log(HELP);
process.exit(0);
}
if (opts.json) {
const stats = getAllStats(opts);
console.log(JSON.stringify(stats, null, 2));
process.exit(0);
}
if (opts.once) {
const table = buildSystemTable({ showColors: opts.showColors !== false, ...opts });
console.log(table);
process.exit(0);
}
// Live monitor
const instance = crystalsystem({
interval: opts.interval || 5000,
clearConsole: opts.clearConsole !== false,
showColors: opts.showColors !== false,
showNetwork: opts.showNetwork !== false,
showDisk: opts.showDisk !== false,
showBattery: opts.showBattery !== false,
showGPU: opts.showGPU !== false,
showProcesses: opts.showProcesses !== false,
httpPort: opts.httpPort || null,
logFile: opts.logFile || null,
});
// Alert handler for CLI
instance.on('alert', (alert) => {
process.stderr.write(`\x1b[33m[ALERT]\x1b[0m ${alert.message}\n`);
});
process.on('SIGINT', () => {
instance.stop();
console.log('\n\x1b[36m[CrystalSystem] Stopped.\x1b[0m\n');
process.exit(0);
});