| 61 | * - Handling file system errors and malformed files gracefully. |
| 62 | */ |
| 63 | export class FileCommandLoader implements ICommandLoader { |
| 64 | private readonly projectRoot: string; |
| 65 | |
| 66 | constructor(private readonly config: Config | null) { |
| 67 | this.projectRoot = config?.getProjectRoot() || process.cwd(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Loads all commands from user, project, and extension directories. |
| 72 | * Returns commands in order: user → project → extensions (alphabetically). |
| 73 | * |
| 74 | * Order is important for conflict resolution in CommandService: |
| 75 | * - User/project commands (without extensionName) use "last wins" strategy |
| 76 | * - Extension commands (with extensionName) get renamed if conflicts exist |
| 77 | * |
| 78 | * @param signal An AbortSignal to cancel the loading process. |
| 79 | * @returns A promise that resolves to an array of all loaded SlashCommands. |
| 80 | */ |
| 81 | async loadCommands(signal: AbortSignal): Promise<SlashCommand[]> { |
| 82 | const allCommands: SlashCommand[] = []; |
| 83 | const globOptions = { |
| 84 | nodir: true, |
| 85 | dot: true, |
| 86 | signal, |
| 87 | follow: true, |
| 88 | }; |
| 89 | |
| 90 | // Load commands from each directory |
| 91 | const commandDirs = this.getCommandDirectories(); |
| 92 | for (const dirInfo of commandDirs) { |
| 93 | try { |
| 94 | const files = await glob('**/*.toml', { |
| 95 | ...globOptions, |
| 96 | cwd: dirInfo.path, |
| 97 | }); |
| 98 | |
| 99 | const commandPromises = files.map((file) => |
| 100 | this.parseAndAdaptFile( |
| 101 | path.join(dirInfo.path, file), |
| 102 | dirInfo.path, |
| 103 | dirInfo.extensionName, |
| 104 | ), |
| 105 | ); |
| 106 | |
| 107 | const commands = (await Promise.all(commandPromises)).filter( |
| 108 | (cmd): cmd is SlashCommand => cmd !== null, |
| 109 | ); |
| 110 | |
| 111 | // Add all commands without deduplication |
| 112 | allCommands.push(...commands); |
| 113 | } catch (error) { |
| 114 | if ((error as NodeJS.ErrnoException).code !== 'ENOENT') { |
| 115 | console.error( |
| 116 | `[FileCommandLoader] Error loading commands from ${dirInfo.path}:`, |
| 117 | error, |
| 118 | ); |
| 119 | } |
| 120 | } |
nothing calls this directly
no outgoing calls
no test coverage detected