Skip to content

Commit 7aab01d

Browse files
committed
refactor(fs): consolidate safeReadFile implementation
- Moved `safeReadFile` function from multiple files to `src/utils/fs.ts` - Updated imports in affected files to use the centralized `safeReadFile` function - Simplified file reading logic by removing redundant implementations
1 parent ca6f151 commit 7aab01d

File tree

4 files changed

+35
-65
lines changed

4 files changed

+35
-65
lines changed

src/core/prompts/sections/custom-instructions.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,7 @@ import { Dirent } from "fs"
55
import { isLanguage } from "@roo-code/types"
66

77
import { LANGUAGES } from "../../../shared/language"
8-
9-
/**
10-
* Safely read a file and return its trimmed content
11-
*/
12-
async function safeReadFile(filePath: string): Promise<string> {
13-
try {
14-
const content = await fs.readFile(filePath, "utf-8")
15-
return content.trim()
16-
} catch (err) {
17-
const errorCode = (err as NodeJS.ErrnoException).code
18-
if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
19-
throw err
20-
}
21-
return ""
22-
}
23-
}
8+
import { safeReadFile } from "../../../utils/fs"
249

2510
/**
2611
* Check if a directory exists

src/core/prompts/sections/custom-system-prompt.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs/promises"
22
import path from "path"
33
import { Mode } from "../../../shared/modes"
4-
import { fileExistsAtPath } from "../../../utils/fs"
4+
import { fileExistsAtPath, safeReadFile } from "../../../utils/fs"
55

66
export type PromptVariables = {
77
workspace?: string
@@ -25,23 +25,6 @@ function interpolatePromptContent(content: string, variables: PromptVariables):
2525
return interpolatedContent
2626
}
2727

28-
/**
29-
* Safely reads a file, returning an empty string if the file doesn't exist
30-
*/
31-
async function safeReadFile(filePath: string): Promise<string> {
32-
try {
33-
const content = await fs.readFile(filePath, "utf-8")
34-
// When reading with "utf-8" encoding, content should be a string
35-
return content.trim()
36-
} catch (err) {
37-
const errorCode = (err as NodeJS.ErrnoException).code
38-
if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
39-
throw err
40-
}
41-
return ""
42-
}
43-
}
44-
4528
/**
4629
* Get the path to a system prompt file for a specific mode
4730
*/

src/core/webview/ClineProvider.ts

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import { McpServerManager } from "../../services/mcp/McpServerManager"
5050
import { ShadowCheckpointService } from "../../services/checkpoints/ShadowCheckpointService"
5151
import { CodeIndexManager } from "../../services/code-index/manager"
5252
import type { IndexProgressUpdate } from "../../services/code-index/interfaces/manager"
53-
import { fileExistsAtPath } from "../../utils/fs"
53+
import { fileExistsAtPath, safeReadFile } from "../../utils/fs"
5454
import { setTtsEnabled, setTtsSpeed } from "../../utils/tts"
5555
import { ContextProxy } from "../config/ContextProxy"
5656
import { ProviderSettingsManager } from "../config/ProviderSettingsManager"
@@ -988,6 +988,16 @@ export class ClineProvider
988988
await this.initClineWithHistoryItem({ ...historyItem, rootTask, parentTask })
989989
}
990990

991+
// Settings Directory
992+
993+
async ensureSettingsDirectoryExists(): Promise<string> {
994+
const { getSettingsDirectoryPath } = await import("../../utils/storage")
995+
const globalStoragePath = this.contextProxy.globalStorageUri.fsPath
996+
return getSettingsDirectoryPath(globalStoragePath)
997+
}
998+
999+
// Custom Instructions
1000+
9911001
async updateCustomInstructions(instructions?: string) {
9921002
const settingsDirPath = await this.ensureSettingsDirectoryExists()
9931003
const customInstructionsFilePath = path.join(settingsDirPath, GlobalFileNames.customInstructions)
@@ -1017,17 +1027,14 @@ export class ClineProvider
10171027
}
10181028

10191029
async refreshCustomInstructions(): Promise<void> {
1030+
const content = await this.readCustomInstructionsFromFile()
1031+
await this.updateCustomInstructions(content)
1032+
}
1033+
1034+
private async readCustomInstructionsFromFile(): Promise<string | undefined> {
10201035
const settingsDirPath = await this.ensureSettingsDirectoryExists()
10211036
const customInstructionsFilePath = path.join(settingsDirPath, GlobalFileNames.customInstructions)
1022-
let content: string | undefined = undefined
1023-
try {
1024-
content = await fs.readFile(customInstructionsFilePath, "utf-8")
1025-
} catch (error) {
1026-
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
1027-
throw error
1028-
}
1029-
}
1030-
await this.updateCustomInstructions(content)
1037+
return await safeReadFile(customInstructionsFilePath)
10311038
}
10321039

10331040
// MCP
@@ -1055,27 +1062,6 @@ export class ClineProvider
10551062
return mcpServersDir
10561063
}
10571064

1058-
async ensureSettingsDirectoryExists(): Promise<string> {
1059-
const { getSettingsDirectoryPath } = await import("../../utils/storage")
1060-
const globalStoragePath = this.contextProxy.globalStorageUri.fsPath
1061-
return getSettingsDirectoryPath(globalStoragePath)
1062-
}
1063-
1064-
private async readCustomInstructionsFromFile(): Promise<string | undefined> {
1065-
const settingsDirPath = await this.ensureSettingsDirectoryExists()
1066-
const customInstructionsFilePath = path.join(settingsDirPath, GlobalFileNames.customInstructions)
1067-
1068-
if (await fileExistsAtPath(customInstructionsFilePath)) {
1069-
try {
1070-
return await fs.readFile(customInstructionsFilePath, "utf-8")
1071-
} catch (error) {
1072-
this.log(`Error reading custom instructions file: ${error}`)
1073-
return undefined
1074-
}
1075-
}
1076-
return undefined
1077-
}
1078-
10791065
// OpenRouter
10801066

10811067
async handleOpenRouterCallback(code: string) {

src/utils/fs.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,19 @@ export async function fileExistsAtPath(filePath: string): Promise<boolean> {
4545
return false
4646
}
4747
}
48+
49+
/**
50+
* Safely read a file and return its trimmed content
51+
*/
52+
export async function safeReadFile(filePath: string): Promise<string> {
53+
try {
54+
const content = await fs.readFile(filePath, "utf-8")
55+
return content.trim()
56+
} catch (err) {
57+
const errorCode = (err as NodeJS.ErrnoException).code
58+
if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
59+
throw err
60+
}
61+
return ""
62+
}
63+
}

0 commit comments

Comments
 (0)