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