-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcodex-routing.js
More file actions
130 lines (119 loc) · 2.87 KB
/
codex-routing.js
File metadata and controls
130 lines (119 loc) · 2.87 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
128
129
130
const AUTH_SUBCOMMANDS = new Set([
"login",
"list",
"status",
"switch",
"best",
"check",
"features",
"verify-flagged",
"forecast",
"report",
"fix",
"doctor",
]);
const COMMAND_FLAGS_WITH_VALUE = new Set(["-c", "--config"]);
const HELP_OR_VERSION_FLAGS = new Set(["--help", "-h", "--version"]);
export function normalizeAuthAlias(args) {
if (args.length >= 2 && args[0] === "multi" && args[1] === "auth") {
return ["auth", ...args.slice(2)];
}
if (args.length >= 1 && (args[0] === "multi-auth" || args[0] === "multiauth")) {
return ["auth", ...args.slice(1)];
}
return args;
}
export function shouldHandleMultiAuthAuth(args) {
if (args[0] !== "auth") return false;
if (args.length === 1) return true;
const subcommand = args[1];
if (typeof subcommand !== "string") return false;
if (subcommand.startsWith("-")) return true;
return AUTH_SUBCOMMANDS.has(subcommand);
}
export function findPrimaryCodexCommand(args) {
let expectFlagValue = false;
let stopOptionParsing = false;
for (let index = 0; index < args.length; index += 1) {
const command = `${args[index] ?? ""}`.trim();
const normalizedCommand = command.toLowerCase();
if (normalizedCommand.length === 0) {
continue;
}
if (expectFlagValue) {
expectFlagValue = false;
continue;
}
if (!stopOptionParsing && normalizedCommand === "--") {
stopOptionParsing = true;
continue;
}
if (!stopOptionParsing) {
if (COMMAND_FLAGS_WITH_VALUE.has(normalizedCommand)) {
expectFlagValue = true;
continue;
}
if (normalizedCommand.startsWith("--config=")) {
continue;
}
if (normalizedCommand.startsWith("-")) {
continue;
}
}
return {
command,
normalizedCommand,
index,
};
}
return null;
}
export function hasTopLevelHelpOrVersionFlag(args) {
let expectFlagValue = false;
for (let index = 0; index < args.length; index += 1) {
const normalizedArg = `${args[index] ?? ""}`.trim().toLowerCase();
if (normalizedArg.length === 0) {
continue;
}
if (expectFlagValue) {
expectFlagValue = false;
continue;
}
if (normalizedArg === "--") {
return false;
}
if (HELP_OR_VERSION_FLAGS.has(normalizedArg)) {
return true;
}
if (COMMAND_FLAGS_WITH_VALUE.has(normalizedArg)) {
expectFlagValue = true;
continue;
}
if (normalizedArg.startsWith("--config=")) {
continue;
}
if (normalizedArg.startsWith("-")) {
continue;
}
return false;
}
return false;
}
export function splitCodexCommandArgs(args) {
const primaryCommand = findPrimaryCodexCommand(args);
if (!primaryCommand) {
return {
leadingArgs: [...args],
command: null,
normalizedCommand: null,
trailingArgs: [],
};
}
return {
leadingArgs: args.slice(0, primaryCommand.index),
command: primaryCommand.command,
normalizedCommand: primaryCommand.normalizedCommand,
trailingArgs: args.slice(primaryCommand.index + 1),
};
}
export { AUTH_SUBCOMMANDS };