Files
orca/src/cli/dispatch.ts
T
OrcaWinandOrcaWin 120f01331c [comment audit] Shorten CLI parsing and host-routing comments (#32265)
* chore: shorten process-host comments

* chore: audit CLI parsing and host-routing comments

* chore: remove stale host-selector restriction comment

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-23 07:49:36 -07:00

54 lines
1.6 KiB
TypeScript

import type { RuntimeClient } from './runtime-client'
import { RuntimeClientError } from './runtime/types'
import { HANDLER_GROUPS, type HandlerGroup } from './handler-group-manifest'
export type HandlerContext = {
flags: Map<string, string | boolean>
client: RuntimeClient
cwd: string
json: boolean
rawArgs?: string[]
}
export type CommandHandler = (ctx: HandlerContext) => Promise<void>
// Load only the selected handler group to avoid importing unrelated command dependencies.
function buildRoutes(groups: readonly HandlerGroup[]): Map<string, HandlerGroup> {
const table = new Map<string, HandlerGroup>()
for (const group of groups) {
for (const key of group.keys) {
const owner = table.get(key)
if (owner) {
throw new Error(
`Duplicate CLI handler registration for "${key}" (${owner.name} and ${group.name})`
)
}
table.set(key, group)
}
}
return table
}
const ROUTES = buildRoutes(HANDLER_GROUPS)
export const HANDLER_COMMAND_KEYS: ReadonlySet<string> = new Set(ROUTES.keys())
export async function dispatch(commandPath: string[], ctx: HandlerContext): Promise<void> {
const key = commandPath.join(' ')
const group = ROUTES.get(key)
if (!group) {
throw new RuntimeClientError('invalid_argument', `Unknown command: ${key}`)
}
const handler = (await group.load())[key]
// Missing handlers indicate drift from the CI-verified manifest.
if (!handler) {
throw new RuntimeClientError(
'invalid_argument',
`CLI handler group "${group.name}" does not export "${key}"`
)
}
await handler(ctx)
}
export { buildRoutes as buildHandlerRoutes }