-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathcli-command.ts
More file actions
37 lines (35 loc) · 1.03 KB
/
Copy pathcli-command.ts
File metadata and controls
37 lines (35 loc) · 1.03 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
const SCRIPT_PATH_PATTERN = /\.(cjs|mjs|js|cts|mts|ts)$/;
const FLAGS_WITHOUT_VALUE = new Set(["--help", "-h"]);
function isScriptPath(arg: string): boolean {
return SCRIPT_PATH_PATTERN.test(arg);
}
/**
* Read the first positional CLI command (not a flag, not a script path).
* `tsx index.ts auth` and `node build/index.js auth` both resolve to `auth`.
* Space-separated option values such as `--api-url https://...` are skipped.
* Flags such as --token stay on the MCP server path.
*/
export function getPositionalCliCommand(argv: readonly string[]): string | undefined {
const args = argv.slice(2);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg.startsWith("--") && arg.includes("=")) {
continue;
}
if (arg.startsWith("-")) {
if (
!FLAGS_WITHOUT_VALUE.has(arg) &&
i + 1 < args.length &&
!args[i + 1].startsWith("-")
) {
i += 1;
}
continue;
}
if (isScriptPath(arg)) {
continue;
}
return arg;
}
return undefined;
}