forked from Postmodum37/ocwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
97 lines (84 loc) · 2.31 KB
/
cli.ts
File metadata and controls
97 lines (84 loc) · 2.31 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
import { DEFAULT_PORT } from "../shared/constants";
export interface CLIFlags {
port: number;
host: string;
noBrowser: boolean;
projectPath: string | null;
showHelp: boolean;
}
export function parseArgs(): CLIFlags {
const args = process.argv.slice(2);
const flags: CLIFlags = {
port: DEFAULT_PORT,
host: "localhost",
noBrowser: false,
projectPath: null,
showHelp: false,
};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--help" || arg === "-h") {
flags.showHelp = true;
} else if (arg === "--no-browser") {
flags.noBrowser = true;
} else if (arg === "--port") {
const portValue = args[i + 1];
if (portValue && !isNaN(parseInt(portValue))) {
flags.port = parseInt(portValue);
i++;
}
} else if (arg === "--host") {
const hostValue = args[i + 1];
if (hostValue && /^[a-zA-Z0-9.\-:]+$/.test(hostValue)) {
flags.host = hostValue;
i++;
} else if (hostValue) {
console.warn(`[ocwatch] Invalid --host value ignored: ${hostValue}`);
i++;
}
} else if (arg === "--project") {
const projectPath = args[i + 1];
if (projectPath) {
flags.projectPath = projectPath;
i++;
}
}
}
return flags;
}
export function printHelp(): void {
console.log(`
OCWatch - Real-time OpenCode Activity Monitor
Usage: ocwatch [options]
Options:
--port <number> Server port (default: 50234)
--host <address> Bind address (default: localhost, use 0.0.0.0 for all interfaces)
--no-browser Skip auto-opening browser
--project <path> Set default project filter
--help, -h Show this help message
Examples:
ocwatch
ocwatch --port 50999
ocwatch --host 0.0.0.0
ocwatch --no-browser
ocwatch --project /path/to/project
`);
}
export async function openBrowser(url: string): Promise<void> {
try {
const isHeadless =
process.env.CI === "true" ||
(!process.env.DISPLAY && !process.env.WAYLAND_DISPLAY);
if (isHeadless) {
console.log(`📱 Open browser: ${url}`);
return;
}
const proc = Bun.spawn(["open", url], {
stdio: ["ignore", "ignore", "ignore"],
});
proc.exited.catch(() => {
});
} catch (error) {
console.log(`📱 Open browser: ${url}`);
}
}