2022-09-06 15:07:12 +02:00
|
|
|
import { Terminal } from "../../Terminal";
|
2021-09-16 01:50:44 +02:00
|
|
|
import { BaseServer } from "../../Server/BaseServer";
|
2023-04-28 00:21:06 +02:00
|
|
|
import { matchScriptPathUnanchored } from "../../utils/helpers/scriptKey";
|
2022-04-07 01:30:08 +02:00
|
|
|
import * as libarg from "arg";
|
2021-09-16 01:50:44 +02:00
|
|
|
|
2022-09-06 15:07:12 +02:00
|
|
|
export function ps(args: (string | number | boolean)[], server: BaseServer): void {
|
2021-12-24 07:31:07 +01:00
|
|
|
let flags;
|
2022-04-07 01:30:08 +02:00
|
|
|
try {
|
|
|
|
flags = libarg(
|
|
|
|
{
|
|
|
|
"--grep": String,
|
|
|
|
"-g": "--grep",
|
|
|
|
},
|
|
|
|
{ argv: args },
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2021-12-24 07:31:07 +01:00
|
|
|
// catch passing only -g / --grep with no string to use as the search
|
2022-09-06 15:07:12 +02:00
|
|
|
Terminal.error("Incorrect usage of ps command. Usage: ps [-g, --grep pattern]");
|
2021-09-16 01:50:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-04-28 00:21:06 +02:00
|
|
|
let pattern = flags["--grep"];
|
|
|
|
if (!pattern) {
|
|
|
|
pattern = ".*"; // Match anything
|
2022-03-06 05:34:04 +01:00
|
|
|
}
|
2023-04-28 00:21:06 +02:00
|
|
|
const re = matchScriptPathUnanchored(pattern);
|
|
|
|
for (const [k, byPid] of server.runningScriptMap) {
|
|
|
|
if (!re.test(k)) continue;
|
|
|
|
for (const rsObj of byPid.values()) {
|
|
|
|
const res = `(PID - ${rsObj.pid}) ${rsObj.filename} ${rsObj.args.join(" ")}`;
|
2022-09-06 15:07:12 +02:00
|
|
|
Terminal.print(res);
|
2021-12-24 02:39:45 +01:00
|
|
|
}
|
2021-09-16 01:50:44 +02:00
|
|
|
}
|
|
|
|
}
|