(filePath: vscode.Uri, root: string, newFilePath: string)
| 90 | }; |
| 91 | |
| 92 | export const updateFileDependencies = async (filePath: vscode.Uri, root: string, newFilePath: string) => { |
| 93 | // Read the contents of the file |
| 94 | const fileContents = await vscode.workspace.fs.readFile(filePath); |
| 95 | let fileText = fileContents.toString(); |
| 96 | |
| 97 | // Regular expression to match import statements |
| 98 | const importRegex = /import\s.*?\sfrom\s['"](.*?)['"]/g; |
| 99 | const multilineImportRegex = /from\s['"](.*?)['"]/g; |
| 100 | // Find all import statements in the file |
| 101 | let match; |
| 102 | while ((match = importRegex.exec(fileText)) !== null) { |
| 103 | const importStatement = match[0]; |
| 104 | const dependencyPath = match[1]; |
| 105 | if (!importStatement || !dependencyPath) { |
| 106 | continue; |
| 107 | } |
| 108 | const difference = calculatePathDifference(root, filePath.fsPath); |
| 109 | const newDep = convertDependencyPath(convertRoutePathToTildeFromRelative(dependencyPath, difference), newFilePath); |
| 110 | // Check if the dependency path refers to the file being renamed |
| 111 | if (newDep !== dependencyPath) { |
| 112 | // Update the import statement with the new file name |
| 113 | const updatedImportStatement = importStatement.replace(dependencyPath, newDep); |
| 114 | fileText = fileText.replace(importStatement, updatedImportStatement); |
| 115 | } |
| 116 | } |
| 117 | while ((match = multilineImportRegex.exec(fileText)) !== null) { |
| 118 | const importStatement = match[0]; |
| 119 | const dependencyPath = match[1]; |
| 120 | if (!importStatement || !dependencyPath) { |
| 121 | continue; |
| 122 | } |
| 123 | const difference = calculatePathDifference(root, filePath.fsPath); |
| 124 | const newDep = convertDependencyPath(convertRoutePathToTildeFromRelative(dependencyPath, difference), newFilePath); |
| 125 | // Check if the dependency path refers to the file being renamed |
| 126 | if (newDep !== dependencyPath) { |
| 127 | // Update the import statement with the new file name |
| 128 | const updatedImportStatement = importStatement.replace(dependencyPath, newDep); |
| 129 | fileText = fileText.replace(importStatement, updatedImportStatement); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Write the updated contents back to the file |
| 134 | await vscode.workspace.fs.writeFile(filePath, Buffer.from(fileText)); |
| 135 | }; |
| 136 | |
| 137 | export const addUnderscoreToFirstPart = (str: string = ""): string => { |
| 138 | const parts = str.split("."); |
no test coverage detected