MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / scanCommandDirectory

Function scanCommandDirectory

src/services/command/commands.ts:269–350  ·  view source on GitHub ↗

* Scan a specific command directory (supports symlinks)

(
	dirPath: string,
	source: "global" | "project",
	commands: Map<string, Command>,
)

Source from the content-addressed store, hash-verified

267 * Scan a specific command directory (supports symlinks)
268 */
269async function scanCommandDirectory(
270 dirPath: string,
271 source: "global" | "project",
272 commands: Map<string, Command>,
273): Promise<void> {
274 try {
275 const stats = await fs.stat(dirPath)
276 if (!stats.isDirectory()) {
277 return
278 }
279
280 const entries = await fs.readdir(dirPath, { withFileTypes: true })
281
282 // Collect all command files, including those from symlinks
283 const fileInfo: CommandFileInfo[] = []
284 const initialPromises: Promise<void>[] = []
285
286 for (const entry of entries) {
287 initialPromises.push(resolveCommandDirectoryEntry(entry, dirPath, fileInfo, 0))
288 }
289
290 // Wait for all files to be resolved
291 await Promise.all(initialPromises)
292
293 // Process each collected file
294 for (const { originalPath, resolvedPath } of fileInfo) {
295 // Command name comes from the original path (symlink name if symlinked)
296 const commandName = getCommandNameFromFile(path.basename(originalPath))
297
298 try {
299 const content = await fs.readFile(resolvedPath, "utf-8")
300
301 let parsed
302 let description: string | undefined
303 let argumentHint: string | undefined
304 let mode: string | undefined
305 let commandContent: string
306
307 try {
308 // Try to parse frontmatter with gray-matter
309 parsed = matter(content)
310 description =
311 typeof parsed.data.description === "string" && parsed.data.description.trim()
312 ? parsed.data.description.trim()
313 : undefined
314 argumentHint =
315 typeof parsed.data["argument-hint"] === "string" && parsed.data["argument-hint"].trim()
316 ? parsed.data["argument-hint"].trim()
317 : undefined
318 mode =
319 typeof parsed.data.mode === "string" && parsed.data.mode.trim()
320 ? parsed.data.mode.trim()
321 : undefined
322 commandContent = parsed.content.trim()
323 } catch {
324 // If frontmatter parsing fails, treat the entire content as command content
325 description = undefined
326 argumentHint = undefined

Callers 1

getCommandsFunction · 0.85

Calls 8

getCommandNameFromFileFunction · 0.85
statMethod · 0.80
allMethod · 0.80
hasMethod · 0.65
setMethod · 0.65
warnMethod · 0.65
readFileMethod · 0.45

Tested by

no test coverage detected