|
1 | 1 | import * as vscode from 'vscode'
|
| 2 | +import type { WorkflowNodes } from '../../webviews/workflow/components/nodes/Nodes' |
2 | 3 | import { writeToCodyJSON } from '../commands/utils/config-file'
|
3 | 4 | import { migrateWorkflowData } from './workflow-migration'
|
4 | 5 |
|
@@ -76,3 +77,193 @@ export async function handleWorkflowLoad(): Promise<any> {
|
76 | 77 | }
|
77 | 78 | return []
|
78 | 79 | }
|
| 80 | + |
| 81 | +const CODY_NODES_DIR = '.cody/nodes' |
| 82 | + |
| 83 | +/** |
| 84 | + * Retrieves an array of custom workflow nodes from the `.cody/nodes` directory in the current workspace. |
| 85 | + * |
| 86 | + * This function ensures the `.cody/nodes` directory exists, and then reads all the JSON files in that directory, |
| 87 | + * parsing them as `WorkflowNodes` objects and returning them in an array. |
| 88 | + * |
| 89 | + * If the `.cody/nodes` directory does not exist, or if there are any errors loading the custom nodes, the function |
| 90 | + * will return an empty array and log the errors. |
| 91 | + * |
| 92 | + * @returns An array of `WorkflowNodes` objects representing the custom workflow nodes. |
| 93 | + */ |
| 94 | +export async function getCustomNodes(): Promise<WorkflowNodes[]> { |
| 95 | + try { |
| 96 | + const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri |
| 97 | + if (!workspaceRoot) { |
| 98 | + vscode.window.showErrorMessage('No workspace found.') |
| 99 | + return [] |
| 100 | + } |
| 101 | + const nodesDirUri = vscode.Uri.joinPath(workspaceRoot, CODY_NODES_DIR) |
| 102 | + try { |
| 103 | + // Ensure the directory exists. If it doesn't, return an empty array. |
| 104 | + await vscode.workspace.fs.createDirectory(nodesDirUri) |
| 105 | + } catch (e: any) { |
| 106 | + if (e.code !== 'FileExists') { |
| 107 | + console.warn('Directory .cody/nodes does not exist.') |
| 108 | + return [] |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + const files = await vscode.workspace.fs.readDirectory(nodesDirUri) |
| 113 | + const nodes: WorkflowNodes[] = [] |
| 114 | + |
| 115 | + for (const [filename, fileType] of files) { |
| 116 | + if (fileType === vscode.FileType.File && filename.endsWith('.json')) { |
| 117 | + try { |
| 118 | + const fileUri = vscode.Uri.joinPath(nodesDirUri, filename) |
| 119 | + const fileData = await vscode.workspace.fs.readFile(fileUri) |
| 120 | + const node = JSON.parse(fileData.toString()) as WorkflowNodes |
| 121 | + nodes.push(node) |
| 122 | + } catch (error: any) { |
| 123 | + console.error(`Failed to load custom node "${filename}": ${error.message}`) |
| 124 | + vscode.window.showErrorMessage( |
| 125 | + `Failed to load custom node "${filename}": ${error.message}` |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return nodes |
| 132 | + } catch (error: any) { |
| 133 | + console.error(`Failed to load custom nodes: ${error.message}`) |
| 134 | + vscode.window.showErrorMessage(`Failed to load custom nodes: ${error.message}`) |
| 135 | + return [] |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Saves a custom workflow node to the `.cody/nodes` directory. |
| 141 | + * |
| 142 | + * If the `.cody/nodes` directory does not exist, it will be created. The node is saved as a JSON file with the |
| 143 | + * sanitized title of the node as the filename. |
| 144 | + * |
| 145 | + * @param node - The `WorkflowNodes` object to be saved. |
| 146 | + * @returns A Promise that resolves when the node has been saved successfully. |
| 147 | + */ |
| 148 | +export async function saveCustomNodes(node: WorkflowNodes): Promise<void> { |
| 149 | + try { |
| 150 | + const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri |
| 151 | + if (!workspaceRoot) { |
| 152 | + vscode.window.showErrorMessage('No workspace found.') |
| 153 | + return |
| 154 | + } |
| 155 | + const nodesDirUri = vscode.Uri.joinPath(workspaceRoot, CODY_NODES_DIR) |
| 156 | + try { |
| 157 | + await vscode.workspace.fs.createDirectory(nodesDirUri) |
| 158 | + } catch (e: any) { |
| 159 | + if (e.code !== 'FileExists') { |
| 160 | + vscode.window.showErrorMessage(`Failed to create directory: ${e.message}`) |
| 161 | + return |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + const filename = `${sanitizeFilename(node.data.title)}.json` |
| 166 | + const fileUri = vscode.Uri.joinPath(nodesDirUri, filename) |
| 167 | + const { id, ...nodeToSave } = node |
| 168 | + await writeToCodyJSON(fileUri, nodeToSave) |
| 169 | + vscode.window.showInformationMessage(`Custom node "${node.data.title}" saved successfully.`) |
| 170 | + } catch (error: any) { |
| 171 | + vscode.window.showErrorMessage(`Failed to save custom node: ${error.message}`) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Deletes a custom workflow node from the `.cody/nodes` directory. |
| 177 | + * |
| 178 | + * The function first checks if the workspace has a valid root directory, and then displays a warning message to confirm the deletion of the custom node. If the user confirms the deletion, the function finds the corresponding JSON file in the `.cody/nodes` directory and deletes it. If the file is not found, an error message is displayed. |
| 179 | + * |
| 180 | + * @param nodeTitle - The title of the custom node to be deleted. |
| 181 | + * @returns A Promise that resolves when the node has been deleted successfully. |
| 182 | + */ |
| 183 | +export async function deleteCustomNode(nodeTitle: string): Promise<void> { |
| 184 | + try { |
| 185 | + const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri |
| 186 | + if (!workspaceRoot) { |
| 187 | + vscode.window.showErrorMessage('No workspace found.') |
| 188 | + return |
| 189 | + } |
| 190 | + const confirmed = await vscode.window.showWarningMessage( |
| 191 | + `Delete custom node "${nodeTitle}"?`, |
| 192 | + { modal: true }, |
| 193 | + 'Delete' |
| 194 | + ) |
| 195 | + if (confirmed !== 'Delete') { |
| 196 | + return |
| 197 | + } |
| 198 | + |
| 199 | + const nodesDirUri = vscode.Uri.joinPath(workspaceRoot, CODY_NODES_DIR) |
| 200 | + const files = await vscode.workspace.fs.readDirectory(nodesDirUri) |
| 201 | + const nodeFile = files.find(([filename]) => filename.startsWith(sanitizeFilename(nodeTitle))) |
| 202 | + if (!nodeFile) { |
| 203 | + vscode.window.showErrorMessage(`Custom node with title "${nodeTitle}" not found.`) |
| 204 | + return |
| 205 | + } |
| 206 | + const fileUri = vscode.Uri.joinPath(nodesDirUri, nodeFile[0]) |
| 207 | + await vscode.workspace.fs.delete(fileUri) |
| 208 | + vscode.window.showInformationMessage( |
| 209 | + `Custom node with title "${nodeTitle}" deleted successfully.` |
| 210 | + ) |
| 211 | + } catch (error: any) { |
| 212 | + vscode.window.showErrorMessage(`Failed to delete custom node: ${error.message}`) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +export async function renameCustomNode(oldNodeTitle: string, newNodeTitle: string): Promise<void> { |
| 217 | + try { |
| 218 | + const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri |
| 219 | + if (!workspaceRoot) { |
| 220 | + vscode.window.showErrorMessage('No workspace found.') |
| 221 | + return |
| 222 | + } |
| 223 | + |
| 224 | + const nodesDirUri = vscode.Uri.joinPath(workspaceRoot, CODY_NODES_DIR) |
| 225 | + const files = await vscode.workspace.fs.readDirectory(nodesDirUri) |
| 226 | + const oldNodeFile = files.find(([filename]) => |
| 227 | + filename.startsWith(sanitizeFilename(oldNodeTitle)) |
| 228 | + ) |
| 229 | + |
| 230 | + if (!oldNodeFile) { |
| 231 | + vscode.window.showErrorMessage(`Custom node with title "${oldNodeTitle}" not found.`) |
| 232 | + return |
| 233 | + } |
| 234 | + |
| 235 | + const oldFileUri = vscode.Uri.joinPath(nodesDirUri, oldNodeFile[0]) |
| 236 | + const fileData = await vscode.workspace.fs.readFile(oldFileUri) |
| 237 | + const node = JSON.parse(fileData.toString()) as WorkflowNodes |
| 238 | + |
| 239 | + // Update the node's title |
| 240 | + node.data.title = newNodeTitle |
| 241 | + |
| 242 | + // Construct the new file URI |
| 243 | + const newFilename = `${sanitizeFilename(newNodeTitle)}.json` |
| 244 | + const newFileUri = vscode.Uri.joinPath(nodesDirUri, newFilename) |
| 245 | + |
| 246 | + // Write the updated node to the new file |
| 247 | + const { id, ...nodeToSave } = node |
| 248 | + await writeToCodyJSON(newFileUri, nodeToSave) |
| 249 | + |
| 250 | + // Delete the old file |
| 251 | + await vscode.workspace.fs.delete(oldFileUri) |
| 252 | + |
| 253 | + vscode.window.showInformationMessage( |
| 254 | + `Custom node "${oldNodeTitle}" renamed to "${newNodeTitle}" successfully.` |
| 255 | + ) |
| 256 | + } catch (error: any) { |
| 257 | + vscode.window.showErrorMessage(`Failed to rename custom node: ${error.message}`) |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +/** |
| 262 | + * Sanitizes a filename by replacing any non-alphanumeric, non-underscore, and non-hyphen characters with an underscore. |
| 263 | + * |
| 264 | + * @param name - The filename to be sanitized. |
| 265 | + * @returns The sanitized filename. |
| 266 | + */ |
| 267 | +function sanitizeFilename(name: string): string { |
| 268 | + return name.replace(/[^a-zA-Z0-9_-]/g, '_') |
| 269 | +} |
0 commit comments