(filePath: string)
| 132 | * asterisks, question marks, dollar signs, backticks, quotes, hash, and other shell metacharacters. |
| 133 | */ |
| 134 | export function escapePath(filePath: string): string { |
| 135 | let result = ''; |
| 136 | for (let i = 0; i < filePath.length; i++) { |
| 137 | const char = filePath[i]; |
| 138 | |
| 139 | // Count consecutive backslashes before this character |
| 140 | let backslashCount = 0; |
| 141 | for (let j = i - 1; j >= 0 && filePath[j] === '\\'; j--) { |
| 142 | backslashCount++; |
| 143 | } |
| 144 | |
| 145 | // Character is already escaped if there's an odd number of backslashes before it |
| 146 | const isAlreadyEscaped = backslashCount % 2 === 1; |
| 147 | |
| 148 | // Only escape if not already escaped |
| 149 | if (!isAlreadyEscaped && SHELL_SPECIAL_CHARS.test(char)) { |
| 150 | result += '\\' + char; |
| 151 | } else { |
| 152 | result += char; |
| 153 | } |
| 154 | } |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Unescapes special characters in a file path. |
no outgoing calls
no test coverage detected