* Try to load a specific command from a directory (supports symlinks)
( dirPath: string, name: string, source: "global" | "project", )
| 173 | * Try to load a specific command from a directory (supports symlinks) |
| 174 | */ |
| 175 | async function tryLoadCommand( |
| 176 | dirPath: string, |
| 177 | name: string, |
| 178 | source: "global" | "project", |
| 179 | ): Promise<Command | undefined> { |
| 180 | try { |
| 181 | const stats = await fs.stat(dirPath) |
| 182 | if (!stats.isDirectory()) { |
| 183 | return undefined |
| 184 | } |
| 185 | |
| 186 | // Try to find the command file directly |
| 187 | const commandFileName = `${name}.md` |
| 188 | const filePath = path.join(dirPath, commandFileName) |
| 189 | |
| 190 | // Check if this is a regular file first |
| 191 | let resolvedPath = filePath |
| 192 | let content: string | undefined |
| 193 | |
| 194 | try { |
| 195 | content = await fs.readFile(filePath, "utf-8") |
| 196 | } catch { |
| 197 | // File doesn't exist or can't be read - try resolving as symlink |
| 198 | const symlinkedPath = await tryResolveSymlinkedCommand(filePath) |
| 199 | if (symlinkedPath) { |
| 200 | try { |
| 201 | content = await fs.readFile(symlinkedPath, "utf-8") |
| 202 | resolvedPath = symlinkedPath |
| 203 | } catch { |
| 204 | // Symlink target can't be read |
| 205 | return undefined |
| 206 | } |
| 207 | } else { |
| 208 | return undefined |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if (!content) { |
| 213 | return undefined |
| 214 | } |
| 215 | |
| 216 | let parsed |
| 217 | let description: string | undefined |
| 218 | let argumentHint: string | undefined |
| 219 | let mode: string | undefined |
| 220 | let commandContent: string |
| 221 | |
| 222 | try { |
| 223 | // Try to parse frontmatter with gray-matter |
| 224 | parsed = matter(content) |
| 225 | description = |
| 226 | typeof parsed.data.description === "string" && parsed.data.description.trim() |
| 227 | ? parsed.data.description.trim() |
| 228 | : undefined |
| 229 | argumentHint = |
| 230 | typeof parsed.data["argument-hint"] === "string" && parsed.data["argument-hint"].trim() |
| 231 | ? parsed.data["argument-hint"].trim() |
| 232 | : undefined |
no test coverage detected