(filePath: string)
| 176 | */ |
| 177 | |
| 178 | export function findSimilarFile(filePath: string): string | undefined { |
| 179 | const fs = getFsImplementation() |
| 180 | try { |
| 181 | const dir = dirname(filePath) |
| 182 | const fileBaseName = basename(filePath, extname(filePath)) |
| 183 | |
| 184 | // Get all files in the directory |
| 185 | const files = fs.readdirSync(dir) |
| 186 | |
| 187 | // Find files with the same base name but different extension |
| 188 | const similarFiles = files.filter( |
| 189 | file => |
| 190 | basename(file.name, extname(file.name)) === fileBaseName && |
| 191 | join(dir, file.name) !== filePath, |
| 192 | ) |
| 193 | |
| 194 | // Return just the filename of the first match if found |
| 195 | const firstMatch = similarFiles[0] |
| 196 | if (firstMatch) { |
| 197 | return firstMatch.name |
| 198 | } |
| 199 | return undefined |
| 200 | } catch (error) { |
| 201 | // Missing dir (ENOENT) is expected; for other errors log and return undefined |
| 202 | if (!isENOENT(error)) { |
| 203 | logError(error) |
| 204 | } |
| 205 | return undefined |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Marker included in file-not-found error messages that contain a cwd note. |
no test coverage detected