-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcli.js
executable file
·98 lines (81 loc) · 3.12 KB
/
cli.js
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
#! /usr/bin/env node
// NPM ecosystem includes
const parseArgs = require("minimist")
const path = require("path")
const fs = require("fs")
const child_process = require("child_process")
// Particles Includes
const { Disk } = require("scrollsdk/products/Disk.node.js")
const { Particle } = require("scrollsdk/products/Particle.js")
const { SimpleCLI } = require("scroll-cli")
const { ScrollHub } = require("./ScrollHub.js")
const packageJson = require("./package.json")
class ScrollHubCLI extends SimpleCLI {
welcomeMessage = `\n🛜 WELCOME TO SCROLLHUB (v${packageJson.version})`
startCommand(cwd) {
new ScrollHub(cwd).startAll()
}
getRunningScrollHubs() {
try {
// Get detailed process info including CPU and memory
const command = "ps aux | grep '[S]crollHub' | awk '{printf \"%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s %s %s\\n\", $2, $3, $4, $5, $6, $9, $11, $12, $13}'"
const output = child_process.execSync(command, { encoding: "utf-8" }).trim()
if (!output) return []
return output.split("\n").map(line => {
const [pid, cpu, mem, vsz, rss, startTime, command] = line.split("\t")
// Get listening ports for this process
const portsCommand = `lsof -Pan -p ${pid} -i | grep LISTEN | awk '{print $9}' | cut -d: -f2`
let ports = []
try {
ports = child_process.execSync(portsCommand, { encoding: "utf-8" }).trim().split("\n").filter(Boolean)
} catch (e) {
// Process might be gone or no permissions
}
return {
pid,
cpu: `${parseFloat(cpu).toFixed(1)}%`,
mem: `${parseFloat(mem).toFixed(1)}%`,
vsz: `${Math.round(vsz / 1024)}MB`, // Convert to MB
rss: `${Math.round(rss / 1024)}MB`, // Convert to MB
startTime,
ports,
command
}
})
} catch (error) {
console.error("Error getting ScrollHub processes:", error.message)
return []
}
}
listCommand(cwd) {
const processes = this.getRunningScrollHubs()
if (!processes.length) {
console.log("No ScrollHub processes currently running")
return
}
console.log("\nRunning ScrollHub Processes:")
console.log("PID\tCPU\tMEM\tRSS\tSTARTED\tPORTS\t\tCOMMAND")
console.log("-".repeat(80))
processes.forEach(proc => {
const ports = proc.ports.length ? proc.ports.join(", ") : "none"
console.log(`${proc.pid}\t` + `${proc.cpu}\t` + `${proc.mem}\t` + `${proc.rss}\t` + `${proc.startTime}\t` + `${ports}\t\t` + `${proc.command}`)
})
}
killCommand(cwd) {
const processes = this.getRunningScrollHubs()
if (!processes.length) {
console.log("No ScrollHub process found")
return
}
processes.forEach(proc => {
try {
child_process.execSync(`kill -9 ${proc.pid}`)
console.log(`Successfully killed ScrollHub process (PID: ${proc.pid})`)
} catch (error) {
console.error(`Error killing process ${proc.pid}:`, error.message)
}
})
}
}
if (module && !module.parent) new ScrollHubCLI().executeUsersInstructionsFromShell(parseArgs(process.argv.slice(2))._)
module.exports = { ScrollHubCLI }