@@ -27,11 +27,58 @@ export async function getCommands(cwd: string): Promise<Command[]> {
2727}
2828
2929/**
30- * Get a specific command by name
30+ * Get a specific command by name (optimized to avoid scanning all commands)
3131 */
3232export async function getCommand ( cwd : string , name : string ) : Promise < Command | undefined > {
33- const commands = await getCommands ( cwd )
34- return commands . find ( ( cmd ) => cmd . name === name )
33+ // Try to find the command directly without scanning all commands
34+ const projectDir = path . join ( getProjectRooDirectoryForCwd ( cwd ) , "commands" )
35+ const globalDir = path . join ( getGlobalRooDirectory ( ) , "commands" )
36+
37+ // Check project directory first (project commands override global ones)
38+ const projectCommand = await tryLoadCommand ( projectDir , name , "project" )
39+ if ( projectCommand ) {
40+ return projectCommand
41+ }
42+
43+ // Check global directory if not found in project
44+ const globalCommand = await tryLoadCommand ( globalDir , name , "global" )
45+ return globalCommand
46+ }
47+
48+ /**
49+ * Try to load a specific command from a directory
50+ */
51+ async function tryLoadCommand (
52+ dirPath : string ,
53+ name : string ,
54+ source : "global" | "project" ,
55+ ) : Promise < Command | undefined > {
56+ try {
57+ const stats = await fs . stat ( dirPath )
58+ if ( ! stats . isDirectory ( ) ) {
59+ return undefined
60+ }
61+
62+ // Try to find the command file directly
63+ const commandFileName = `${ name } .md`
64+ const filePath = path . join ( dirPath , commandFileName )
65+
66+ try {
67+ const content = await fs . readFile ( filePath , "utf-8" )
68+ return {
69+ name,
70+ content : content . trim ( ) ,
71+ source,
72+ filePath,
73+ }
74+ } catch ( error ) {
75+ // File doesn't exist or can't be read
76+ return undefined
77+ }
78+ } catch ( error ) {
79+ // Directory doesn't exist or can't be read
80+ return undefined
81+ }
3582}
3683
3784/**
0 commit comments