-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
127 lines (116 loc) · 4.05 KB
/
Copy pathcli.ts
File metadata and controls
127 lines (116 loc) · 4.05 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
123
124
125
126
127
#!/usr/bin/env node
// CLI entry point — argv normalization, `-h`/`-V` fast paths, dispatch over the command registry,
// and exit-code mapping. Port of `CLIEntry.swift`. Owned by Slice B.
//
// argv normalization mirrors `CLIEntry.effectiveArgv`: no args → the default `usage` command; a
// leading flag (`codexbar --provider claude`) prepends `usage`. Each command parses its own flags
// (and any subcommand token) via `parseArgs`. A thrown `CLIError` maps its `code` to
// `process.exitCode`; any other throw becomes `ExitCode.failure`.
import { CLIError, defaultOutputPrefs, ExitCode, type OutputPrefs } from "./cli/io.ts";
import { findCommand } from "./cli/registry.ts";
import { getVersion, helpForCommand } from "./cli/help.ts";
/** Mirror `CLIEntry.effectiveArgv`: empty → ["usage"]; leading flag → prepend "usage". */
function effectiveArgv(argv: string[]): string[] {
const first = argv[0];
if (first == null) return ["usage"];
if (first.startsWith("-")) return ["usage", ...argv];
return argv;
}
/** Resolve global OutputPrefs by scanning argv (mirrors `CLIOutputPreferences.from(argv:)`). */
function resolveOutputPrefs(argv: string[]): OutputPrefs {
const prefs = defaultOutputPrefs();
for (const arg of argv) {
switch (arg) {
case "--json-only":
prefs.jsonOnly = true;
break;
case "--json-output":
prefs.jsonLogs = true;
break;
case "--no-color":
prefs.useColor = false;
break;
case "--pretty":
prefs.pretty = true;
break;
default:
break;
}
}
return prefs;
}
function kindForCode(code: ExitCode): string {
switch (code) {
case ExitCode.parseFailure:
return "args";
case ExitCode.providerMissing:
return "providerMissing";
case ExitCode.timeout:
return "timeout";
default:
return "runtime";
}
}
/** Print a CLI-level error, respecting `--json-only` (JSON error payload) vs stderr text. */
function reportError(err: CLIError, prefs: OutputPrefs): void {
if (prefs.jsonOnly) {
const payload = {
provider: "cli",
account: null,
version: null,
source: "cli",
status: null,
usage: null,
credits: null,
antigravityPlanInfo: null,
openaiDashboard: null,
error: { message: err.message, kind: kindForCode(err.code) },
pace: null,
};
process.stdout.write(`${prefs.pretty ? JSON.stringify([payload], null, 2) : JSON.stringify([payload])}\n`);
} else {
const message = err.message.startsWith("Error") ? err.message : `Error: ${err.message}`;
process.stderr.write(`${message}\n`);
}
}
async function main(): Promise<void> {
const rawArgv = process.argv.slice(2);
const argv = effectiveArgv(rawArgv);
const prefs = resolveOutputPrefs(argv);
// Fast path: help before building/dispatching commands.
const helpIndex = argv.findIndex((arg) => arg === "-h" || arg === "--help");
if (helpIndex >= 0) {
const commandToken = helpIndex === 0 ? argv[1] : argv[0];
process.stdout.write(`${helpForCommand(commandToken)}\n`);
process.exitCode = ExitCode.success;
return;
}
// Fast path: version.
if (argv.includes("-V") || argv.includes("--version")) {
process.stdout.write(`CodexBar ${getVersion()}\n`);
process.exitCode = ExitCode.success;
return;
}
const command = findCommand(argv);
if (command == null) {
process.stderr.write(`Error: Unknown command '${argv[0] ?? ""}'.\n`);
process.exitCode = ExitCode.failure;
return;
}
// Each registry command owns a single top-level token; the remainder (subcommand + flags)
// is parsed by the command itself.
const rest = argv.slice(command.path.length);
try {
process.exitCode = await command.run(rest, prefs);
} catch (err) {
if (err instanceof CLIError) {
reportError(err, prefs);
process.exitCode = err.code;
} else {
const message = err instanceof Error ? err.message : String(err);
reportError(new CLIError(message, ExitCode.failure), prefs);
process.exitCode = ExitCode.failure;
}
}
}
await main();