Support escaping spaces in file paths. (#241)

This commit is contained in:
Jacob Richman
2025-05-01 18:02:04 -07:00
committed by GitHub
parent ca53565240
commit 53ac7952c7
5 changed files with 138 additions and 25 deletions

View File

@@ -100,3 +100,26 @@ export function makeRelative(
// If the paths are the same, path.relative returns '', return '.' instead
return relativePath || '.';
}
/**
* Escapes spaces in a file path.
*/
export function escapePath(filePath: string): string {
let result = '';
for (let i = 0; i < filePath.length; i++) {
// Only escape spaces that are not already escaped.
if (filePath[i] === ' ' && (i === 0 || filePath[i - 1] !== '\\')) {
result += '\\ ';
} else {
result += filePath[i];
}
}
return result;
}
/**
* Unescapes spaces in a file path.
*/
export function unescapePath(filePath: string): string {
return filePath.replace(/\\ /g, ' ');
}