Refactor util functions out of claude dev

This commit is contained in:
Saoud Rizwan
2024-09-29 02:52:43 -04:00
parent 9e01aaa3d3
commit ab327e3bbf
5 changed files with 89 additions and 81 deletions

View File

@@ -1,4 +1,5 @@
import * as path from "path"
import os from "os"
/*
The Node.js 'path' module resolves and normalizes paths differently depending on the platform:
@@ -76,3 +77,25 @@ function normalizePath(p: string): string {
}
return normalized
}
export function getReadablePath(cwd: string, relPath?: string): string {
relPath = relPath || ""
// path.resolve is flexible in that it will resolve relative paths like '../../' to the cwd and even ignore the cwd if the relPath is actually an absolute path
const absolutePath = path.resolve(cwd, relPath)
if (arePathsEqual(cwd, path.join(os.homedir(), "Desktop"))) {
// User opened vscode without a workspace, so cwd is the Desktop. Show the full absolute path to keep the user aware of where files are being created
return absolutePath.toPosix()
}
if (arePathsEqual(path.normalize(absolutePath), path.normalize(cwd))) {
return path.basename(absolutePath).toPosix()
} else {
// show the relative path to the cwd
const normalizedRelPath = path.relative(cwd, absolutePath)
if (absolutePath.includes(cwd)) {
return normalizedRelPath.toPosix()
} else {
// we are outside the cwd, so show the absolute path (useful for when claude passes in '../../' for example)
return absolutePath.toPosix()
}
}
}