forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstoragePathManager.ts
More file actions
149 lines (132 loc) · 4.38 KB
/
storagePathManager.ts
File metadata and controls
149 lines (132 loc) · 4.38 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
import * as vscode from "vscode"
import * as path from "path"
import * as fs from "fs/promises"
import { t } from "../i18n"
/**
* Gets the base storage path for conversations
* If a custom path is configured, uses that path
* Otherwise uses the default VSCode extension global storage path
*/
export async function getStorageBasePath(defaultPath: string): Promise<string> {
// Get user-configured custom storage path
let customStoragePath = ""
try {
// This is the line causing the error in tests
const config = vscode.workspace.getConfiguration("roo-cline")
customStoragePath = config.get<string>("customStoragePath", "")
} catch (error) {
console.warn("Could not access VSCode configuration - using default path")
return defaultPath
}
// If no custom path is set, use default path
if (!customStoragePath) {
return defaultPath
}
try {
// Ensure custom path exists
await fs.mkdir(customStoragePath, { recursive: true })
// Test if path is writable
const testFile = path.join(customStoragePath, ".write_test")
await fs.writeFile(testFile, "test")
await fs.rm(testFile)
return customStoragePath
} catch (error) {
// If path is unusable, report error and fall back to default path
console.error(`Custom storage path is unusable: ${error instanceof Error ? error.message : String(error)}`)
if (vscode.window) {
vscode.window.showErrorMessage(t("common:errors.custom_storage_path_unusable", { path: customStoragePath }))
}
return defaultPath
}
}
/**
* Gets the storage directory path for a task
*/
export async function getTaskDirectoryPath(globalStoragePath: string, taskId: string): Promise<string> {
const basePath = await getStorageBasePath(globalStoragePath)
const taskDir = path.join(basePath, "tasks", taskId)
await fs.mkdir(taskDir, { recursive: true })
return taskDir
}
/**
* Gets the settings directory path
*/
export async function getSettingsDirectoryPath(globalStoragePath: string): Promise<string> {
const basePath = await getStorageBasePath(globalStoragePath)
const settingsDir = path.join(basePath, "settings")
await fs.mkdir(settingsDir, { recursive: true })
return settingsDir
}
/**
* Gets the cache directory path
*/
export async function getCacheDirectoryPath(globalStoragePath: string): Promise<string> {
const basePath = await getStorageBasePath(globalStoragePath)
const cacheDir = path.join(basePath, "cache")
await fs.mkdir(cacheDir, { recursive: true })
return cacheDir
}
/**
* Prompts the user to set a custom storage path
* Displays an input box allowing the user to enter a custom path
*/
export async function promptForCustomStoragePath(): Promise<void> {
if (!vscode.window || !vscode.workspace) {
console.error("VS Code API not available")
return
}
let currentPath = ""
try {
const currentConfig = vscode.workspace.getConfiguration("roo-cline")
currentPath = currentConfig.get<string>("customStoragePath", "")
} catch (error) {
console.error("Could not access configuration")
return
}
const result = await vscode.window.showInputBox({
value: currentPath,
placeHolder: t("common:storage.path_placeholder"),
prompt: t("common:storage.prompt_custom_path"),
validateInput: (input) => {
if (!input) {
return null // Allow empty value (use default path)
}
try {
// Validate path format
path.parse(input)
// Check if path is absolute
if (!path.isAbsolute(input)) {
return t("common:storage.enter_absolute_path")
}
return null // Path format is valid
} catch (e) {
return t("common:storage.enter_valid_path")
}
},
})
// If user canceled the operation, result will be undefined
if (result !== undefined) {
try {
const currentConfig = vscode.workspace.getConfiguration("roo-cline")
await currentConfig.update("customStoragePath", result, vscode.ConfigurationTarget.Global)
if (result) {
try {
// Test if path is accessible
await fs.mkdir(result, { recursive: true })
vscode.window.showInformationMessage(t("common:info.custom_storage_path_set", { path: result }))
} catch (error) {
vscode.window.showErrorMessage(
t("common:errors.cannot_access_path", {
path: result,
error: error instanceof Error ? error.message : String(error),
}),
)
}
} else {
vscode.window.showInformationMessage(t("common:info.default_storage_path"))
}
} catch (error) {
console.error("Failed to update configuration", error)
}
}
}