Adjust path-utils

This commit is contained in:
olaservo
2025-06-19 21:18:38 -07:00
parent 96e251fd93
commit 9dd94fd9a0

View File

@@ -53,31 +53,26 @@ export function normalizePath(p: string): string {
// Convert WSL or Unix-style Windows paths to Windows format
p = convertToWindowsPath(p);
// Check if this is a UNC path before normalization
const isUNCPath = p.startsWith('\\\\');
// For non-UNC paths, normalize double backslashes first
if (!isUNCPath) {
// Handle double backslashes, preserving leading UNC \\
if (p.startsWith('\\\\')) {
// For UNC paths, normalize double backslashes *after* the leading marker
const restOfPath = p.substring(2).replace(/\\\\/g, '\\');
p = '\\\\' + restOfPath;
} else {
// For non-UNC paths, normalize all double backslashes
p = p.replace(/\\\\/g, '\\');
}
// Use Node's path normalization, which handles . and .. segments
let normalized = path.normalize(p);
// Handle UNC paths after normalization to preserve the leading \\
if (isUNCPath) {
// Ensure UNC path starts with exactly two backslashes
if (normalized.startsWith('\\') && !normalized.startsWith('\\\\')) {
normalized = '\\' + normalized;
}
// Normalize any remaining double backslashes in the rest of the path
normalized = normalized.replace(/^(\\\\)(.*)/, (match, leading, rest) => {
return leading + rest.replace(/\\\\/g, '\\');
});
// Fix UNC paths after normalization (path.normalize can remove a leading backslash)
if (p.startsWith('\\\\') && !normalized.startsWith('\\\\')) {
normalized = '\\' + normalized;
}
// Handle Windows paths: convert slashes and ensure drive letter is capitalized
if (normalized.match(/^[a-zA-Z]:|^\/mnt\/[a-z]\/|^\/[a-z]\//i)) {
if (normalized.match(/^[a-zA-Z]:/)) {
let result = normalized.replace(/\//g, '\\');
// Capitalize drive letter if present
if (/^[a-z]:/.test(result)) {
@@ -86,8 +81,9 @@ export function normalizePath(p: string): string {
return result;
}
// Leave other paths unchanged
return normalized;
// For all other paths (including relative paths), convert forward slashes to backslashes
// This ensures relative paths like "some/relative/path" become "some\\relative\\path"
return normalized.replace(/\//g, '\\');
}
/**