continue-custom/extensions/cli/src/util/pathResolver.ts
Patrick Erichsen acad86f263
Apply suggestion from @cubic-dev-ai[bot]
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
2025-10-17 10:19:15 -07:00

33 lines
977 B
TypeScript

import * as fs from "fs";
import * as os from "os";
import * as path from "path";
/**
* Resolves user-provided paths for CLI context.
* Handles absolute paths, tilde paths, and relative paths.
*/
export function resolveInputPath(inputPath: string): string | null {
// Trim whitespace
const trimmedPath = inputPath.trim();
// Expand tilde paths
let expandedPath = trimmedPath;
if (trimmedPath.startsWith("~/") || trimmedPath.startsWith("~\\")) {
expandedPath = path.join(os.homedir(), trimmedPath.slice(2));
} else if (trimmedPath === "~") {
expandedPath = os.homedir();
} else if (trimmedPath.startsWith("./")) {
// Keep relative paths starting with ./ as is (relative to cwd)z
expandedPath = trimmedPath;
}
// Resolve the path (handles both absolute and relative paths)
const resolvedPath = path.resolve(expandedPath);
// Check if the path exists
if (fs.existsSync(resolvedPath)) {
return resolvedPath;
}
return null;
}