Skip to content

Commit 84910b8

Browse files
committed
PR fixes
1 parent 51c47dc commit 84910b8

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/services/command/commands.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
3232
export 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
/**

src/shared/context-mentions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const mentionRegex =
5858
export const mentionRegexGlobal = new RegExp(mentionRegex.source, "g")
5959

6060
// Regex to match command mentions like /command-name (only at start of message)
61-
export const commandRegexGlobal = /^\/([a-zA-Z0-9_\.-]+)(?=\s|$)/g
61+
export const commandRegexGlobal = /\/([a-zA-Z0-9_\.-]+)(?=\s|$)/g
6262

6363
export interface MentionSuggestion {
6464
type: "file" | "folder" | "git" | "problems"

0 commit comments

Comments
 (0)