11import fs from "fs/promises"
22import path from "path"
33
4+ async function safeReadFile ( filePath : string ) : Promise < string > {
5+ try {
6+ const content = await fs . readFile ( filePath , "utf-8" )
7+ return content . trim ( )
8+ } catch ( err ) {
9+ const errorCode = ( err as NodeJS . ErrnoException ) . code
10+ if ( ! errorCode || ! [ "ENOENT" , "EISDIR" ] . includes ( errorCode ) ) {
11+ throw err
12+ }
13+ return ""
14+ }
15+ }
16+
417export async function loadRuleFiles ( cwd : string ) : Promise < string > {
518 const ruleFiles = [ ".clinerules" , ".cursorrules" , ".windsurfrules" ]
619 let combinedRules = ""
720
821 for ( const file of ruleFiles ) {
9- try {
10- const content = await fs . readFile ( path . join ( cwd , file ) , "utf-8" )
11- if ( content . trim ( ) ) {
12- combinedRules += `\n# Rules from ${ file } :\n${ content . trim ( ) } \n`
13- }
14- } catch ( err ) {
15- const errorCode = ( err as NodeJS . ErrnoException ) . code
16- if ( ! errorCode || ! [ "ENOENT" , "EISDIR" ] . includes ( errorCode ) ) {
17- throw err
18- }
22+ const content = await safeReadFile ( path . join ( cwd , file ) )
23+ if ( content ) {
24+ combinedRules += `\n# Rules from ${ file } :\n${ content } \n`
1925 }
2026 }
2127
@@ -34,18 +40,8 @@ export async function addCustomInstructions(
3440 // Load mode-specific rules if mode is provided
3541 let modeRuleContent = ""
3642 if ( mode ) {
37- try {
38- const modeRuleFile = `.clinerules-${ mode } `
39- const content = await fs . readFile ( path . join ( cwd , modeRuleFile ) , "utf-8" )
40- if ( content . trim ( ) ) {
41- modeRuleContent = content . trim ( )
42- }
43- } catch ( err ) {
44- const errorCode = ( err as NodeJS . ErrnoException ) . code
45- if ( ! errorCode || ! [ "ENOENT" , "EISDIR" ] . includes ( errorCode ) ) {
46- throw err
47- }
48- }
43+ const modeRuleFile = `.clinerules-${ mode } `
44+ modeRuleContent = await safeReadFile ( path . join ( cwd , modeRuleFile ) )
4945 }
5046
5147 // Add language preference if provided
0 commit comments