refactor: allow run command to return a typed result
This commit is contained in:
parent
d4a8668b54
commit
5042d6ebc7
|
|
@ -1,5 +1,5 @@
|
|||
import type { Command, CommandSchema } from './types.js';
|
||||
import type { CommandRunner, CommandRunnerContext, PromptEvent } from './command-runner.js';
|
||||
import type {CommandResult, CommandRunner, CommandRunnerContext, PromptEvent} from './command-runner.js';
|
||||
import { parseCommand } from './command-parse.js';
|
||||
import { applyCommandSchema } from './command-validate.js';
|
||||
import { parseCommandSchema } from './schema-parse.js';
|
||||
|
|
@ -92,7 +92,7 @@ export function createCommandRunnerContext<TContext>(
|
|||
const runnerCtx: CommandRunnerContextExport<TContext> = {
|
||||
registry,
|
||||
context,
|
||||
run: (input: string) => runCommandWithContext(runnerCtx, input),
|
||||
run: <T=unknown>(input: string) => runCommandWithContext(runnerCtx, input) as Promise<CommandResult<T>>,
|
||||
runParsed: (command: Command) => runCommandParsedWithContext(runnerCtx, command),
|
||||
prompt,
|
||||
on,
|
||||
|
|
@ -114,7 +114,7 @@ async function executeWithRunnerContext<TContext>(
|
|||
runnerCtx: CommandRunnerContextExport<TContext>,
|
||||
runner: CommandRunner<TContext, unknown>,
|
||||
command: Command
|
||||
): Promise<{ success: true; result: unknown } | { success: false; error: string }> {
|
||||
): Promise<CommandResult> {
|
||||
try {
|
||||
const result = await runner.run.call(runnerCtx, command);
|
||||
return { success: true, result };
|
||||
|
|
@ -128,7 +128,7 @@ export async function runCommand<TContext>(
|
|||
registry: CommandRegistry<TContext>,
|
||||
context: TContext,
|
||||
input: string
|
||||
): Promise<{ success: true; result: unknown } | { success: false; error: string }> {
|
||||
): Promise<CommandResult> {
|
||||
const runnerCtx = createCommandRunnerContext(registry, context);
|
||||
return await runCommandWithContext(runnerCtx, input);
|
||||
}
|
||||
|
|
@ -145,7 +145,7 @@ export async function runCommandParsed<TContext>(
|
|||
registry: CommandRegistry<TContext>,
|
||||
context: TContext,
|
||||
command: Command
|
||||
): Promise<{ success: true; result: unknown } | { success: false; error: string }> {
|
||||
): Promise<CommandResult> {
|
||||
const runnerCtx = createCommandRunnerContext(registry, context);
|
||||
return await runCommandParsedWithContext(runnerCtx, command);
|
||||
}
|
||||
|
|
@ -153,7 +153,7 @@ export async function runCommandParsed<TContext>(
|
|||
async function runCommandParsedWithContext<TContext>(
|
||||
runnerCtx: CommandRunnerContextExport<TContext>,
|
||||
command: Command
|
||||
): Promise<{ success: true; result: unknown } | { success: false; error: string }> {
|
||||
): Promise<CommandResult> {
|
||||
const runner = runnerCtx.registry.get(command.name);
|
||||
if (!runner) {
|
||||
return { success: false, error: `Unknown command: ${command.name}` };
|
||||
|
|
|
|||
|
|
@ -10,9 +10,17 @@ export type CommandRunnerEvents = {
|
|||
prompt: PromptEvent;
|
||||
};
|
||||
|
||||
export type CommandResult<T=unknown> = {
|
||||
success: true;
|
||||
result: T;
|
||||
} | {
|
||||
success: false;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export type CommandRunnerContext<TContext> = {
|
||||
context: TContext;
|
||||
run: (input: string) => Promise<{ success: true; result: unknown } | { success: false; error: string }>;
|
||||
run: <T=unknown>(input: string) => Promise<CommandResult<T>>;
|
||||
runParsed: (command: Command) => Promise<{ success: true; result: unknown } | { success: false; error: string }>;
|
||||
prompt: (schema: CommandSchema | string) => Promise<Command>;
|
||||
on: <T extends keyof CommandRunnerEvents>(event: T, listener: (e: CommandRunnerEvents[T]) => void) => void;
|
||||
|
|
|
|||
Loading…
Reference in New Issue