-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcustomCommandService.ts
More file actions
194 lines (169 loc) · 6.07 KB
/
customCommandService.ts
File metadata and controls
194 lines (169 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { SlashCommand } from '../utils/slash-commands';
export interface CustomCommand {
name: string; // Command name without prefix
file: string; // Path to the command file
type: 'project' | 'user'; // Whether this is a project or user command
description?: string; // Optional description (first line of the file)
}
export class CustomCommandService {
private projectCommands: CustomCommand[] = [];
private userCommands: CustomCommand[] = [];
constructor() {}
/**
* Scans for custom slash commands in both project and user directories
* @returns Promise that resolves when scanning is complete
*/
public async scanCustomCommands(): Promise<void> {
// Reset the command arrays
this.projectCommands = [];
this.userCommands = [];
// Scan project commands
await this.scanProjectCommands();
// Scan user commands
await this.scanUserCommands();
}
/**
* Gets all custom commands as SlashCommand objects
* @returns Array of SlashCommand objects
*/
public getCustomCommands(): SlashCommand[] {
const commands: SlashCommand[] = [];
const commandMap = new Map<string, SlashCommand>();
// Add project commands first (they take precedence)
for (const cmd of this.projectCommands) {
const command = {
command: `/${cmd.name}`,
description: `📄 ${cmd.description || cmd.name}`,
icon: '📄', // Document icon for project commands
isCustom: true
};
commandMap.set(cmd.name, command);
}
// Add user commands only if no project command with the same name exists
for (const cmd of this.userCommands) {
if (!commandMap.has(cmd.name)) {
const command = {
command: `/${cmd.name}`,
description: `👤 ${cmd.description || cmd.name}`,
icon: '👤', // User icon for user commands
isCustom: true
};
commandMap.set(cmd.name, command);
}
}
// Convert map to array
return Array.from(commandMap.values());
}
/**
* Scans for project commands in the .claude/commands directory
*/
private async scanProjectCommands(): Promise<void> {
// Get workspace folders
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders || workspaceFolders.length === 0) {
return;
}
// Use the first workspace folder
const workspaceRoot = workspaceFolders[0].uri.fsPath;
const commandsDir = path.join(workspaceRoot, '.claude', 'commands');
try {
// Check if the commands directory exists
const stats = await fs.promises.stat(commandsDir);
if (!stats.isDirectory()) {
return;
}
// Read the directory
const files = await fs.promises.readdir(commandsDir);
// Process each markdown file
for (const file of files) {
if (path.extname(file).toLowerCase() === '.md') {
const filePath = path.join(commandsDir, file);
const name = path.basename(file, '.md');
// Read the first line for description
try {
const content = await fs.promises.readFile(filePath, 'utf-8');
const firstLine = content.split('\n')[0].trim();
const description = firstLine.startsWith('#')
? firstLine.substring(1).trim()
: firstLine;
this.projectCommands.push({
name,
file: filePath,
type: 'project',
description: description || undefined
});
} catch (err) {
console.error(`Error reading project command file ${filePath}:`, err);
// Add without description if we couldn't read it
this.projectCommands.push({
name,
file: filePath,
type: 'project'
});
}
}
}
} catch (err) {
// Directory doesn't exist or can't be read, which is fine
console.log('No project commands directory found or error reading it:', err);
}
}
/**
* Scans for user commands in the ~/.claude/commands directory
*/
private async scanUserCommands(): Promise<void> {
try {
// Get user home directory
const homeDir = process.env.HOME || process.env.USERPROFILE;
if (!homeDir) {
console.error('Could not determine user home directory');
return;
}
const commandsDir = path.join(homeDir, '.claude', 'commands');
// Check if the directory exists
const stats = await fs.promises.stat(commandsDir);
if (!stats.isDirectory()) {
return;
}
// Read the directory
const files = await fs.promises.readdir(commandsDir);
// Process each markdown file
for (const file of files) {
if (path.extname(file).toLowerCase() === '.md') {
const filePath = path.join(commandsDir, file);
const name = path.basename(file, '.md');
// Read the first line for description
try {
const content = await fs.promises.readFile(filePath, 'utf-8');
const firstLine = content.split('\n')[0].trim();
const description = firstLine.startsWith('#')
? firstLine.substring(1).trim()
: firstLine;
this.userCommands.push({
name,
file: filePath,
type: 'user',
description: description || undefined
});
} catch (err) {
console.error(`Error reading user command file ${filePath}:`, err);
// Add without description if we couldn't read it
this.userCommands.push({
name,
file: filePath,
type: 'user'
});
}
}
}
} catch (err) {
// Directory doesn't exist or can't be read, which is fine
console.log('No user commands directory found or error reading it:', err);
}
}
}
// Create a singleton instance
export const customCommandService = new CustomCommandService();