|
|
@@ -1,21 +1,27 @@
|
|
|
import fs from "fs/promises"
|
|
|
import path from "path"
|
|
|
|
|
|
+async function safeReadFile(filePath: string): Promise<string> {
|
|
|
+ try {
|
|
|
+ const content = await fs.readFile(filePath, "utf-8")
|
|
|
+ return content.trim()
|
|
|
+ } catch (err) {
|
|
|
+ const errorCode = (err as NodeJS.ErrnoException).code
|
|
|
+ if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
|
|
|
+ throw err
|
|
|
+ }
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
export async function loadRuleFiles(cwd: string): Promise<string> {
|
|
|
const ruleFiles = [".clinerules", ".cursorrules", ".windsurfrules"]
|
|
|
let combinedRules = ""
|
|
|
|
|
|
for (const file of ruleFiles) {
|
|
|
- try {
|
|
|
- const content = await fs.readFile(path.join(cwd, file), "utf-8")
|
|
|
- if (content.trim()) {
|
|
|
- combinedRules += `\n# Rules from ${file}:\n${content.trim()}\n`
|
|
|
- }
|
|
|
- } catch (err) {
|
|
|
- const errorCode = (err as NodeJS.ErrnoException).code
|
|
|
- if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
|
|
|
- throw err
|
|
|
- }
|
|
|
+ const content = await safeReadFile(path.join(cwd, file))
|
|
|
+ if (content) {
|
|
|
+ combinedRules += `\n# Rules from ${file}:\n${content}\n`
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -34,18 +40,8 @@ export async function addCustomInstructions(
|
|
|
// Load mode-specific rules if mode is provided
|
|
|
let modeRuleContent = ""
|
|
|
if (mode) {
|
|
|
- try {
|
|
|
- const modeRuleFile = `.clinerules-${mode}`
|
|
|
- const content = await fs.readFile(path.join(cwd, modeRuleFile), "utf-8")
|
|
|
- if (content.trim()) {
|
|
|
- modeRuleContent = content.trim()
|
|
|
- }
|
|
|
- } catch (err) {
|
|
|
- const errorCode = (err as NodeJS.ErrnoException).code
|
|
|
- if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
|
|
|
- throw err
|
|
|
- }
|
|
|
- }
|
|
|
+ const modeRuleFile = `.clinerules-${mode}`
|
|
|
+ modeRuleContent = await safeReadFile(path.join(cwd, modeRuleFile))
|
|
|
}
|
|
|
|
|
|
// Add language preference if provided
|