| |
| |
| |
| |
| |
|
|
| import { type SlashCommand } from '../ui/commands/types.js'; |
|
|
| export type ParsedSlashCommand = { |
| commandToExecute: SlashCommand | undefined; |
| args: string; |
| canonicalPath: string[]; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export const parseSlashCommand = ( |
| query: string, |
| commands: readonly SlashCommand[], |
| ): ParsedSlashCommand => { |
| const trimmed = query.trim(); |
|
|
| const parts = trimmed.substring(1).trim().split(/\s+/); |
| const commandPath = parts.filter((p) => p); |
|
|
| let currentCommands = commands; |
| let commandToExecute: SlashCommand | undefined; |
| let pathIndex = 0; |
| const canonicalPath: string[] = []; |
| let parentCommand: SlashCommand | undefined; |
|
|
| for (const part of commandPath) { |
| |
| |
| |
| |
| |
|
|
| |
| let foundCommand = currentCommands.find((cmd) => cmd.name === part); |
|
|
| |
| if (!foundCommand) { |
| foundCommand = currentCommands.find((cmd) => |
| cmd.altNames?.includes(part), |
| ); |
| } |
|
|
| if (foundCommand) { |
| parentCommand = commandToExecute; |
| commandToExecute = foundCommand; |
| canonicalPath.push(foundCommand.name); |
| pathIndex++; |
| if (foundCommand.subCommands) { |
| currentCommands = foundCommand.subCommands; |
| } else { |
| break; |
| } |
| } else { |
| break; |
| } |
| } |
|
|
| const args = parts.slice(pathIndex).join(' '); |
|
|
| |
| |
| if ( |
| commandToExecute && |
| commandToExecute.takesArgs === false && |
| args.length > 0 && |
| parentCommand && |
| parentCommand.action |
| ) { |
| return { |
| commandToExecute: parentCommand, |
| args: parts.slice(pathIndex - 1).join(' '), |
| canonicalPath: canonicalPath.slice(0, -1), |
| }; |
| } |
|
|
| return { commandToExecute, args, canonicalPath }; |
| }; |
|
|