From 53ac7952c7ac11770037fecccda5f0f2fffa3e0b Mon Sep 17 00:00:00 2001 From: Jacob Richman Date: Thu, 1 May 2025 18:02:04 -0700 Subject: Support escaping spaces in file paths. (#241) --- packages/server/src/utils/paths.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'packages/server') diff --git a/packages/server/src/utils/paths.ts b/packages/server/src/utils/paths.ts index f1a42131..6da3d4ab 100644 --- a/packages/server/src/utils/paths.ts +++ b/packages/server/src/utils/paths.ts @@ -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, ' '); +} -- cgit v1.2.3