refactor: change how we define command
This commit is contained in:
parent
2d5200bdb7
commit
70b1ac1e43
|
|
@ -19,7 +19,7 @@ export type { Region, RegionAxis } from './core/region';
|
||||||
export { createRegion, createRegionAxis, applyAlign, shuffle, moveToRegion } from './core/region';
|
export { createRegion, createRegionAxis, applyAlign, shuffle, moveToRegion } from './core/region';
|
||||||
|
|
||||||
// Utils
|
// 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 { parseCommand, parseCommandSchema, validateCommand, parseCommandWithSchema, applyCommandSchema } from './utils/command';
|
||||||
|
|
||||||
export type { CommandRunner, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './utils/command';
|
export type { CommandRunner, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './utils/command';
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import type { Command, CommandSchema } from './types';
|
import type { Command, CommandSchema } from './types';
|
||||||
import type {
|
import type {
|
||||||
|
CommandDef, CommandFunction,
|
||||||
CommandResult,
|
CommandResult,
|
||||||
CommandRunner,
|
CommandRunner,
|
||||||
CommandRunnerContext,
|
CommandRunnerContext,
|
||||||
|
|
@ -16,9 +17,8 @@ type CanRunParsed = {
|
||||||
runParsed<T=unknown>(command: Command): Promise<CommandResult<T>>,
|
runParsed<T=unknown>(command: Command): Promise<CommandResult<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type CmdFunc<TContext> = (ctx: TContext, ...args: any[]) => Promise<unknown>;
|
|
||||||
export class CommandRegistry<TContext> extends Map<string, CommandRunner<TContext>>{
|
export class CommandRegistry<TContext> extends Map<string, CommandRunner<TContext>>{
|
||||||
asCommand<TFunc extends CmdFunc<TContext> = CmdFunc<TContext>>(schema: CommandSchema | string, run: TFunc) {
|
register<TFunc extends CommandFunction<TContext>>({schema,run}: CommandDef<TContext, TFunc>) {
|
||||||
const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema;
|
const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema;
|
||||||
registerCommand(this, {
|
registerCommand(this, {
|
||||||
schema: parsedSchema,
|
schema: parsedSchema,
|
||||||
|
|
|
||||||
|
|
@ -51,3 +51,9 @@ export type CommandRunner<TContext, TResult = unknown> = {
|
||||||
schema: CommandSchema;
|
schema: CommandSchema;
|
||||||
run: CommandRunnerHandler<TContext, TResult>;
|
run: CommandRunnerHandler<TContext, TResult>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type CommandFunction<TContext> = (ctx: TContext, ...args: any[]) => Promise<unknown>;
|
||||||
|
export type CommandDef<TContext,TFunc extends CommandFunction<TContext>> = {
|
||||||
|
schema: string | CommandSchema,
|
||||||
|
run: TFunc;
|
||||||
|
}
|
||||||
|
|
@ -18,5 +18,5 @@ export type {
|
||||||
CommandFlagSchema,
|
CommandFlagSchema,
|
||||||
CommandSchema,
|
CommandSchema,
|
||||||
} from './types';
|
} 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';
|
export type { CommandRegistry, CommandRunnerContextExport } from './command-registry';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue