From 70b1ac1e43fa66b54393388edc4146f2672cf3eb Mon Sep 17 00:00:00 2001 From: hypercross Date: Mon, 6 Apr 2026 10:06:04 +0800 Subject: [PATCH] refactor: change how we define command --- src/index.ts | 2 +- src/utils/command/command-registry.ts | 4 ++-- src/utils/command/command-runner.ts | 6 ++++++ src/utils/command/index.ts | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2557fd6..46c8735 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,7 +19,7 @@ export type { Region, RegionAxis } from './core/region'; export { createRegion, createRegionAxis, applyAlign, shuffle, moveToRegion } from './core/region'; // Utils -export type { Command, CommandResult, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command'; +export type { Command, CommandDef, CommandResult, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command'; export { parseCommand, parseCommandSchema, validateCommand, parseCommandWithSchema, applyCommandSchema } from './utils/command'; export type { CommandRunner, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './utils/command'; diff --git a/src/utils/command/command-registry.ts b/src/utils/command/command-registry.ts index 5d6e8aa..1375f86 100644 --- a/src/utils/command/command-registry.ts +++ b/src/utils/command/command-registry.ts @@ -1,5 +1,6 @@ import type { Command, CommandSchema } from './types'; import type { + CommandDef, CommandFunction, CommandResult, CommandRunner, CommandRunnerContext, @@ -16,9 +17,8 @@ type CanRunParsed = { runParsed(command: Command): Promise>, } -type CmdFunc = (ctx: TContext, ...args: any[]) => Promise; export class CommandRegistry extends Map>{ - asCommand = CmdFunc>(schema: CommandSchema | string, run: TFunc) { + register>({schema,run}: CommandDef) { const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema; registerCommand(this, { schema: parsedSchema, diff --git a/src/utils/command/command-runner.ts b/src/utils/command/command-runner.ts index d28d12a..c45c500 100644 --- a/src/utils/command/command-runner.ts +++ b/src/utils/command/command-runner.ts @@ -51,3 +51,9 @@ export type CommandRunner = { schema: CommandSchema; run: CommandRunnerHandler; }; + +export type CommandFunction = (ctx: TContext, ...args: any[]) => Promise; +export type CommandDef> = { + schema: string | CommandSchema, + run: TFunc; +} \ No newline at end of file diff --git a/src/utils/command/index.ts b/src/utils/command/index.ts index 4733320..31900c0 100644 --- a/src/utils/command/index.ts +++ b/src/utils/command/index.ts @@ -18,5 +18,5 @@ export type { CommandFlagSchema, CommandSchema, } from './types'; -export type { CommandRunner, CommandResult, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './command-runner'; +export type { CommandRunner, CommandDef, CommandResult, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './command-runner'; export type { CommandRegistry, CommandRunnerContextExport } from './command-registry';