perf(cli): speed up help/config paths and route config get/unset
This commit is contained in:
@@ -23,4 +23,16 @@ describe("program routes", () => {
|
||||
it("does not match unknown routes", () => {
|
||||
expect(findRoutedCommand(["definitely-not-real"])).toBeNull();
|
||||
});
|
||||
|
||||
it("returns false for config get route when path argument is missing", async () => {
|
||||
const route = findRoutedCommand(["config", "get"]);
|
||||
expect(route).not.toBeNull();
|
||||
await expect(route?.run(["node", "openclaw", "config", "get", "--json"])).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for config unset route when path argument is missing", async () => {
|
||||
const route = findRoutedCommand(["config", "unset"]);
|
||||
expect(route).not.toBeNull();
|
||||
await expect(route?.run(["node", "openclaw", "config", "unset"])).resolves.toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -88,12 +88,58 @@ const routeMemoryStatus: RouteSpec = {
|
||||
},
|
||||
};
|
||||
|
||||
function getCommandPositionals(argv: string[]): string[] {
|
||||
const out: string[] = [];
|
||||
const args = argv.slice(2);
|
||||
for (const arg of args) {
|
||||
if (!arg || arg === "--") {
|
||||
break;
|
||||
}
|
||||
if (arg.startsWith("-")) {
|
||||
continue;
|
||||
}
|
||||
out.push(arg);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
const routeConfigGet: RouteSpec = {
|
||||
match: (path) => path[0] === "config" && path[1] === "get",
|
||||
run: async (argv) => {
|
||||
const positionals = getCommandPositionals(argv);
|
||||
const pathArg = positionals[2];
|
||||
if (!pathArg) {
|
||||
return false;
|
||||
}
|
||||
const json = hasFlag(argv, "--json");
|
||||
const { runConfigGet } = await import("../config-cli.js");
|
||||
await runConfigGet({ path: pathArg, json });
|
||||
return true;
|
||||
},
|
||||
};
|
||||
|
||||
const routeConfigUnset: RouteSpec = {
|
||||
match: (path) => path[0] === "config" && path[1] === "unset",
|
||||
run: async (argv) => {
|
||||
const positionals = getCommandPositionals(argv);
|
||||
const pathArg = positionals[2];
|
||||
if (!pathArg) {
|
||||
return false;
|
||||
}
|
||||
const { runConfigUnset } = await import("../config-cli.js");
|
||||
await runConfigUnset({ path: pathArg });
|
||||
return true;
|
||||
},
|
||||
};
|
||||
|
||||
const routes: RouteSpec[] = [
|
||||
routeHealth,
|
||||
routeStatus,
|
||||
routeSessions,
|
||||
routeAgentsList,
|
||||
routeMemoryStatus,
|
||||
routeConfigGet,
|
||||
routeConfigUnset,
|
||||
];
|
||||
|
||||
export function findRoutedCommand(path: string[]): RouteSpec | null {
|
||||
|
||||
Reference in New Issue
Block a user